package com.ruoyi.web.controller.projectV2.common;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.projectV2.*;
import com.ruoyi.system.service.projectV2.IZsyzFjService;
import com.ruoyi.system.service.projectV2.IZsyzLcjlService;
import com.ruoyi.system.service.projectV2.IZsyzProjectDeptService;
import com.ruoyi.system.service.projectV2.IZsyzSbbzbService;
import com.ruoyi.system.service.projectV2.common.IZsyzCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.constant.CommonConstants.*;

/**
 * 公共Controller
 *
 * @author 博曼
 * @date 2023-02-22
 */
@RestController
@RequestMapping("/projectV2/common")
public class ZsyzCommonController extends BaseController {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IZsyzSbbzbService zsyzSbbzbService;

    @Autowired
    private IZsyzProjectDeptService zsyzProjectDeptService;

    @Autowired
    private IZsyzCommonService zsyzCommonService;
    @Autowired
    private IZsyzLcjlService zsyzLcjlService;

    /**
     * 首页统计
     * @return
     */
    @GetMapping("/census")
    public AjaxResult census() {
        return  zsyzCommonService.census();
    }

    /**
     * 首页统计折线图
     * @return
     */
    @GetMapping("/censusXy")
    public AjaxResult censusXy() {
        return  zsyzCommonService.censusXy();
    }


    /**
     * 生成项目编号接口
     * @return
     */
    @GetMapping("/xmbh")
    public AjaxResult xmbh() {
        //获取当前日期
        String nowDate = DateUtils.dateTime();
        String xmbhDate = XMBH + nowDate;
        Integer cacheObject = redisCache.getCacheObject(xmbhDate);
        int number = 100;
        if (cacheObject != null) {
            number = cacheObject + 1;
        }
        StringBuffer stringBuffer = new StringBuffer(ZS);
        stringBuffer.append(nowDate).append(number);
        //生成一个项目编码
        redisCache.setCacheObject(xmbhDate, number);
        return AjaxResult.success(stringBuffer);
    }


    /**
     * 线索指派给承接地的接口
     * todo 需要短信下发
     * @return
     */
    @PostMapping("/xszp")
    @PreAuthorize("@ss.hasPermi('projectV2:common:xszp')")
    public AjaxResult xszp(@RequestBody ZsyzSbbzb zsyzSbbzb) {
        String zpType = zsyzSbbzb.getZpType();
        if (ONE.equals(zpType)){
            ArrayList<SysDept> deptList = zsyzSbbzb.getDeptList();
            if (deptList != null && deptList.size() > 0){
                for (SysDept sysDept : deptList) {
                    ZsyzProjectDept zsyzProjectDept = new ZsyzProjectDept();
                    zsyzProjectDept.setXmId(zsyzSbbzb.getId());
                    zsyzProjectDept.setXmbh(zsyzSbbzb.getXmbh());
                    zsyzProjectDept.setXmmc(zsyzSbbzb.getXmxsmc());
                    zsyzProjectDept.setDeptId(sysDept.getDeptId());
                    zsyzProjectDept.setDeptName(sysDept.getDeptName());
                    //指派给要素部门
                    zsyzProjectDeptService.insertZsyzProjectDept(zsyzProjectDept);
                }
            }
            zsyzSbbzb.setProgress(THR);
            zsyzSbbzbService.updateZsyzSbbzbCjd(zsyzSbbzb);
        }else if (TWO.equals(zpType)){
            //判断该项目是否已经被指派
            ZsyzSbbzb zsyzSbbzbOld = zsyzSbbzbService.selectZsyzSbbzbById(zsyzSbbzb.getId());
            if (zsyzSbbzbOld != null){
                Long cjdId = zsyzSbbzbOld.getCjdId();
                if (cjdId != null){
                    return AjaxResult.error("该线索已被指派,请勿重复指派或等待当前指派承接地放弃承接");
                }
            }
            //指派给承接地
            zsyzSbbzb.setProgress(FOR);
            zsyzSbbzbService.updateZsyzSbbzbCjd(zsyzSbbzb);
        }else if (THR.equals(zpType)){
            //两个都发先去发承接地,承接地发送成功再去发要素部门
            //判断该项目是否已经被指派
            ZsyzSbbzb zsyzSbbzbOld = zsyzSbbzbService.selectZsyzSbbzbById(zsyzSbbzb.getId());
            if (zsyzSbbzbOld != null){
                Long cjdId = zsyzSbbzbOld.getCjdId();
                if (cjdId != null){
                    return AjaxResult.error("该线索已被指派,请勿重复指派或等待当前指派承接地放弃承接");
                }
            }

            ArrayList<SysDept> deptList = zsyzSbbzb.getDeptList();
            if (deptList != null && deptList.size() > 0){
                for (SysDept sysDept : deptList) {
                    ZsyzProjectDept zsyzProjectDept = new ZsyzProjectDept();
                    zsyzProjectDept.setXmId(zsyzSbbzb.getId());
                    zsyzProjectDept.setXmbh(zsyzSbbzb.getXmbh());
                    zsyzProjectDept.setXmmc(zsyzSbbzb.getXmxsmc());
                    zsyzProjectDept.setDeptId(sysDept.getDeptId());
                    zsyzProjectDept.setDeptName(sysDept.getDeptName());
                    //指派给要素部门
                    zsyzProjectDeptService.insertZsyzProjectDept(zsyzProjectDept);
                }
                //插入流程记录表
                SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
                ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
                zsyzLcjl.setXmId(zsyzSbbzb.getId());
                zsyzLcjl.setXmmc(zsyzSbbzb.getXmxsmc());
                zsyzLcjl.setXmbh(zsyzSbbzb.getXmbh());
                zsyzLcjl.setDeptId(dept.getDeptId());
                String deptName = dept.getDeptName();
                zsyzLcjl.setDeptName(deptName);
                zsyzLcjl.setProgress(THR);
                zsyzLcjlService.insertZsyzLcjl(zsyzLcjl);
            }
            zsyzSbbzb.setProgress(FOR);
            zsyzSbbzbService.updateZsyzSbbzbCjd(zsyzSbbzb);
        }
        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzSbbzb.getXmxsmc());
        zsyzLcjl.setXmbh(zsyzSbbzb.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(zsyzSbbzb.getProgress());
        zsyzLcjlService.insertZsyzLcjl(zsyzLcjl);
        return AjaxResult.success();
    }

    /**
     * 线索暂存/终止接口
     * @return
     */
    @PostMapping("/xszc")
    @PreAuthorize("@ss.hasPermi('projectV2:common:xszc')")
    public AjaxResult xszc(@RequestBody ZsyzSbbzb zsyzSbbzb) {
        return  toAjax(zsyzSbbzbService.updateZsyzSbbzbForZc(zsyzSbbzb));
    }



    /**
     * 项目审核/驳回接口
     * 参数是审核建议
     * @return
     */
    @PostMapping("/xssh")
    @PreAuthorize("@ss.hasPermi('projectV2:common:xssh')")
    public AjaxResult xssh(@RequestBody ZsyzShyj zsyzShyj) {
        //所有审核需要往审核意见表插入数据
        return  zsyzCommonService.xssh(zsyzShyj);
    }
}
