package com.ruoyi.system.service.impl.project;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.BmProject;
import com.ruoyi.system.domain.project.BmProjectStatus;
import com.ruoyi.system.domain.vo.BmDeptPlanVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.project.BmProjectMapper;
import com.ruoyi.system.service.project.IBmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.constant.Constants.EXAMINE_BACK;
import static com.ruoyi.common.constant.Constants.EXAMINE_PASS;


/**
 * 项目(添加线索)Service业务层处理
 *
 * @author bm
 * @date 2021-03-03
 */
@Service
public class BmProjectServiceImpl implements IBmProjectService {
    @Autowired
    private BmProjectMapper bmProjectMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询项目(添加线索)
     *
     * @param id 项目(添加线索)ID
     * @return 项目(添加线索)
     */
    @Override
    public BmProject selectBmProjectById(Long id) {
        return bmProjectMapper.selectBmProjectById(id);
    }

    /**
     * 查询项目(添加线索)列表
     *
     * @param bmProject 项目(添加线索)
     * @return 项目(添加线索)
     */
    @Override
    public List<BmProject> selectBmProjectList(BmProject bmProject) {
        return bmProjectMapper.selectBmProjectList(bmProject);
    }

    /**
     * 新增项目(添加线索)
     *
     * @param bmProject 项目(添加线索)
     * @return 结果
     */
    @Override
    public AjaxResult insertBmProject(BmProject bmProject) {
        bmProject.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long deptId = user.getDeptId();
        bmProject.setDeptId(deptId);
        bmProject.setCreateBy(user.getUserName());
        //新增线索,默认值1
        bmProject.setProjectStatus(1L);
        bmProjectMapper.insertBmProject(bmProject);
        String isBackbussiness = bmProject.getIsBackbussiness();
        if (StringUtils.isBlank(isBackbussiness)) {
            bmProject.setIsBackbussiness("N");
        }
        return AjaxResult.success(bmProject);
    }

    /**
     * 修改项目(添加线索)
     *
     * @param bmProject 项目(添加线索)
     * @return 结果
     */
    @Override
    public int updateBmProject(BmProject bmProject) {
        bmProject.setUpdateTime(DateUtils.getNowDate());
        bmProject.setUpdateBy(SecurityUtils.getUsername());
        return bmProjectMapper.updateBmProject(bmProject);
    }

    /**
     * 批量删除项目(添加线索)
     *
     * @param ids 需要删除的项目(添加线索)ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectByIds(Long[] ids) {
        return bmProjectMapper.deleteBmProjectByIds(ids);
    }

    /**
     * 删除项目(添加线索)信息
     *
     * @param id 项目(添加线索)ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectById(Long id) {
        return bmProjectMapper.deleteBmProjectById(id);
    }

    /**
     * 审核项目状态(审核/提交/驳回项目)
     *
     * @param bmProjectStatus 审核请求对象
     * @return
     */
    @Override
    public AjaxResult examine(BmProjectStatus bmProjectStatus) {
        Long projectId = bmProjectStatus.getBmProjectId();
        if (projectId == null) {
            return AjaxResult.error("项目id为NULL");
        }
        Long code = bmProjectStatus.getCode();
        //查询项目状态最多有多少个
        int count = dictDataMapper.countDictDataByType("project_status");
        synchronized (this) {
            BmProject bmProject = bmProjectMapper.selectBmProjectById(projectId);
            //获取当前状态
            Long projectStatus = bmProject.getProjectStatus();
            //如果当前进度是1:新增线索,不能继续驳回
            if (projectStatus.equals(EXAMINE_PASS) && code.equals(EXAMINE_BACK)) {
                return AjaxResult.error("无法继续驳回");
            }
            if (projectStatus == count) {
                return AjaxResult.error("当前项目已结束,无法继续操作");
            }
            //提交审核/审核通过
            projectStatus = code.equals(EXAMINE_PASS) ? projectStatus + EXAMINE_PASS : projectStatus - EXAMINE_PASS;
            if (projectStatus < EXAMINE_PASS) {
                projectStatus = EXAMINE_PASS;
            }
            BmProject bmProjectNew = new BmProject();
            bmProjectNew.setId(projectId);
            bmProjectNew.setProjectStatus(projectStatus);
            bmProjectMapper.updateBmProject(bmProjectNew);
        }
        return AjaxResult.success();

    }

    /**
     * 首页查询待审核列表
     * @param bmProject
     * @return
     */
    @Override
    public List<BmProject> selectToBeReviewedList(BmProject bmProject) {
        List<BmProject> bmProjects = bmProjectMapper.selectToBeReviewedList(bmProject);
        return bmProjects;
    }

    /**
     * 统计分析公共查询列表
     * @param ids
     * @return
     */
    @Override
    public List<BmProject> selectStatisticsList(Long[] ids) {
        List<BmProject> bmProjects = bmProjectMapper.selectStatisticsList(ids);
        return bmProjects;
    }

    /**
     * 统计分析-在谈线索-柱状图
     * @param year
     * @return
     */
    @Override
    public Map<String, Object> selectProjectEharts(String year) {
        List<BmDeptPlanVo> bmDeptPlanVos = bmProjectMapper.selectProjectEharts(year);
        Map<String, Object> map  = new HashMap<>(2);
        List<String> deptName = new ArrayList<>();
        List<Integer> successMission = new ArrayList<>();
        if (bmDeptPlanVos.size() > 0){
            for (BmDeptPlanVo bmDeptPlanVo : bmDeptPlanVos) {
                deptName.add(bmDeptPlanVo.getDeptName());
                successMission.add(bmDeptPlanVo.getSuccessMission());
            }
        }
        map.put("x",deptName);
        map.put("y",successMission);
        return map;
    }

    /**
     * 统计分析-签约项目-柱状图
     * @param year
     * @return
     */
    @Override
    public Map<String, Object> selectProjectContractEharts(String year) {
        List<BmDeptPlanVo> bmDeptPlanVos = bmProjectMapper.selectProjectContractEharts(year);
        Map<String, Object> map  = new HashMap<>(3);
        List<String> deptName = new ArrayList<>();
        List<Integer> successMission = new ArrayList<>();
        List<BigDecimal> totAmt = new ArrayList<>();
        if (bmDeptPlanVos.size() > 0){
            for (BmDeptPlanVo bmDeptPlanVo : bmDeptPlanVos) {
                deptName.add(bmDeptPlanVo.getDeptName());
                successMission.add(bmDeptPlanVo.getSuccessMission());
                totAmt.add(bmDeptPlanVo.getTotAmt());
            }
        }
        map.put("x",deptName);
        map.put("y1",successMission);
        map.put("y2",totAmt);
        return map;
    }
}
