package digital.laboratory.platform.reagent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.PurchaseCatalogueDTO;
import digital.laboratory.platform.reagent.dto.PurchaseCatalogueGetDTO;
import digital.laboratory.platform.reagent.entity.CatalogueDetails;
import digital.laboratory.platform.reagent.entity.PurchaseCatalogue;
import digital.laboratory.platform.reagent.service.CatalogueDetailsService;
import digital.laboratory.platform.reagent.service.PurchaseCatalogueService;
import digital.laboratory.platform.reagent.vo.PurchaseCatalogueVO;
import org.springframework.beans.factory.annotation.Autowired;
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 springfox.documentation.swagger2.annotations.EnableSwagger2;

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("/purchase_catalogue")
@Api(value = "purchase_catalogue", tags = "采购目录管理")
public class PurchaseCatalogueController {
    @Autowired
    private final PurchaseCatalogueService purchaseCatalogueService;
    @Autowired
    private final CatalogueDetailsService catalogueDetailsService;

    /**
     * 分页查询
     *
     * @param

     * @return
     */
    @ApiOperation(value = "分页查询已发布的采购目录明细", notes = "分页查询已发布的采购目录明细")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_getPage')")
    public R<PurchaseCatalogueVO> getPurchaseCataloguePage(HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchaseCatalogueVO vOpage = purchaseCatalogueService.getVOpage();

        return R.ok(vOpage);

    }

    /**
     * 分页查询
     *
     * @param page              分页对象

     * @return
     */
    @ApiOperation(value = "分页查询制定编辑的采购目录", notes = "分页查询制定编辑的采购目录")
    @GetMapping("/pages")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_getPage')")
    public R<IPage<PurchaseCatalogueVO>> getPurchaseCatalogueVOPage(Page<PurchaseCatalogue> page, Integer status, HttpServletRequest theHttpServletRequest) {

        IPage<PurchaseCatalogueVO> purchaseCatalogueVOPage = purchaseCatalogueService.getPurchaseCatalogueVOPage(page, Wrappers.<PurchaseCatalogue>query().orderByDesc("create_time"));
        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        if (status!=null){

        IPage<PurchaseCatalogueVO> purchaseCatalogueVOPage1 = purchaseCatalogueService.getPurchaseCatalogueVOPage(page, Wrappers.<PurchaseCatalogue>query().eq("status",status).orderByDesc("create_time"));

        List<PurchaseCatalogueVO> records = purchaseCatalogueVOPage.getRecords();

        return R.ok(purchaseCatalogueVOPage1);}

        return R.ok(purchaseCatalogueVOPage);

    }

    /**
     * 通过id查询(采购目录)
     *
     * @param purchaseCatalogueId id
     * @return R
     */
    @ApiOperation(value = "主页点击一行通过id查询", notes = "主页点击一行通过id查询")
    @GetMapping()
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_get')")
    public R<PurchaseCatalogueVO> getById(String purchaseCatalogueId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchaseCatalogueVO catalogueVo = purchaseCatalogueService.getPurchaseCatalogueVO(purchaseCatalogueId);

        if (catalogueVo != null) {
            return R.ok(catalogueVo);
        } else {
            return R.failed("未查询到该信息");
        }


    }






    /**
     * 新增(采购目录)
     *
     * @param purchaseCatalogueDTOList (采购目录)
     * @return R
     */
    @ApiOperation(value = "保存采购目录", notes = "保存采购目录")
    @SysLog("新增(采购目录)")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_add')")
    public R<PurchaseCatalogue> postAddObject(@RequestBody List<PurchaseCatalogueDTO> purchaseCatalogueDTOList, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.addCatalogue(dlpUser, purchaseCatalogueDTOList);


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

    /**
     * 修改(采购目录)
     *
     * @param purchaseCatalogueDto (采购目录)
     * @return R
     */
    @ApiOperation(value = "修改采购目录明细", notes = "修改采购目录明细")
    @SysLog("修改明细采购目录明细")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('reagent_catalogue_details_edit')")
    public R<CatalogueDetails> putUpdateById(@RequestBody PurchaseCatalogueDTO purchaseCatalogueDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        CatalogueDetails catalogueDetails = purchaseCatalogueService.editCatalogue(purchaseCatalogueDto);

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

    /**
     * 创建(采购目录)
     *
     * @param purchaseCatalogueDto (采购目录)
     * @return R
     */
    @ApiOperation(value = "创建采购目录明细", notes = "创建采购目录明细")
    @SysLog("创建采购目录明细")
    @PostMapping("/details")
//    @PreAuthorize("@pms.hasPermission('reagent_catalogue_details_add')")
    public R<CatalogueDetails> addDetails(@RequestBody PurchaseCatalogueDTO purchaseCatalogueDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        CatalogueDetails catalogueDetails = purchaseCatalogueService.addDetails(purchaseCatalogueDto);

        if (catalogueDetails!=null) {
            return R.ok(catalogueDetails, "创建成功");
        } else {
            return R.failed(catalogueDetails, "创建失败");
        }
    }

    /**
     * 通过id删除(采购目录)
     *
     * @param purchaseCatalogueId id
     * @return R
     */
    @ApiOperation(value = "通过id删除采购目录", notes = "通过id删除采购目录")
    @SysLog("通过id删除采购目录")
    @DeleteMapping("/catalogue")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_del')")
    public R<String > deleteById( String purchaseCatalogueId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        Boolean aBoolean = purchaseCatalogueService.delCatalogue(purchaseCatalogueId);

        if (aBoolean){
            return R.ok("删除采购");
        }else return R.failed("删除失败");
    }


    /**
     * 通过id删除采购目录明细
     *
     * @param catalogueDetailsId
     * @return R
     */
    @ApiOperation(value = "通过id删除采购目录明细", notes = "通过id删除采购目录明细")
    @SysLog("通过id删除采购目录")
    @DeleteMapping("/details")
//    @PreAuthorize("@pms.hasPermission('reagent_catalogue_details_del')")
    public R<CatalogueDetails> deleteDetailsById(String catalogueDetailsId, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        CatalogueDetails byId = catalogueDetailsService.getById(catalogueDetailsId);

        if (catalogueDetailsService.removeById(byId)){
            return R.ok(byId,"删除明细成功");
        }else {
            return R.failed("删除失败");
        }
    }

    /**
     * 提交采购目录
     *
     * @param purchaseCatalogueDTOList
     * @return R
     */
    @ApiOperation(value = "提交采购目录", notes = "提交采购目录")
    @SysLog("提交采购目录")
    @PostMapping("/commit")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_commit')")
    public R<PurchaseCatalogue> commitById(@RequestBody List<PurchaseCatalogueDTO> purchaseCatalogueDTOList , HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.commitById(purchaseCatalogueDTOList,dlpUser);

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

    /**
     * 审核采购目录
     *
     * @param auditAndApproveDto
     * @return R
     */
    @ApiOperation(value = "一级审核采购目录", notes = "一级审核采购目录")
    @SysLog("一级审核采购目录")
    @PutMapping("/primary/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_primary')")
    public R<PurchaseCatalogue> primaryAuditCatalogue(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.primaryAuditCatalogue(auditAndApproveDto,dlpUser);

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

    /**
     * 审核采购目录
     *
     * @param auditAndApproveDto
     * @return R
     */
    @ApiOperation(value = "二级审核采购目录", notes = "二级审核采购目录")
    @SysLog("二级审核采购目录")
    @PutMapping("/secondary/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_secondary')")
    public R<PurchaseCatalogue> secondaryAuditCatalogue(@RequestBody AuditAndApproveDTO auditAndApproveDto, HttpServletRequest theHttpServletRequest) {

        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.secondaryAuditCatalogue(auditAndApproveDto,dlpUser);

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

    /**
     * 发布采购目录
     *
     * @param purchaseCatalogueId
     * @return R
     */
    @ApiOperation(value = "发布采购目录", notes = "发布采购目录")
    @SysLog("发布采购目录")
    @PutMapping("/release/{purchaseCatalogueId}")
//    @PreAuthorize("@pms.hasPermission('reagent_purchase_catalogue_release')")
    public R<PurchaseCatalogue> releaseById(@PathVariable String purchaseCatalogueId, HttpServletRequest theHttpServletRequest) {

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.releaseCatalogue(purchaseCatalogueId);

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


