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.dto.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.dto.ComplianceCheckDTO;
import digital.laboratory.platform.reagent.entity.ComplianceCheck;
import digital.laboratory.platform.reagent.service.ComplianceCheckService;
import digital.laboratory.platform.reagent.vo.ComplianceCheckVO;
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;
import java.util.List;

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

    private final ComplianceCheckService complianceCheckService;

    /**
     * 通过id查询(符合性检查记录表)
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{complianceCheckId}")
//	@PreAuthorize("@pms.hasPermission('reagent_compliance_check_get')" )
    public R<ComplianceCheckVO> getById(@PathVariable("complianceCheckId") String complianceCheckId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ComplianceCheckVO complianceCheckVO = complianceCheckService.getComplianceCheckVO(complianceCheckId);

        return R.ok(complianceCheckVO);
    }

    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param complianceCheck (符合性检查记录表)
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_get')")
    public R<IPage<ComplianceCheckVO>> getComplianceCheckPage(Page<ComplianceCheck> page, ComplianceCheck complianceCheck, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        IPage<ComplianceCheckVO> complianceCheckSList = complianceCheckService.getComplianceCheckVOPage(page, Wrappers.<ComplianceCheck>query()
                .eq("create_by", dlpUser.getId())
                .orderByDesc("create_time")
        );
        return R.ok(complianceCheckSList);
//		return R.ok(complianceCheckService.page(page, Wrappers.query(complianceCheck)));
    }


    /**
     * 新增符合性检查记录表(试剂耗材)
     *
     * @param complianceCheckDTO (符合性检查记录表)
     * @return R
     */
    @ApiOperation(value = "新增符合性检查记录表(试剂耗材)", notes = "新增符合性检查记录表(试剂耗材)")
    @SysLog("新增(符合性检查记录表)")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_add')")
    public R<ComplianceCheck> postAddObject(@RequestBody ComplianceCheckDTO complianceCheckDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ComplianceCheck complianceCheck = complianceCheckService.addCheck(complianceCheckDTO, dlpUser);

        if (complianceCheck != null) {
            return R.ok(complianceCheck, "保存成功");
        } else {
            return R.failed(complianceCheck, "保存失败");
        }
    }
    /**
     * 手动新增符合性检查记录表(试剂耗材)
     *
     * @param complianceCheckDTO (手动新增符合性检查记录表)
     * @return R
     */
    @ApiOperation(value = "手动新增符合性检查记录表(试剂耗材)", notes = "手动新增符合性检查记录表(试剂耗材)")
    @SysLog("新增(符合性检查记录表)")
    @PostMapping("/addList")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_add')")
    public R<List<ComplianceCheck>> addCheckList(@RequestBody List<ComplianceCheckDTO> complianceCheckDTOList, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        List<ComplianceCheck> complianceCheckList = complianceCheckService.addCheckList(complianceCheckDTOList, dlpUser);

        if (complianceCheckList != null) {
            return R.ok(complianceCheckList, "保存成功");
        } else {
            return R.failed(complianceCheckList, "保存失败");
        }
    }


    /**
     * 通过id删除(符合性检查记录表)
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "通过id删除(符合性检查记录表)", notes = "通过id删除(符合性检查记录表)")
    @SysLog("通过id删除(符合性检查记录表)")
    @DeleteMapping("/{complianceCheckId}")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_del')" )
    public R<String> deleteById(@PathVariable String complianceCheckId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ;

        if (complianceCheckService.delCheckById(complianceCheckId)) {
            return R.ok("删除成功");
        } else {
            return R.failed("删除失败");
        }

    }

    /**
     * 编辑符合性检查记录表（录入结果）
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "编辑符合性检查记录表", notes = "编辑符合性检查记录表")
    @SysLog("编辑符合性检查记录表")
    @PutMapping("/edit")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_del')" )
    public R<String> editCheckById(@RequestBody ComplianceCheckDTO complianceCheckDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ComplianceCheck complianceCheck = complianceCheckService.editCheckById(complianceCheckDTO);

        if (complianceCheck != null) {
            return R.ok("保存成功");
        } else {
            return R.failed("保存失败");
        }

    }

    /**
     * 录入符合性检查方案
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "录入符合性检查方案", notes = "录入符合性检查方案")
    @SysLog("编辑符合性检查记录表")
    @PutMapping("/addScheme")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_del')" )
    public R<String> addScheme(@RequestBody ComplianceCheckDTO complianceCheckDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ComplianceCheck complianceCheck = complianceCheckService.addScheme(complianceCheckDTO);

        if (complianceCheck != null) {
            return R.ok("保存成功");
        } else {
            return R.failed("保存失败");
        }

    }

    /**
     * 提交符合性检查记录表
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "提交符合性检查记录表", notes = "提交符合性检查记录表")
    @SysLog("提交符合性检查记录表")
    @PutMapping("/commit/{complianceCheckId}")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_del')" )
    public R<String> commitCheck(@RequestBody ComplianceCheckDTO complianceCheckDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ComplianceCheck complianceCheck = complianceCheckService.commitCheck(complianceCheckDTO,dlpUser);

        if (complianceCheck != null) {
            return R.ok("提交成功");
        } else {
            return R.failed("提交失败");
        }

    }

    /**
     * 一级审核符合性检查记录表
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "一级审核符合性检查记录表", notes = "一级审核符合性检查记录表")
    @SysLog("一级审核符合性检查记录表")
    @PutMapping("/primaryAudit")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_del')" )
    public R<String> primaryAuditCheck(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        ComplianceCheck complianceCheck = complianceCheckService.primaryAuditCheck(auditAndApproveDTO, dlpUser);

        if (complianceCheck != null) {
            return R.ok("审核成功");
        } else {
            return R.failed("审核失败");
        }

    }

    /**
     * 二级审核符合性检查记录表
     *
     * @param complianceCheckId id
     * @return R
     */
    @ApiOperation(value = "二级审核符合性检查记录表", notes = "二级审核符合性检查记录表")
    @SysLog("二级审核符合性检查记录表")
    @PutMapping("/secondaryAudit")
//    @PreAuthorize("@pms.hasPermission('reagent_compliance_check_del')" )
    public R<String> secondaryAuditCheck(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        ComplianceCheck complianceCheck = complianceCheckService.secondaryAuditCheck(auditAndApproveDTO, dlpUser);
        if (complianceCheck != null) {
            return R.ok("审核成功");
        } else {
            return R.failed("审核失败");
        }

    }

}
