package com.ozs.web.controller.pm;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.entity.SysUser;
import com.ozs.common.core.domain.model.LoginUser;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import com.ozs.framework.web.service.TokenService;
import com.ozs.pm.doman.PmDemandHis;
import com.ozs.pm.doman.vo.requestVo.PmBookBuildingReqVo;
import com.ozs.pm.doman.vo.requestVo.PmDemandReqVo;
import com.ozs.pm.doman.vo.responseVo.PmDemandResVo;
import com.ozs.pm.service.IPmDemandService;
import com.ozs.pm.service.PmDemandHisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务下达Controller
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Api(tags = "任务下达")
@RestController
@RequestMapping("/pm/taskRelease")
public class PmTaskReleaseController extends BaseController {
    @Autowired
    private IPmDemandService pmDemandService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private PmDemandHisService pmDemandHisService;
    /**
     * 任务下达查询列表
     */
    @ApiOperation(value = "任务下达查询列表", notes = "参数非必传")
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('pm:taskRelease:list')")
    @Log(title = ModularConstans.taskRelease, businessType = BusinessType.QUERY)
    public AjaxResult list(@RequestBody PmDemandReqVo pmDemandReqVo) {
        pmDemandReqVo.setIsAdmin(SysUser.isAdmin(getUserId()));
        pmDemandReqVo.setDeptId(getDeptId());
        pmDemandReqVo.setUserId(getUserId());
        IPage<PmDemandResVo> page = pmDemandService.selectPmDemandList(pmDemandReqVo, 2);
        return success(page);
    }

    @ApiOperation(value = "下达任务", notes = "必传demandId、taskReleaseTime和上传附件，其他字段不传")
    @PostMapping("/releaseTask")
    @PreAuthorize("@ss.hasPermi('pm:taskRelease:releaseTask')")
    @Log(title = ModularConstans.taskRelease, businessType = BusinessType.UPDATE)
    public AjaxResult releaseTask(@RequestBody PmDemandReqVo pmDemandReqVo, HttpServletRequest request) {
        if(pmDemandReqVo.getDemandId() == null){
            return AjaxResult.error("demandId不能为空");
        }
        if(pmDemandReqVo.getSysFileRefs() == null || pmDemandReqVo.getSysFileRefs().size() == 0){
            return AjaxResult.error("上传附件不能为空");
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        pmDemandReqVo.setUpdateBy(String.valueOf(loginUser.getUserId()));
        return toAjax(pmDemandService.releaseTask(pmDemandReqVo));
    }

    /**
     * 查看详情
     */
    @ApiOperation(value = "查看详情", notes = "必传demandId和详情类型(1项目计划，2需求建档，3任务下达,4中标信息，5合同信息，6建设情况),其他字段不传")
    @PostMapping("/view")
    @PreAuthorize("@ss.hasPermi('pm:taskRelease:view')")
    @Log(title = ModularConstans.taskRelease, businessType = BusinessType.QUERY)
    public AjaxResult view(@RequestBody PmDemandReqVo pmDemandReqVo) {
        if(pmDemandReqVo.getDemandId() == null){
            return AjaxResult.error("demandId不能为空");
        }
        if(StringUtils.isEmpty(pmDemandReqVo.getDetailType())){
            return AjaxResult.error("详情的类型不能为空");
        }
        return success(pmDemandService.selectPmDemandByDemandId(pmDemandReqVo.getDemandId(), pmDemandReqVo.getDetailType()));
    }

    /**
     * 获取回退下拉列表
     */
    @ApiOperation(value = "获取回退下拉列表", notes = "必传demandId")
    @GetMapping("/getReturnList")
    @PreAuthorize("@ss.hasPermi('pm:taskRelease:getReturnList')")
    @Log(title = ModularConstans.taskRelease, businessType = BusinessType.QUERY)
    public AjaxResult getReturnList(@NotEmpty(message = "需求ID不能为空")
                                        @RequestParam(value = "demandId", required = true) Long demandId) {
        List< Map<String,Integer>> list = new ArrayList<>();
        LambdaQueryWrapper<PmDemandHis> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmDemandHis::getDemandId,demandId).orderByAsc(PmDemandHis::getReturnOrderNumber);
        List<PmDemandHis> pmDemandHisList = this.pmDemandHisService.list(lambdaQueryWrapper);
        if(!ObjectUtils.isEmpty(pmDemandHisList)){
            Map<String,Integer> map = new HashMap<>();
            map.put("当前信息",0);
            list.add(map);
            for (PmDemandHis pmDemandHis : pmDemandHisList) {
                Map<String,Integer> map2 = new HashMap<>();
                map2.put("第" + pmDemandHis.getReturnOrderNumber() + "回退",pmDemandHis.getReturnOrderNumber());
                list.add(map2);
            }
        }

        return success(list);
    }

    /**
     * 查看历史详情
     */
    @ApiOperation(value = "查看历史详情", notes = "必传demandId和详情类型(1项目计划，2需求建档，3任务下达,4中标信息，5合同信息，6建设情况),必传returnOrderNumber(回退序号,0当前/1第一次回退/2第二次回退...),其他字段不传")
    @PostMapping("/viewHis")
    @PreAuthorize("@ss.hasPermi('pm:taskRelease:viewHis')")
    @Log(title = ModularConstans.taskRelease, businessType = BusinessType.QUERY)
    public AjaxResult viewHis(@RequestBody PmDemandReqVo pmDemandReqVo) {
        if(pmDemandReqVo.getDemandId() == null){
            return AjaxResult.error("demandId不能为空");
        }
        if(ObjectUtils.isEmpty(pmDemandReqVo.getDetailType())){
            return AjaxResult.error("详情的类型不能为空");
        }
        if(ObjectUtils.isEmpty(pmDemandReqVo.getReturnOrderNumber())){
            return AjaxResult.error("回退序号不能为空");
        }
        if(0 == pmDemandReqVo.getReturnOrderNumber()){
            return view(pmDemandReqVo);
        } else {
            return success(pmDemandHisService.selectPmDemandHisByDemandId(pmDemandReqVo.getDemandId(),pmDemandReqVo.getDetailType(),pmDemandReqVo.getReturnOrderNumber()));
        }

    }
}
