package com.qianhe.zqbf.service.impl;

import com.github.pagehelper.PageInfo;
import com.qianhe.common.annotation.DataScope;
import com.qianhe.common.core.domain.entity.SysRole;
import com.qianhe.common.core.page.TableDataInfo;
import com.qianhe.common.exception.BusinessException;
import com.qianhe.common.utils.SecurityUtils;
import com.qianhe.common.utils.StringUtils;
import com.qianhe.zqbf.domain.*;
import com.qianhe.zqbf.mapper.*;
import com.qianhe.zqbf.service.ZqbfXmzBfxqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qianhe.common.constant.MtConstant;

import java.util.*;
import java.util.stream.Collectors;

import static com.qianhe.common.utils.PageUtils.startPage;

@Service
public class ZqbfXmzBfxqServiceImpl implements ZqbfXmzBfxqService {
    @Autowired
    private ZqbfXmzBfxqMapper zqbfXmzBfxqMapper;
    @Autowired
    private ZqczXmzBfjlMapper zqczXmzBfjlMapper;
    @Autowired
    private ZqczXmzDdzjMapper zqczXmzDdzjMapper;
    @Autowired
    private ZqczXmzBfpzMapper zqczXmzBfpzMapper;
    @Autowired
    private ZqczXmzBffkMapper zqczXmzBffkMapper;
    @Autowired
    private ZqbfXmzBfxqxmMapper zqbfXmzBfxqxmMapper;


    /**
     * 分页查询
     * 项目组权限(查询提报的所有数据)
     * 项目组权限(查询提报的所有数据)
     * 项目组专家(只查询分配给自己的数据)
     * 基层单位(查询本部门以及一下数据)
     */
    @Override
    @DataScope(deptAlias = "dept")
    public List<ZqbfXmzBfxqVo> list(ZqbfXmzBfxqQuery zqbfXmzBfxqQuery) {
        if (SecurityUtils.hasPermi(MtConstant.SGZQ_XMZ_ZJ)) {
            zqbfXmzBfxqQuery.setUser_id(SecurityUtils.getUserId());
        }

        String xmids = zqbfXmzBfxqQuery.getXmids();
        if (StringUtils.isNotBlank(xmids)) {
            List<String> xmidList = Arrays.asList(xmids.split(","));
            zqbfXmzBfxqQuery.setXmidList(xmidList);
        }

        List<ZqbfXmzBfxqVo> dataList = zqbfXmzBfxqMapper.list(zqbfXmzBfxqQuery);
        return dataList;
    }

    /**
     * 查询详情
     */
    @Override
    public ZqbfXmzBfxqVo info(Long id) {
        ZqbfXmzBfxqVo info = zqbfXmzBfxqMapper.info(id);
        return info;
    }

    /**
     * 新增
     */
    @Transactional
    @Override
    public int add(ZqbfXmzBfxq zqbfXmzBfxq, String username) {
        String xmids = zqbfXmzBfxq.getXmids();
        if (StringUtils.isEmpty(xmids)) {
            throw new BusinessException("帮扶项目不能为空");
        }

        // 新增帮扶需求
        zqbfXmzBfxq.setDqzt("1");
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxq.setCjr(username);
        zqbfXmzBfxq.setCjsj(new Date());
        Long xqid = zqbfXmzBfxqMapper.add(zqbfXmzBfxq);

        // 同步需求项目关联表
        zqbfXmzBfxqxmMapper.delete(new Long[]{xqid});
        List<ZqbfXmzBfxqxm> dataList = new ArrayList<>();
        List<String> xmidList = Arrays.asList(xmids.split(","));
        for (String xmid : xmidList) {
            dataList.add(ZqbfXmzBfxqxm.builder().xqid(xqid).xmid(xqid).build());
        }
        zqbfXmzBfxqxmMapper.batchAdd(dataList);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("1");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);
        return 1;
    }

    /**
     * 修改
     */
    @Transactional
    @Override
    public int edit(ZqbfXmzBfxq zqbfXmzBfxq, String username) {
        Long xqid = zqbfXmzBfxq.getXqid();
        String xmids = zqbfXmzBfxq.getXmids();

        if (xqid == null) {
            throw new BusinessException("需求ID不能为空");
        }
        if (StringUtils.isEmpty(xmids)) {
            throw new BusinessException("帮扶项目不能为空");
        }

        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 同步需求项目关联表
        zqbfXmzBfxqxmMapper.delete(new Long[]{xqid});
        List<ZqbfXmzBfxqxm> dataList = new ArrayList<>();
        List<String> xmidList = Arrays.asList(xmids.split(","));
        for (String xmid : xmidList) {
            dataList.add(ZqbfXmzBfxqxm.builder().xqid(xqid).xmid(xqid).build());
        }
        zqbfXmzBfxqxmMapper.batchAdd(dataList);
        return 1;
    }

    /**
     * 删除
     */
    @Transactional
    @Override
    public int delete(Long[] ids) {
        zqbfXmzBfxqMapper.delete(ids);
        zqbfXmzBfxqxmMapper.delete(ids);
        return 1;
    }

    /**
     * 提报需求
     */
    @Transactional
    @Override
    public int sub(ZqbfXmzBfxq zqbfXmzBfxq, String username) {
        Long xqid = zqbfXmzBfxq.getXqid();

        // 查询当前需求
        ZqbfXmzBfxqVo zqbfXmzBfxqVo = info(xqid);
        if (zqbfXmzBfxqVo == null) {
            throw new BusinessException("暂无当前需求");
        }
        if (!Arrays.asList("1", "2").contains(zqbfXmzBfxqVo.getDqzt())) {  // 如果当前需求不是待提交与撤销则不允许提交
            throw new BusinessException("当前环节不允许提交");
        }

        // 修改需求
        zqbfXmzBfxq.setDqzt("3");
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("3");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);
        return 1;
    }

    /**
     * 撤销需求
     */
    @Transactional
    @Override
    public int revoke(ZqbfXmzBfxq zqbfXmzBfxq, String username) {
        Long xqid = zqbfXmzBfxq.getXqid();
        String htms = zqbfXmzBfxq.getHtms();

        // 查询当前需求
        ZqbfXmzBfxqVo zqbfXmzBfxqVo = info(xqid);
        if (zqbfXmzBfxqVo == null) {
            throw new BusinessException("暂无当前需求");
        }
        if (!"3".equals(zqbfXmzBfxqVo.getDqzt())) {  // 如果当前需求不是待提交与撤销则不允许提交
            throw new BusinessException("当前环节不允许提交");
        }
        if (StringUtils.isEmpty(htms)) {
            throw new BusinessException("撤销描述不能为空");
        }

        // 修改需求
        zqbfXmzBfxq.setDqzt("2");
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("2");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjl.setBz(htms);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);
        return 1;
    }

    /**
     * 调度专家
     */
    @Transactional
    @Override
    public int dispatch(List<ZqczXmzDdzj> zqczXmzDdzjList, String username) {
        if (Optional.ofNullable(zqczXmzDdzjList).orElse(Collections.emptyList()).isEmpty()) {
            throw new BusinessException("专家列表不能为空");
        }

        // 删除之前的专家信息
        ZqczXmzDdzj zqczXmzDdzj = zqczXmzDdzjList.get(0);
        Long xqid = zqczXmzDdzj.getXqid();

        ZqbfXmzBfxqVo zqbfXmzBfxqVo = info(xqid);
        if (zqbfXmzBfxqVo == null) {
            throw new BusinessException("暂无当前需求");
        }

        zqczXmzDdzjMapper.delete(xqid);

        // 重新添加专家信息
        zqczXmzDdzjMapper.batchAdd(zqczXmzDdzjList);

        // 修改需求
        ZqbfXmzBfxq zqbfXmzBfxq = new ZqbfXmzBfxq();
        zqbfXmzBfxq.setXqid(xqid);
        zqbfXmzBfxq.setDqzt("4");
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("4");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);
        return 1;
    }

    @Override
    public int dispatchT(ZqbfXmzBfxq zqbfXmzBfxq, String username) {
        Long xqid = zqbfXmzBfxq.getXqid();

        // 查询当前需求
        ZqbfXmzBfxqVo zqbfXmzBfxqVo = info(xqid);
        if (zqbfXmzBfxqVo == null) {
            throw new BusinessException("暂无当前需求");
        }
        if (!Arrays.asList("3", "4").contains(zqbfXmzBfxqVo.getDqzt())) {  // 3=待调度 4=已驳回
            throw new BusinessException("当前环节不允许提交");
        }

        // 修改需求
        zqbfXmzBfxq.setDqzt("5");
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("5");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);
        return 1;
    }

    /**
     * 领导审核
     */
    @Transactional
    @Override
    public int approval(ZqczXmzApproval zqczXmzApproval, String username) {
        Long xqid = zqczXmzApproval.getXqid();
        String dqzt = zqczXmzApproval.getDqzt();
        String htms = zqczXmzApproval.getHtms();


        ZqbfXmzBfxqVo zqbfXmzBfxqVo = info(xqid);
        if (zqbfXmzBfxqVo == null) {
            throw new BusinessException("暂无当前需求");
        }

        if (!Arrays.asList("4", "6").contains(dqzt)) {  // 审核状态
            throw new BusinessException("无效审核：4=已驳回6=已审核(帮扶中)");
        }

        // 修改需求
        ZqbfXmzBfxq zqbfXmzBfxq = new ZqbfXmzBfxq();
        zqbfXmzBfxq.setXqid(xqid);
        zqbfXmzBfxq.setDqzt(dqzt);
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt(dqzt);
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjl.setBz(htms);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);
        return 1;
    }

    /**
     * 专家反馈
     */
    @Transactional
    @Override
    public int feedback(ZqczXmzZjpj zqczXmzZjpj, String username) {
        Long xqid = zqczXmzZjpj.getXqid();  // 需求ID
        String bfms = zqczXmzZjpj.getBfms();  // 帮扶描述
        List<ZqczXmzBfpz> dataList = zqczXmzZjpj.getDataList();  // 帮扶凭证

        ZqbfXmzBfxqVo zqbfXmzBfxqVo = info(xqid);
        if (zqbfXmzBfxqVo == null) {
            throw new BusinessException("暂无当前需求");
        }
        if (Optional.ofNullable(dataList).orElse(Collections.emptyList()).isEmpty()) {
            throw new BusinessException("帮扶凭证不能为空");
        }

        // 修改需求
        ZqbfXmzBfxq zqbfXmzBfxq = new ZqbfXmzBfxq();
        zqbfXmzBfxq.setXqid(xqid);
        zqbfXmzBfxq.setDqzt("7");
        zqbfXmzBfxq.setBfms(bfms);
        zqbfXmzBfxq.setWcsj(new Date());
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("7");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);

        // 保存帮扶凭证
        for (ZqczXmzBfpz zqczXmzBfpz : dataList) {
            zqczXmzBfpz.setXqid(xqid);
            zqczXmzBfpz.setGxr(username);
        }
        zqczXmzBfpzMapper.batchAdd(dataList);
        return 1;
    }

    /**
     * 帮扶反馈
     */
    @Override
    public int support(ZqczXmzBffk zqczXmzBffk, String username) {
        Long xqid = zqczXmzBffk.getXqid();

        // 修改需求
        ZqbfXmzBfxq zqbfXmzBfxq = new ZqbfXmzBfxq();
        zqbfXmzBfxq.setXqid(xqid);
        zqbfXmzBfxq.setDqzt("8");
        zqbfXmzBfxq.setWcsj(new Date());
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("8");
        zqczXmzBfjl.setGxr(username);
        zqczXmzBfjlMapper.add(zqczXmzBfjl);

        // 新增反馈
        List<String> bftsList = zqczXmzBffk.getBftsList();
        bftsList.add(zqczXmzBffk.getBftsOther());
        String bfts = String.join(", ", bftsList);
        zqczXmzBffk.setBfts(bfts);
        zqczXmzBffk.setGxr(username);
        zqczXmzBffkMapper.add(zqczXmzBffk);
        return 1;
    }
}
