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.PurchasingPlanDTO;
import digital.laboratory.platform.reagent.entity.ProcurementContent;
import digital.laboratory.platform.reagent.entity.PurchasingPlan;
import digital.laboratory.platform.reagent.service.PurchasingPlanService;
import digital.laboratory.platform.reagent.vo.PurchasingPlanVO;
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("/purchasing_plan")
@Api(value = "purchasing_plan", tags = "采购计划管理")
public class PurchasingPlanController {

    @Autowired
    private final PurchasingPlanService purchasingPlanService;

    /**
     * 通过id查询(采购计划)
     *
     * @param purchasingPlanId id
     * @return R
     */
    @ApiOperation(value = "点击一行通过id查询采购计划明细", notes = "点击一行通过id查询采购计划明细")
    @GetMapping("/{purchasingPlanId}")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_get')")
    public R<PurchasingPlan> getById(@PathVariable("purchasingPlanId") String purchasingPlanId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        PurchasingPlanVO purchasingPlanVO = purchasingPlanService.getPurchasingPlanVO(purchasingPlanId);
        return R.ok(purchasingPlanVO);
        //return R.ok(purchasingPlanService.getById(purchasingPlanId));
    }

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_get')")
    public R<IPage<PurchasingPlanVO>> getPurchasingPlanPage(Page<PurchasingPlan> page,  HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        IPage<PurchasingPlanVO> purchasingPlanVOPage = purchasingPlanService.getPurchasingPlanVOPage(page, Wrappers.<PurchasingPlan>query()
                .orderByDesc("create_time"));

        return R.ok(purchasingPlanVOPage);
    }




    /**
     * 新增(采购计划)
     *
     * @param purchasingPlanDTOList (采购计划)
     * @return R
     */
    @ApiOperation(value = "新增(采购计划)", notes = "新增(采购计划) 参数DTO：（detailsOfCentralizedId，quantity = ，）")
    @SysLog("新增(采购计划)")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_add')" )
    public R<PurchasingPlanVO> postAddObject(@RequestBody List<PurchasingPlanDTO> purchasingPlanDTOList, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        PurchasingPlanVO purchasingPlanVO = purchasingPlanService.addById(purchasingPlanDTOList, dlpUser);

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

        }
    }

    /**
     * 修改采购计划明细
     *
     * @param purchasingPlanDTO (采购计划)
     * @return R
     */
    @ApiOperation(value = "修改采购计划明细", notes = "修改采购计划明细")
    @SysLog("修改(采购计划)")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('reagent_procurement_content_edit')")
    public R<ProcurementContent> putUpdateById(@RequestBody PurchasingPlanDTO purchasingPlanDTO, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        ProcurementContent procurementContent = purchasingPlanService.editById(purchasingPlanDTO);

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

    /**
     * 修改采购计划合并明细
     *
     * @param purchasingPlanDTO (采购计划DTO)
     * @return R
     */
    @ApiOperation(value = "修改采购计划合并明细", notes = "修改采购计划合并明细")
    @SysLog("修改(采购计划)")
    @PutMapping("/details")
//    @PreAuthorize("@pms.hasPermission('reagent_procurement_content_edit')")
    public R<ProcurementContent> editDetails(@RequestBody PurchasingPlanDTO purchasingPlanDTO, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        ProcurementContent procurementContent = purchasingPlanService.editDetails(purchasingPlanDTO);

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

    /**
     * 通过id删除采购计划明细
     *
     * @param procurementContentId id
     * @return R
     */
    @ApiOperation(value = "通过id删除采购计划明细", notes = "通过id删除采购计划明细")
    @SysLog("通过id删除采购计划明细")
    @DeleteMapping("content/{procurementContentId}")
//    @PreAuthorize("@pms.hasPermission('reagent_procurement_content_del')")
    public R<String> deleteContentById(@PathVariable String procurementContentId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        if (purchasingPlanService.delContentById(procurementContentId)) {
            return R.ok("移除成功");
        } else {
            return R.failed("移除失败");
        }

    }

    /**
     * 提交采购计划
     *
     * @param purchasingPlanId (采购计划)
     * @return R
     */
    @ApiOperation(value = "提交采购计划", notes = "提交采购计划")
    @SysLog("修改(采购计划)")
    @PutMapping("/commit")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_edit')")
        public R<PurchasingPlan> commitById( String purchasingPlanId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchasingPlan purchasingPlan = purchasingPlanService.commitById(purchasingPlanId);

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

    /**
     * 审核采购计划
     *
     * @param purchasingPlanDTO (采购计划)
     * @return R
     */
    @ApiOperation(value = "换货", notes = "换货")
    @SysLog("(换货)")
    @PutMapping("/replacement")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_edit')")
    public R<ProcurementContent> replacementById(@RequestBody PurchasingPlanDTO purchasingPlanDTO, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        ProcurementContent procurementContent = purchasingPlanService.replacementById(purchasingPlanDTO);

        if (procurementContent != null) {
            return R.ok(procurementContent,"换货成功");
        } else {
            return R.failed("换货成功");
        }
    }

    /**
     * 审核采购计划
     *
     * @param auditAndApproveDto (采购计划)
     * @return R
     */
    @ApiOperation(value = "审核采购计划", notes = "审核采购计划")
    @SysLog("修改(采购计划)")
    @PutMapping("/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_edit')")
    public R<PurchasingPlan> auditById(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchasingPlan purchasingPlan = purchasingPlanService.auditById(auditAndApproveDto,dlpUser);

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

    /**
     * 审批采购计划
     *
     * @param auditAndApproveDto (采购计划)
     * @return R
     */
    @ApiOperation(value = "审批采购计划", notes = "审批采购计划")
    @SysLog("修改(采购计划)")
    @PutMapping("/approve")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_edit')")
    public R<PurchasingPlan> approveById(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchasingPlan purchasingPlan = purchasingPlanService.approveById(auditAndApproveDto,dlpUser);

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

    /**
     * 发布采购计划
     *
     * @param purchasingPlanId (采购计划)
     * @return R
     */
    @ApiOperation(value = "发布采购计划", notes = "发布采购计划")
    @SysLog("修改(采购计划)")
    @PutMapping("/release")
//    @PreAuthorize("@pms.hasPermission('reagent_purchasing_plan_edit')")
    public R<PurchasingPlan> releaseById(@PathVariable String  purchasingPlanId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchasingPlan purchasingPlan = purchasingPlanService.releaseById(purchasingPlanId);

        if (purchasingPlan != null) {
            return R.ok(purchasingPlan,"发布成功");
        } else {
            return R.failed("发布失败");
        }
    }


}
