package com.casic.missiles.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.labExecutive.*;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessLabExecutiveInfoService;
import com.casic.missiles.utils.DictCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 业务管理-实验室检定信息记录表（用于流程中的实验室分配及后续检定过程查询） 前端控制器
 * 样品状态（任务收发&任务分发使用，委托书和样品关联表中的状态）：
 * 1:待收入;2:待分发;3:检测中;4:检测完成;5:待归还;6:已归还;7:无需检测;8:已超期
 * 实验室检定状态（实验室检测使用，实验室检定信息记录表中的状态）：
 * 1:待分配（负责人）;2:待检测（检定人）;3:检测完成;4:退回;5:终止（已收入中点击终止更新实验室中待分配和待检测的的状态为已终止）
 * </p>
 *
 * @author wangpeng
 * @since 2023-02-02
 */
@Api(tags = "业务管理模块-任务分发&我的检测&部门检测相关接口")
@Slf4j
@RestController
@RequestMapping("/business/labExecutive")
public class BusinessLabExecutiveInfoController extends ExportController {
    @Autowired
    private IBusinessLabExecutiveInfoService labExecutiveInfoService;

    /**
     * 任务分发-查询各状态列表（待分发、检测中、检测完、已超期）
     */
    @ApiOperation("任务分发-查询各状态列表")
    @PostMapping("/handOut/list")
    @ResponseBody
    public ReturnDTO<PageInfoBT<SampleHandOutListResponse>> queryHandOutList(@RequestBody @Valid SampleHandOutListRequest handOutListRequest, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(handOutListRequest);
        Page<SampleHandOutListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(labExecutiveInfoService.handOutListPage(page, handOutListRequest)));
    }

    /**
     * 任务分发-查询各状态样品数量（待分发、检测中、检测完、已超期）
     */
    @ApiOperation("任务分发-查询各状态样品数量")
    @GetMapping("/handOut/amount")
    @ResponseBody
    public ReturnDTO<List<SampleAmountResponse>> queryHandOutAmount() {
        return ReturnUtil.success(labExecutiveInfoService.handOutAmount());
    }

    /**
     * 任务分发-详情
     */
    @ApiOperation("任务分发-详情")
    @PostMapping("/handOut/detail")
    @ResponseBody
    public ReturnDTO<SampleHandOutDetailResponse> queryHandOutDetail(@RequestBody @Valid SampleHandOutDetailRequest handOutDetailRequest, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(labExecutiveInfoService.handOutListDetail(handOutDetailRequest));
    }

    /**
     * 任务分发-标签绑定（复用设备收发中的标签绑定）
     */

    /**
     * 任务分发-获取初始化检定流程（实验室列表）
     */
    @ApiOperation("任务分发-获取初始化检定流程")
    @PostMapping("/handOut/initList")
    @ResponseBody
    public ReturnDTO<List<SampleHandOutInitListResponse>> queryInitList(@RequestBody @Valid SampleHandOutInitListRequest handOutInitListRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(labExecutiveInfoService.handOutInitList(handOutInitListRequest));
    }

    /**
     * 任务分发-分发
     */
    @ApiOperation("任务分发-分发")
    @PostMapping("/handOut/execute")
    @ResponseBody
    public ReturnDTO handOut(@RequestBody @Valid SampleHandOutRequest handOutRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.handOut(handOutRequest);
    }

    /**
     * 实验室检测-部门检测-查询各状态列表（待检测、检测中、检测完）
     */
    @ApiOperation("实验室检测-部门检测-查询各状态列表（待检测、检测中、检测完）")
    @PostMapping("/deptExecutive/List")
    @ResponseBody
    public ReturnDTO<PageInfoBT<SampleLabExecutiveListResponse>> queryDeptExecutiveList(@RequestBody @Valid SampleLabExecutiveListRequest labExecutiveListRequest, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(labExecutiveListRequest);
        Page<SampleLabExecutiveListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(labExecutiveInfoService.deptLabExecutiveListPage(page, labExecutiveListRequest)));
    }

    /**
     * 实验室检测-我的检测-查询各状态列表（待检测、检测中、检测完）
     */
    @ApiOperation("实验室检测-我的检测-查询各状态列表（待检测、检测中、检测完）")
    @PostMapping("/myExecutive/list")
    @ResponseBody
    public ReturnDTO<PageInfoBT<SampleLabExecutiveListResponse>> queryMyExecutiveList(@RequestBody @Valid SampleLabExecutiveListRequest labExecutiveListRequest, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        DictCodeUtils.validDictCode(labExecutiveListRequest);
        Page<SampleLabExecutiveListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(labExecutiveInfoService.myLabExecutiveListPage(page, labExecutiveListRequest)));
    }

    /**
     * 实验室检测-我的检测-查询各状态样品数量（待检测、检测中、检测完）
     */
    @ApiOperation("实验室检测-我的检测-查询各状态样品数量（待检测、检测中、检测完）")
    @GetMapping("/myExecutive/amount")
    @ResponseBody
    public ReturnDTO<List<SampleAmountResponse>> queryMyExecutiveAmount() {
        return ReturnUtil.success(labExecutiveInfoService.myExecutiveAmount());
    }

    /**
     * 实验室检测-部门检测-分配（分配给具体人员）
     */
    @ApiOperation("实验室检测-部门检测-分配按钮（分配给具体人员）")
    @PostMapping("/deptExecutive/allocate")
    @ResponseBody
    public ReturnDTO allocateStaff(@RequestBody @Valid SampleAllocateRequest sampleAllocateRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.allocateStaff(sampleAllocateRequest);
    }

    /**
     * 实验室检测-我的检测-检定人扫描收入（待检测->检测中）
     */
    @ApiOperation("实验室检测-我的检测-检定人扫描收入（待检测->检测中）")
    @PostMapping("/myExecutive/receive")
    @ResponseBody
    public ReturnDTO sampleReceive(@RequestBody @Valid List<SampleOperateRequest> requestList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.sampleReceive(requestList);
    }

    /**
     * 实验室检测-负责人/检定人扫描检完（检测中->检测完）
     */
    @ApiOperation("实验室检测-负责人/检定人扫描检完（检测中->检测完）")
    @PostMapping("/myExecutive/complete")
    @ResponseBody
    public ReturnDTO sampleExecutiveComplete(@RequestBody @Valid List<SampleOperateRequest> requestList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.sampleExecutiveComplete(requestList);
    }

    /**
     * 实验室检测-部门检测/我的检测 编辑（只能修改应出具证书）
     */
    @ApiOperation("实验室检测-部门检测/我的检测 编辑（只能修改应出具证书）")
    @PostMapping("/executive/update")
    @ResponseBody
    public ReturnDTO updateExecutive(@RequestBody @Valid UpdateExecutiveRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.updateExecutive(request);
    }

    /**
     * 实验室检测-部门检测/我的检测 详情
     */
    @ApiOperation("实验室检测-部门检测/我的检测 详情")
    @PostMapping("/executive/detail")
    @ResponseBody
    public ReturnDTO<ExecutiveDetailResponse> executiveDetail(@RequestBody @Valid ExecutiveDetailRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(labExecutiveInfoService.executiveDetail(request));
    }

    /**
     * 实验室检测-部门检测/我的检测 退回（待检测/检测中->退回）
     * 退回后会将下一个要执行的实验室检测状态更新为待分配，将后面的实验室顺序变更为连续（都进行减1）
     * 最后一个检测的实验室退回，等分发员再增加实验室或设备收发中的已收入直接手动点击完成
     */
    @ApiOperation("实验室检测-部门检测/我的检测 退回（待检测/检测中->退回）")
    @PostMapping("/executive/sendBack")
    @ResponseBody
    public ReturnDTO executiveSendBack(@RequestBody @Valid ExecutiveSendBackRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.sampleExecutiveSendBack(request);
    }

    /**
     * 分发员编辑实验室检测列表
     */
    @ApiOperation("分发员编辑实验室检测列表")
    @PostMapping("/handOut/updateList")
    @ResponseBody
    public ReturnDTO updateExecutiveList(@RequestBody @Valid UpdateExecutiveListRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return labExecutiveInfoService.updateExecutiveList(request);
    }

    /**
     * 各状态列表导出
     */
    @ApiOperation("任务分发-各状态列表导出")
    @PostMapping("/handOut/listExport")
    @ResponseBody
    public void export(@RequestBody @Valid SampleHandOutListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<SampleHandOutListResponse> page = PageFactory.defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);
        Page<SampleHandOutListResponse> responsePage = labExecutiveInfoService.handOutListPage(page, request);
        List<SampleHandOutListResponse> list = responsePage.getRecords();
        super.exportExcel(SampleHandOutListResponse.class, list, ExportEnum.HANDOUT_EXPORT.getSheetName());
    }

}
