package cn.iocoder.yudao.module.museum.controller.admin.museumflow;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.museum.controller.admin.museumflow.vo.MuseumFlowPageVO;
import cn.iocoder.yudao.module.museum.controller.admin.museumflow.vo.MuseumFlowRespVO;
import cn.iocoder.yudao.module.museum.controller.admin.museumflow.vo.MuseumFlowSaveVO;
import cn.iocoder.yudao.module.museum.dal.database.museumflow.MuseumFlowDO;
import cn.iocoder.yudao.module.museum.dal.database.museuminfo.MuseumInfoDO;
import cn.iocoder.yudao.module.museum.service.museumflow.MuseumFlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Tag(name = "博物馆-出回库管理")
@RestController
@RequestMapping("/museum/flow")
public class MuseumFlowController {

    @Resource
    private MuseumFlowService museumFlowService;

    @PostMapping("/create")
    @Operation(summary = "新增表单")
    public Integer createMuseumFlow(MuseumFlowSaveVO saveVO) {
        return museumFlowService.createMuseumFlow(saveVO);
    }

    @PutMapping("/update")
    @Operation(summary = "更新表单")
    public CommonResult<Boolean> updateMuseumFlow(@RequestBody MuseumFlowSaveVO saveVO){
        museumFlowService.updateMuseumFlow(saveVO);
        return CommonResult.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除表单")
    public CommonResult<Boolean> deleteMuseumFlow(@RequestParam("id") Integer id){
        museumFlowService.deleteMuseumFlow(id);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "查找表单")
    public CommonResult<MuseumFlowRespVO> getMuseumFlow(@RequestParam("id") Integer id){
        MuseumFlowDO result = museumFlowService.selectMuseumFlow(id);
        MuseumFlowRespVO result1 = BeanUtils.toBean(result,MuseumFlowRespVO.class);
        return CommonResult.success(result1);
    }

    @PostMapping("/Outbound")
    @Operation(summary = "添加出库单")
    public CommonResult<Boolean>  createMuseumFlowState(@RequestBody MuseumFlowSaveVO saveVO){
        String sample_id=saveVO.getNumber();
        MuseumInfoDO result = museumFlowService.selectMuseumFlowSampleId(sample_id);
        if(result == null){
            return CommonResult.error(200,"样品不存在");
        }
        int Save=result.getSave();
        //      0现为已出库，1为未出库或已回库，2为审批中，3为审批通过
        if(Save == 0){
            return CommonResult.error(200,"样品不在库中");
        }
        else if (Save == 2){
            return CommonResult.error(200,"样品正在审批");
        }
        else {
            museumFlowService.createMuseumFlow(saveVO);
            museumFlowService.updateMuseumFlowState(saveVO);
            return CommonResult.success(true);
        }
    }

    @GetMapping("/page")
    @Operation(summary = "获取分页")
    public PageResult<MuseumFlowDO> selectPageS(@Valid MuseumFlowPageVO pageVO){
        return  museumFlowService.selectPageS(pageVO);
    }

    @PostMapping("/review")
    @Operation(summary = "新增通过的审批单")
    public CommonResult<Boolean> createMuseumAFlowReview(@RequestBody MuseumFlowSaveVO saveVO) {
        String sample_id = saveVO.getNumber();
        MuseumInfoDO result = museumFlowService.selectMuseumFlowSampleId(sample_id);
        if (result == null) {
            return CommonResult.error(200, "样品不存在");
        }
        int Save = result.getSave();
        //      0现为已出库，1为未出库或已回库，2为审批中，3为审批通过，4为审批驳回
        if (Save == 4) {
            return CommonResult.error(200, "样品审批已被驳回");
        }
        else if (Save == 3) {
            return CommonResult.error(200, "样品已审批通过");
        }
        else if (Save == 1) {
            return CommonResult.error(200, "样品并未申请出库");
        }
        else if (Save == 2) {
            museumFlowService.createMuseumFlow(saveVO);
            museumFlowService.updateMuseumFlowState3(saveVO);
            return CommonResult.success(true);
        }
        else {
            return CommonResult.error(200,"样品已审批通过并出库");
        }
    }

    @PostMapping("/reject")
    @Operation(summary = "新增拒绝的审批单")
    public CommonResult<Boolean> createMuseumAFlowReject(@RequestBody MuseumFlowSaveVO saveVO) {
        String sample_id = saveVO.getNumber();
        MuseumInfoDO result = museumFlowService.selectMuseumFlowSampleId(sample_id);
        if (result == null) {
            return CommonResult.error(200, "样品不存在");
        }
        int Save = result.getSave();
        //      0现为已出库，1为未出库或已回库，2为审批中，3为审批通过，4为审批驳回
        if (Save == 4) {
            return CommonResult.error(200, "样品审批已被驳回");
        }
        else if (Save == 3) {
            return CommonResult.error(200, "样品已审批通过");
        }
        else if (Save == 1) {
            return CommonResult.error(200, "样品并未申请出库");
        }
        else if (Save == 2) {
            museumFlowService.createMuseumFlow(saveVO);
            museumFlowService.updateMuseumFlowState4(saveVO);
            return CommonResult.success(true);
        }
        else {
            return CommonResult.error(200,"样品已审批通过并出库");
        }
    }
}