package digital.laboratory.platform.reagent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.AuditDecentralizedRequestDTO;
import digital.laboratory.platform.reagent.dto.DecentralizedRequestDTO;
import digital.laboratory.platform.reagent.entity.DecentralizeDetails;
import digital.laboratory.platform.reagent.entity.DecentralizedRequest;
import digital.laboratory.platform.reagent.service.DecentralizeDetailsService;
import digital.laboratory.platform.reagent.service.DecentralizedRequestService;
import digital.laboratory.platform.reagent.vo.DecentralizedRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
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("/decentralized_request")
@Api(value = "decentralized_request", tags = "(分散采购申请)管理")
public class DecentralizedRequestController {

    @Autowired
    private final DecentralizedRequestService decentralizedRequestService;
    @Autowired
    private final DecentralizeDetailsService decentralizeDetailsService;

    /**
     * 通过id查询(分散采购申请)
     *
     * @param decentralizedRequestId id
     * @return R
     */
    @ApiOperation(value = "通过id查询分散采购申请", notes = "通过id查询分散采购申请")
    @GetMapping("/{decentralizedRequestId}")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_get')")
    public R<DecentralizedRequest> getById(@PathVariable("decentralizedRequestId") String decentralizedRequestId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequestVO requestById = decentralizedRequestService.getRequestById(decentralizedRequestId);
        return R.ok(requestById);
        //return R.ok(decentralizedRequestService.getById(decentralizedRequestId));
    }

    /**
     * 分页查询
     *
     * @param page                 分页对象
     * @param decentralizedRequest (分散采购申请)
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_get')")
    public R<IPage<DecentralizedRequest>> getDecentralizedRequestPage(Page<DecentralizedRequest> page, DecentralizedRequest decentralizedRequest, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        IPage<DecentralizedRequest> decentralizedRequestSList = decentralizedRequestService.page(page, Wrappers.<DecentralizedRequest>query()
                .eq("create_by", dlpUser.getId())
                .orderByDesc("create_time")
        );
        return R.ok(decentralizedRequestSList);
//		return R.ok(decentralizedRequestService.page(page, Wrappers.query(decentralizedRequest)));
    }


    /**
     * 新增(分散采购申请)
     *
     * @param decentralizedRequestDTOList (分散采购申请)
     * @return R
     */
    @ApiOperation(value = "保存分散采购申请", notes = "保存分散采购申请")
    @SysLog("新增分散采购申请")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_add')")
    public R<DecentralizedRequest> postAddObject(@RequestBody List<DecentralizedRequestDTO> decentralizedRequestDTOList, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.addRequest(decentralizedRequestDTOList, dlpUser);

        if (decentralizedRequest != null) {

            return R.ok(decentralizedRequest, "保存成功");
        } else {
            return R.failed("保存失败");
        }


    }

    /**
     * 新增分散采购申请明细
     *
     * @param decentralizedRequestDTO (新增分散采购申请明细)
     * @return R
     */
    @ApiOperation(value = "新增分散采购申请明细", notes = "新增分散采购申请明细")
    @SysLog("新增分散采购申请明细")
    @PostMapping("/addDetails")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralize_details_add')")
    public R<DecentralizeDetails> addDetails(@RequestBody DecentralizedRequestDTO decentralizedRequestDTO, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizeDetails decentralizeDetails = decentralizedRequestService.addDetails(decentralizedRequestDTO);

        if (decentralizeDetails != null) {

            return R.ok(decentralizeDetails, "保存成功");
        } else {
            return R.failed("保存失败");
        }


    }

    /**
     * 修改(分散采购申请)
     *
     * @param decentralizedRequestDTO (分散采购申请)
     * @return R
     */
    @ApiOperation(value = "修改分散采购申请明细", notes = "修改分散采购申请明细")
    @SysLog("修改(分散采购申请)")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('reagent_decentralize_details_edit')")
    public R<DecentralizeDetails> putUpdateById(@RequestBody DecentralizedRequestDTO decentralizedRequestDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizeDetails decentralizeDetails = decentralizedRequestService.editRequest(decentralizedRequestDTO);

        if (decentralizeDetails != null) {
            return R.ok(decentralizeDetails, "修改成功");
        } else {
            return R.failed("修改失败");
        }
    }

    /**
     * 通过id删除(分散采购申请)
     *
     * @param decentralizedRequestId id
     * @return R
     */
    @ApiOperation(value = "通过id删除分散采购申请", notes = "通过id删除分散采购申请")
    @SysLog("通过id删除(分散采购申请)")
    @DeleteMapping("/{decentralizedRequestId}")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_del')")
    public R<String> deleteById(@PathVariable String decentralizedRequestId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        DecentralizedRequest oldDecentralizedRequest = decentralizedRequestService.getById(decentralizedRequestId);
        if (decentralizedRequestService.delRequestById(decentralizedRequestId)) {
            return R.ok("删除成功");
        } else return R.failed("删除失败");


    }

    /**
     * 通过id删除分散采购申请明细
     *
     * @param decentralizeDetailsId
     * @return R
     */
    @ApiOperation(value = "通过id删除分散采购申请明细", notes = "通过id删除分散采购申请明细")
    @SysLog("通过id删除分散采购申请明细")
    @DeleteMapping("details/{decentralizeDetailsId}")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralize_details_del')")
    public R<DecentralizeDetails> deleteDetailsById(@PathVariable String decentralizeDetailsId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizeDetails decentralizeDetails = decentralizeDetailsService.getById(decentralizeDetailsId);

        if (decentralizeDetailsService.removeById(decentralizeDetails)) {
            return R.ok(decentralizeDetails, "移除成功");
        } else return R.failed("移除失败");


    }

    /**
     * 提交分散采购申请明细
     *
     * @param decentralizedRequestDTOList
     * @return R
     */
    @ApiOperation(value = "提交分散采购申请", notes = "提交分散采购申请")
    @SysLog("提交分散采购申请明细")
    @PutMapping("/commit")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_commit')")
    public R<DecentralizedRequest> commitById(@RequestBody List<DecentralizedRequestDTO> decentralizedRequestDTOList, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.commitRequest(decentralizedRequestDTOList, dlpUser);

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

    /**
     * 一级审核分散采购申请
     *
     * @param auditAndApproveDto
     * @return R
     */
    @ApiOperation(value = "一级审核分散采购申请", notes = "一级审核分散采购申请")
    @SysLog("审核分散采购申请明细")
    @PutMapping("/primary/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_primary')")
    public R<DecentralizedRequest> primaryAuditRequest(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.primaryAuditRequest(dlpUser, auditAndApproveDto);

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

    /**
     * 二级审核分散采购申请
     *
     * @param auditAndApproveDto
     * @return R
     */
    @ApiOperation(value = "二级审核分散采购申请", notes = "二级审核分散采购申请")
    @SysLog("审核分散采购申请明细")
    @PutMapping("/secondary/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_secondary')")
    public R<DecentralizedRequest> secondaryAuditRequest(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.secondaryAuditRequest(dlpUser, auditAndApproveDto);

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

    /**
     * 三级审核分散采购申请
     *
     * @param auditDecentralizedRequestDTO
     * @return R
     */
    @ApiOperation(value = "三级审核分散采购申请", notes = "三级审核分散采购申请")
    @SysLog("审核分散采购申请明细")
    @PutMapping("/threeLevel/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_threeLevel')")
    public R<DecentralizedRequest> threeLevelAuditRequest(@RequestBody AuditDecentralizedRequestDTO auditDecentralizedRequestDTO, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.threeLevelAuditRequest(dlpUser, auditDecentralizedRequestDTO);

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

    /**
     * 审批分散采购申请
     *
     * @param auditAndApproveDto
     * @return R
     */
    @ApiOperation(value = "审批分散采购申请", notes = "审批分散采购申请")
    @SysLog("审批分散采购申请明细")
    @PutMapping("/approve")
//    @PreAuthorize("@pms.hasPermission('reagent_decentralized_request_approve')")
    public R<DecentralizedRequest> approveRequest(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.approveRequest(dlpUser, auditAndApproveDto);

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