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.AcceptanceRecordFormDTO;
import digital.laboratory.platform.reagent.dto.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.entity.AcceptanceRecordForm;
import digital.laboratory.platform.reagent.service.AcceptanceRecordFormService;
import digital.laboratory.platform.reagent.vo.AcceptanceRecordFormVO;
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 (验收记录表) 前端控制器
 * <p>
 * 这是与表示层的接口, 不应该接业务逻辑写在这里, 业务逻辑应该写在 service 中
 * 这里写什么:
 * 为前端提供数据, 接受前端的数据
 * 为前端提供的数据, 从 service 取得后, 可以做一些适当的加工, 这种加工不是业务层面的, 只能是数据格式上, 为方便前端处理
 * 接受前端的数据, 每一个函数的参数可以先做一些整理后, 再调用 service 中的函数。这里对参数的整理, 应该只是格式上的, 而不能是业务上的
 * 数据层在 mapper 中, 数据层不涉及业务, 只管技术上的 对象<->表 之间的转换
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/acceptance_record_form")
@Api(value = "acceptance_record_form", tags = "(验收记录表)管理")
public class AcceptanceRecordFormController {

    private final AcceptanceRecordFormService acceptanceRecordFormService;

    /**
     * 通过id查询(验收记录表)
     *
     * @param acceptanceRecordFormId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{acceptanceRecordFormId}")
//	@PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_get')" )
    public R<AcceptanceRecordFormVO> getById(@PathVariable("acceptanceRecordFormId") String acceptanceRecordFormId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        AcceptanceRecordFormVO acceptanceRecordFormVO = acceptanceRecordFormService.getAcceptanceRecordFormVO(acceptanceRecordFormId);
        return R.ok(acceptanceRecordFormVO);
        //return R.ok(acceptanceRecordFormService.getById(acceptanceRecordFormId));
    }

    /**
     * 分页查询
     *
     * @param page                 分页对象
     * @param acceptanceRecordForm (验收记录表)
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_get')" )
    public R<IPage<AcceptanceRecordFormVO>> getAcceptanceRecordFormPage(Page<AcceptanceRecordForm> page, AcceptanceRecordForm acceptanceRecordForm, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        IPage<AcceptanceRecordFormVO> acceptanceRecordFormVOPage = acceptanceRecordFormService.getAcceptanceRecordFormVOPage(page, Wrappers.<AcceptanceRecordForm>query()
                .eq("create_by", dlpUser.getId())
                .orderByDesc("create_time")
        );
        return R.ok(acceptanceRecordFormVOPage);
//		return R.ok(acceptanceRecordFormService.page(page, Wrappers.query(acceptanceRecordForm)));
    }


    /**
     * 录入(验收记录表)
     *
     * @param acceptanceRecordFormDTO (验收记录表)
     * @return R
     */
    @ApiOperation(value = "录入(验收记录表)", notes = "录入(验收记录表)")
    @SysLog("录入(验收记录表)")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_add')" )
    public R<AcceptanceRecordForm> postAddObject(@RequestBody AcceptanceRecordFormDTO acceptanceRecordFormDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        AcceptanceRecordForm acceptanceRecordForm = acceptanceRecordFormService.editFrom(acceptanceRecordFormDTO);

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

    }

    /**
     * 提交(验收记录表)
     *
     * @param acceptanceRecordFormDTO (验收记录表)
     * @return R
     */
    @ApiOperation(value = "提交(验收记录表)", notes = "提交(验收记录表)")
    @SysLog("提交(验收记录表)")
    @PostMapping("/commit")
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_edit')")
    public R<AcceptanceRecordForm> putUpdateById(@RequestBody AcceptanceRecordFormDTO acceptanceRecordFormDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        AcceptanceRecordForm acceptanceRecordForm = acceptanceRecordFormService.commitForm(acceptanceRecordFormDTO);

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

//    /**
//     * 通过id删除(验收记录表)
//     *
//     * @param acceptanceRecordFormId id
//     * @return R
//     */
//    @ApiOperation(value = "通过id删除(验收记录表)", notes = "通过id删除(验收记录表)")
//    @SysLog("通过id删除(验收记录表)")
//    @DeleteMapping("/{acceptanceRecordFormId}")
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_del')")
//    public R<AcceptanceRecordForm> deleteById(@PathVariable String acceptanceRecordFormId, HttpServletRequest theHttpServletRequest) {
//        Principal principal = theHttpServletRequest.getUserPrincipal();
//        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
//
//        AcceptanceRecordForm oldAcceptanceRecordForm = acceptanceRecordFormService.getById(acceptanceRecordFormId);
//
//        if (acceptanceRecordFormService.removeById(acceptanceRecordFormId)) {
//            return R.ok(oldAcceptanceRecordForm, "对象删除成功");
//        } else {
//            return R.failed(oldAcceptanceRecordForm, "对象删除失败");
//        }
//
//    }

    /**
     * 一级审核(验收记录表)
     *
     * @param auditAndApproveDTO (验收记录表)
     * @return R
     */
    @ApiOperation(value = "一级审核(验收记录表)", notes = "一级审核(验收记录表)")
    @SysLog("一级审核(验收记录表)")
    @PutMapping("/primary")
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_edit')")
    public R<AcceptanceRecordForm> primaryAudit(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        AcceptanceRecordForm acceptanceRecordForm = acceptanceRecordFormService.primaryAudit(auditAndApproveDTO,dlpUser);

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

    /**
     * 二级审核(验收记录表)
     *
     * @param auditAndApproveDTO (验收记录表)
     * @return R
     */
    @ApiOperation(value = "二级审核(验收记录表)", notes = "二级审核(验收记录表)")
    @SysLog("二级审核(验收记录表)")
    @PutMapping("/secondary")
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_edit')")
    public R<AcceptanceRecordForm> secondaryAudit(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        AcceptanceRecordForm acceptanceRecordForm = acceptanceRecordFormService.secondaryAudit(auditAndApproveDTO,dlpUser);

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

    /**
     * 三级审核(验收记录表)
     *
     * @param auditAndApproveDTO (验收记录表)
     * @return R
     */
    @ApiOperation(value = "三级审核(验收记录表)", notes = "三级审核(验收记录表)")
    @SysLog("三级审核(验收记录表)")
    @PutMapping("/threeLevel")
//    @PreAuthorize("@pms.hasPermission('reagent_acceptance_record_form_edit')")
    public R<AcceptanceRecordForm> threeLevelAudit(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        AcceptanceRecordForm acceptanceRecordForm = acceptanceRecordFormService.threeLevelAudit(auditAndApproveDTO,dlpUser);

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

}
