package com.dayou.controller;

import com.dayou.annotation.DataPermission;
import com.dayou.dto.MajorStaticsDTO;
import com.dayou.dto.PriceFeedbackDTO;
import com.dayou.dto.TaskRecordDTO;
import com.dayou.dto.WorkNodeCommit;
import com.dayou.vo.MajorVO;
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.IMajorService;
import com.dayou.entity.Major;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.validation.Valid;

import static com.dayou.common.Constants.MANAGER_OFFICE;

/**
 * 大中型项目
 *
 * @author wucl
 * @since 2023-10-30
 */
@RestController
@RequestMapping("major")
@Slf4j
public class MajorController extends BaseController {
    @Autowired
    private IMajorService majorService;

    /**
    * 大中型项目列表
    */
    @GetMapping("")
    public RestResponse<Page<MajorVO>> page(MajorVO major, Page page,String keyword){
        Page<MajorVO> pages=majorService.selectPage(page,major,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 条件查询大中型已终止流程
     */
    @GetMapping("/getTerminatedPage")
    public RestResponse<Page<MajorVO>> getTerminatedPage(MajorVO major, Page page,String keyword){
        Page<MajorVO> pages=majorService.getTerminatedPage(page,major,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 大中型项目列表(平铺)
     */
    @DataPermission(ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("/page")
    public RestResponse<Page<MajorVO>> xPage(MajorVO major, Page page,String keyword){
        Page<MajorVO> pages=majorService.xPage(page,major,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 大中型项目详情
     */
    @GetMapping("/{id}")
    public RestResponse<MajorVO> detail(@PathVariable Long id) throws InterruptedException {
        MajorVO xMajor =majorService.detail(id);
        return RestResponse.data(xMajor);
     }

    /**
     * 大中型项目新增
     */
    @PostMapping("")
    public synchronized RestResponse<WorkNodeCommit> save(@RequestBody Major major) {
        WorkNodeCommit ret = majorService.add(major);
        return RestResponse.data(ret);
    }

    /**
     * 大中型项目更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody Major major) {
        Boolean ret = majorService.update(major);
        return RestResponse.data(ret);
    }

    /**
     * 大中型项目删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = majorService.delete(id);
        return RestResponse.data(ret);
    }


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

    /**
     * 业务分单
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/allocation")
    public RestResponse<Boolean> allocation(@RequestBody @Valid TaskRecordDTO<MajorVO> taskRecordDTO){
        Boolean ret = majorService.allocation(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 客户反馈
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/priceFeedback")
    public RestResponse<Boolean> priceFeedback(@RequestBody @Valid TaskRecordDTO<PriceFeedbackDTO> taskRecordDTO){
        Boolean ret = majorService.priceFeedback(taskRecordDTO);
        return RestResponse.data(ret);
    }

    /**
     * 大中型首页统计信息
     * @return
     */
    @GetMapping("/statics")
    public RestResponse<MajorStaticsDTO> staticsInformation(){
        MajorStaticsDTO ret = majorService.staticsInformation();
        return RestResponse.data(ret);
    }

    /**
     * 大中型流程撤回
     */
    @GetMapping("/recall/{id}")
    public RestResponse<Boolean> doRecall(@PathVariable Long id) {
        Boolean ret =majorService.doRecall(id);
        return RestResponse.data(ret);
    }

    /**
     * 大中型产品详情
     */
    @GetMapping("/{id}/{productionNo}/{production}")
    public RestResponse<MajorVO> productionDetail(@PathVariable Long id,@PathVariable("productionNo") String productionNo,@PathVariable("production") String production) throws InterruptedException {
        MajorVO xMajor =majorService.productionDetail(id,productionNo,production);
        return RestResponse.data(xMajor);
    }

    /**
     * 评估部经理修改客户反馈
     * @param priceFeedbackDTO
     * @return
     */
    @PostMapping("/feedback")
    public RestResponse<Boolean> feedback(@RequestBody PriceFeedbackDTO priceFeedbackDTO){
        Boolean ret = majorService.feedback(priceFeedbackDTO);
        return RestResponse.data(ret);
    }

    /**
     * 更新接单部门
     * @param taskRecordDTO
     * @return
     */
    @FinishTask
    @PostMapping("/department")
    public RestResponse<Boolean> updateDepartment(@RequestBody @Valid TaskRecordDTO<Major> taskRecordDTO){
        Boolean ret = majorService.updateDepartment(taskRecordDTO);
        return RestResponse.data(ret);
    }
}

