package digital.laboratory.platform.reagent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.log.annotation.SysLog;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.reagent.entity.ProvideServicesOrSupplies;
import digital.laboratory.platform.reagent.service.ProvideServicesOrSuppliesService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;

/**
 * 提供服务或供应品
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe 提供服务或供应品 前端控制器
 *
 * 这是与表示层的接口, 不应该接业务逻辑写在这里, 业务逻辑应该写在 service 中
 * 这里写什么:
 *     为前端提供数据, 接受前端的数据
 *     为前端提供的数据, 从 service 取得后, 可以做一些适当的加工, 这种加工不是业务层面的, 只能是数据格式上, 为方便前端处理
 *     接受前端的数据, 每一个函数的参数可以先做一些整理后, 再调用 service 中的函数。这里对参数的整理, 应该只是格式上的, 而不能是业务上的
 * 数据层在 mapper 中, 数据层不涉及业务, 只管技术上的 对象<->表 之间的转换
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/provide_services_or_supplies" )
@Api(value = "provide_services_or_supplies", tags = "提供服务或供应品管理")
public class ProvideServicesOrSuppliesController {

    private final  ProvideServicesOrSuppliesService provideServicesOrSuppliesService;

	/**
	 * 通过id查询提供服务或供应品
	 * @param provideServicesOrSuppliesId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{provideServicesOrSuppliesId}" )
	@PreAuthorize("@pms.hasPermission('reagent_provide_services_or_supplies_get')" )
	public R<ProvideServicesOrSupplies> getById(@PathVariable("provideServicesOrSuppliesId" ) String provideServicesOrSuppliesId, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ProvideServicesOrSupplies provideServicesOrSupplies = provideServicesOrSuppliesService.getById(provideServicesOrSuppliesId);
		return R.ok(provideServicesOrSupplies);
		//return R.ok(provideServicesOrSuppliesService.getById(provideServicesOrSuppliesid));
	}

	/**
     * 分页查询
     * @param page 分页对象
     * @param provideServicesOrSupplies 提供服务或供应品
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('reagent_provide_services_or_supplies_get')" )
    public R<IPage<ProvideServicesOrSupplies>> getProvideServicesOrSuppliesPage(Page<ProvideServicesOrSupplies> page, ProvideServicesOrSupplies provideServicesOrSupplies, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		IPage<ProvideServicesOrSupplies> provideServicesOrSuppliesSList = provideServicesOrSuppliesService.page(page, Wrappers.<ProvideServicesOrSupplies>query()
				.eq("create_by", dlpUser.getId())
				.orderByDesc("create_time")
		);
		return R.ok(provideServicesOrSuppliesSList);
//		return R.ok(provideServicesOrSuppliesService.page(page, Wrappers.query(provideServicesOrSupplies)));
    }


    /**
     * 新增提供服务或供应品
     * @param provideServicesOrSupplies 提供服务或供应品
     * @return R
     */
    @ApiOperation(value = "新增提供服务或供应品", notes = "新增提供服务或供应品")
    @SysLog("新增提供服务或供应品" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('reagent_provide_services_or_supplies_add')" )
    public R<ProvideServicesOrSupplies> postAddObject(@RequestBody ProvideServicesOrSupplies provideServicesOrSupplies, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        provideServicesOrSupplies.setEvaluationFormId(IdWorker.get32UUID().toUpperCase());
		if (provideServicesOrSuppliesService.save(provideServicesOrSupplies)) {
			return R.ok(provideServicesOrSupplies, "对象创建成功");
		}
		else {
			return R.failed(provideServicesOrSupplies, "对象创建失败");
		}
    }

    /**
     * 修改提供服务或供应品
     * @param provideServicesOrSupplies 提供服务或供应品
     * @return R
     */
    @ApiOperation(value = "修改提供服务或供应品", notes = "修改提供服务或供应品")
    @SysLog("修改提供服务或供应品" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('reagent_provide_services_or_supplies_edit')" )
    public R<ProvideServicesOrSupplies> putUpdateById(@RequestBody ProvideServicesOrSupplies provideServicesOrSupplies, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		if (provideServicesOrSuppliesService.updateById(provideServicesOrSupplies)) {
			return R.ok(provideServicesOrSupplies, "保存对象成功");
		}
		else {
			return R.failed(provideServicesOrSupplies, "保存对象失败");
		}
    }

    /**
     * 通过id删除提供服务或供应品
     * @param provideServicesOrSuppliesid id
     * @return R
     */
    @ApiOperation(value = "通过id删除提供服务或供应品", notes = "通过id删除提供服务或供应品")
    @SysLog("通过id删除提供服务或供应品" )
    @DeleteMapping("/{provideServicesOrSuppliesid}" )
    @PreAuthorize("@pms.hasPermission('reagent_provide_services_or_supplies_del')" )
    public R<ProvideServicesOrSupplies> deleteById(@PathVariable String provideServicesOrSuppliesid, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ProvideServicesOrSupplies oldProvideServicesOrSupplies = provideServicesOrSuppliesService.getById(provideServicesOrSuppliesid);

		if (provideServicesOrSuppliesService.removeById(provideServicesOrSuppliesid)) {
			return R.ok(oldProvideServicesOrSupplies, "对象删除成功");
		}
		else {
			return R.failed(oldProvideServicesOrSupplies, "对象删除失败");
		}

    }

}
