package com.dayou.controller;

import com.dayou.annotation.DataPermission;
import com.dayou.dto.CommonCheckDTO;
import com.dayou.dto.TaskRecordDTO;
import com.dayou.dto.WareHouseDTO;
import com.dayou.entity.BusinessFile;
import com.dayou.vo.*;
import com.dayou.workflow.annotation.FinishTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IMajorProductionService;
import com.dayou.entity.MajorProduction;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;

import java.io.IOException;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 大中型产品
 *
 * @author wucl
 * @since 2023-11-06
 */
@RestController
@RequestMapping("majorProduction")
@Slf4j
public class MajorProductionController extends BaseController {
    @Autowired
    private IMajorProductionService majorProductionService;

    /**
    * 大中型产品列表
    */
    @GetMapping("")
    public RestResponse<List<MajorProduction>> getList(MajorProduction majorProduction){
        List<MajorProduction> list = majorProductionService.selectPage(majorProduction);
        return RestResponse.data(list);
    }

    /**
     * 大中型产品列表(正在进行)
     */
    @GetMapping("/pending")
    public RestResponse<List<MajorProduction>> getPendingList(MajorProduction majorProduction){
        List<MajorProduction> list = majorProductionService.getPendingList(majorProduction);
        return RestResponse.data(list);
    }


    /**
     * 产品仓库列表
     */
    @GetMapping("/house")
    public RestResponse<Page<MajorProductionVO>> getHouse(Page page ,MajorProductionVO majorProduction,String keyword){
        Page<MajorProductionVO> ret = majorProductionService.getHouse(page,majorProduction,keyword);
        return RestResponse.data(ret);
    }

    /**
     * 我的订单列表
     */
    @GetMapping("/myOrder")
    public RestResponse<Page<MajorOrderVO>> myOrder(Page page , MajorOrderVO myOrder,String keyword){
        Page<MajorOrderVO> ret = majorProductionService.myOrder(page,myOrder,keyword);
        return RestResponse.data(ret);
    }

    /**
     * 导出我的订单
     */
    @GetMapping("/exportMyOrder")
    public void exportMyOrder(MajorOrderVO myOrder,String keyword, HttpServletResponse response ) throws IOException {
        exportPlus(response,"大中型我的订单",majorProductionService.exportMyOrder(myOrder,keyword), MajorOrderVO.class);
    }


    /**
     * 我的认领实收款订单列表
     */
    @GetMapping("/waiting/claim")
    public RestResponse<Page<MajorOrderVO>> waitingClaim(Page page , MajorOrderVO myOrder){
        Page<MajorOrderVO> ret = majorProductionService.waitingClaim(page,myOrder);
        return RestResponse.data(ret);
    }

    /**
     * 我的认领实收款产品列表
     */
    @GetMapping("/waiting/claim/prod")
    public RestResponse<Page<MajorProductionVO>> waitingClaimProd(Page page , MajorProductionVO production){
        Page<MajorProductionVO> ret = majorProductionService.waitingClaimProd(page,production);
        return RestResponse.data(ret);
    }

    /**
     * 大中型产品详情
     */
    @GetMapping("/{id}")
    public RestResponse<MajorProduction> detail(@PathVariable Long id){
        MajorProduction xMajorProduction =majorProductionService.detail(id);
        return RestResponse.data(xMajorProduction);
     }

    /**
     * 大中型产品新增
     */

    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody MajorProduction majorProduction) {
        Boolean ret = majorProductionService.add(majorProduction);
        return RestResponse.data(ret);
    }

    /**
     * 大中型产品更新
     */
    @FinishTask
    @PutMapping("")
    public RestResponse<Boolean> finishAllProductionInfo(@RequestBody @Valid TaskRecordDTO<MajorProduction> majorProduction) {
        Boolean ret = majorProductionService.finishAllProductionInfo(majorProduction);
        return RestResponse.data(ret);
    }

    /**
     * 大中型产品更新
     */
    @PutMapping("/update")
    public RestResponse<Boolean> update(@RequestBody MajorProduction majorProduction) {
        Boolean ret = majorProductionService.update(majorProduction);
        return RestResponse.data(ret);
    }
    /**
     * 大中型产品删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = majorProductionService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 大中型产品下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<MajorProduction> lambdaQueryWrapper = new LambdaQueryWrapper<MajorProduction>()
        .select(MajorProduction::getId,MajorProduction::getName)
        .eq(MajorProduction::getDeleted,false);
        List<MajorProduction> list= majorProductionService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 上传产品文档
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/upload")
    public RestResponse<Boolean> uploadProductionAttachment(@RequestBody @Valid TaskRecordDTO<BusinessFile> taskRecordDTO){
        Boolean ret = majorProductionService.uploadProductionAttachment(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 产品入库
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/in")
    public RestResponse<Boolean> repertoryIn(@RequestBody @Valid TaskRecordDTO<Long> taskRecordDTO){
        Boolean ret = majorProductionService.repertoryIn(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 产品出库
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/out")
    public RestResponse<Boolean> repertoryOut(@RequestBody @Valid TaskRecordDTO<Long> taskRecordDTO){
        Boolean ret = majorProductionService.repertoryOut(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 产品出入库（报告号）
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/warehouse")
    public RestResponse<Boolean> warehouse(@RequestBody @Valid TaskRecordDTO<WareHouseDTO> taskRecordDTO){
        Boolean ret = majorProductionService.warehouse(taskRecordDTO);
        return RestResponse.data(ret);
    }
    /**
     * 产品初审
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/first/check")
    public RestResponse<Boolean> firstCheck(@RequestBody @Valid TaskRecordDTO<CommonCheckDTO> taskRecordDTO){
        Boolean ret = majorProductionService.firstCheck(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 产品复审
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/second/check")
    public RestResponse<Boolean> secondCheck(@RequestBody @Valid TaskRecordDTO<CommonCheckDTO> taskRecordDTO){
        Boolean ret = majorProductionService.secondCheck(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 产品三审
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/third/check")
    public RestResponse<Boolean> thirdCheck(@RequestBody @Valid TaskRecordDTO<CommonCheckDTO> taskRecordDTO){
        Boolean ret = majorProductionService.thirdCheck(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 归档审核
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/saveFile/check")
    public RestResponse<Boolean> saveFileCheck(@RequestBody @Valid TaskRecordDTO<CommonCheckDTO> taskRecordDTO){
        Boolean ret = majorProductionService.saveFileCheck(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 复审部归档审核
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/saveFile/second/check")
    public RestResponse<Boolean> saveFileSecondCheck(@RequestBody @Valid TaskRecordDTO<CommonCheckDTO> taskRecordDTO){
        Boolean ret = majorProductionService.saveFileSecondCheck(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 产品归档
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/saveFile")
    public RestResponse<Boolean> saveFile(@RequestBody @Valid TaskRecordDTO<Long> taskRecordDTO){
        Boolean ret = majorProductionService.saveFile(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 已归档列表
     * @param page
     * @param productionDone
     * @return
     */
    @GetMapping("/saveDone")
    public RestResponse<Page<MajorProductionSaveDoneVO>> saveDonePage(Page page,MajorProductionSaveDoneVO productionDone,String keyword){
        Page<MajorProductionSaveDoneVO> done = majorProductionService.saveDonePage(page,productionDone,keyword);
        return RestResponse.data(done);
    }

    /**
     * 大中型 价值意见书确认送达
     */
    @FinishTask
    @PostMapping("/confirmDelivery")
    public RestResponse<Boolean> confirmDelivery(@RequestBody @Valid TaskRecordDTO<Long> taskRecordDTO) {
        Boolean ret = majorProductionService.confirmDelivery(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 大中型 报告或复评函确认送达
     */
    @GetMapping("/confirmDelivery/{id}")
    public RestResponse<Boolean> noTaskConfirmDelivery(@PathVariable("id") Long id) {
        Boolean ret = majorProductionService.noTaskConfirmDelivery(id);
        return RestResponse.data(ret);
    }

    /**
     * 待归档列表
     * @param page
     * @param todoVO
     * @return
     */
    @GetMapping("/todoSaveFile")
    public RestResponse<Page<TaskTodoVO>> todoSaveFilePage(Page page,TaskTodoVO todoVO,String keyword){
        Page<TaskTodoVO> ret =  majorProductionService.todoSaveFilePage(page,todoVO,keyword);
        return RestResponse.data(ret);
    }

    /**
     * 批量归档
     * @param todoVOList
     * @return
     */
    @PostMapping("/batchSaveFile")
    public RestResponse<Boolean> batchSaveFile(@RequestBody  List<TaskTodoVO> todoVOList){
        Boolean ret = majorProductionService.batchSaveFile(todoVOList);
        return RestResponse.data(ret);
    }

    /**
     * 大中型产品绩效列表
     * @param page
     * @param productionPerformance
     * @return
     */
    @DataPermission(department = true, postChild = false)
    @GetMapping("/performance/allot")
    public RestResponse<Page<MajorProductionPerformanceVO>> performanceAllotPage(MajorProductionPerformanceVO productionPerformance,Page page,String keyword){
        Page<MajorProductionPerformanceVO> ret = majorProductionService.performanceAllotPage(page,productionPerformance,keyword);
        return RestResponse.data(ret);
    }
}

