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 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;


    /**
     * 分页查询
     * 项目组权限(查询提报的所有数据)
     * 项目组权限(查询提报的所有数据)
     * 项目组专家(只查询分配给自己的数据)
     * 基层单位(查询本部门以及一下数据)
     */
    @Override
    @DataScope(deptAlias = "dept", userAlias = "bfzj")
    public List<ZqbfXmzBfxqVo> list(ZqbfXmzBfxqQuery zqbfXmzBfxqQuery) {
        if (SecurityUtils.hasPermi(MtConstant.SGZQ_XMZ) || SecurityUtils.hasPermi(MtConstant.SGZQ_XMZ_LD)) {
            zqbfXmzBfxqQuery.setDqzt("3");
        }
        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) {
        // 新增帮扶需求
        zqbfXmzBfxq.setDqzt("1");
        zqbfXmzBfxq.setGxr(username);
        zqbfXmzBfxq.setCjr(username);
        zqbfXmzBfxq.setCjsj(new Date());
        Long xqid = zqbfXmzBfxqMapper.add(zqbfXmzBfxq);

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

    /**
     * 修改
     */
    @Override
    public int edit(ZqbfXmzBfxq zqbfXmzBfxq, String username) {
        zqbfXmzBfxq.setGxr(username);
        return zqbfXmzBfxqMapper.edit(zqbfXmzBfxq);
    }

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

    /**
     * 提报需求
     */
    @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");
        edit(zqbfXmzBfxq, username);

        // 新增帮扶记录
        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");
        edit(zqbfXmzBfxq, username);

        // 新增帮扶记录
        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");
        edit(zqbfXmzBfxq, username);

        // 新增帮扶记录
        ZqczXmzBfjl zqczXmzBfjl = new ZqczXmzBfjl();
        zqczXmzBfjl.setXqid(xqid);
        zqczXmzBfjl.setDqzt("4");
        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("5", "6").contains(dqzt)) {  // 审核状态
            throw new BusinessException("无效审核：5=已驳回6=已审核(帮扶中)");
        }

        if ("5".equals(dqzt) && StringUtils.isEmpty(htms)) {
            throw new BusinessException("回退原因不能为空");
        }

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

        // 新增帮扶记录
        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());
        edit(zqbfXmzBfxq, username);

        // 新增帮扶记录
        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());
        edit(zqbfXmzBfxq, username);

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

        // 新增反馈
        zqczXmzBffk.setGxr(username);
        zqczXmzBffkMapper.add(zqczXmzBffk);
        return 1;
    }
}
