package cn.wisenergy.oaService.impl;

import cn.wisenergy.common.utils.StringUtil;
import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.model.vo.CreateProjectVo;
import cn.wisenergy.model.vo.ModifyProjectVo;
import cn.wisenergy.oaMapper.ProjectAndBoMapper;
import cn.wisenergy.oaModel.OaProject;
import cn.wisenergy.oaService.OaProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: nh
 * @create: 2021-04-20 11:16
 **/
@Service
@Slf4j
public class OaProjectServiceImpl implements OaProjectService {
    @Autowired
    private ProjectAndBoMapper projectAndBoMapper;

    @Override
    public List<OaProject> getListOaProject(String oaDeptNo, Integer typeId, String projectNameOrNo, List<String> oaProjectIds) {
        log.info("OaProjectServiceImpl[]getListOaProject[]input.param{}oaDeptNo, typeId, projectNameOrNo, oaProjectIds =>" + oaDeptNo +"," + typeId + "," + projectNameOrNo + "," +oaProjectIds);
        if (typeId == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(oaDeptNo)) {
            map.put("oaDeptNo", oaDeptNo);
        }

        map.put("projectNameOrNo", projectNameOrNo);
        if (!CollectionUtils.isEmpty(oaProjectIds)) {
            map.put("oaProjectIds", oaProjectIds);
        }
        List<OaProject> listOaProject = null;

        if (typeId.equals(1)) {
            listOaProject = projectAndBoMapper.getListOaProjectByDept(map);
        }
        if (typeId.equals(2)) {
            listOaProject = projectAndBoMapper.getListOaBoByDept(map);
        }
        return listOaProject;
    }


    @Override
    public OaProject getOaProjectByNoAndName(CreateProjectVo createProjectVo, String managerNo) {
        log.info("OaProjectServiceImpl[]getOaProjectByNoAndName[]input.param{}createProjectVo,managerNo =>" + createProjectVo + "," + managerNo);
        if (StringUtils.isBlank(createProjectVo.getBusinessId())  && StringUtils.isNotBlank(createProjectVo.getBusinessName())) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.BO_INFO_NOT_FIND);
        }

        Map<String, Object> map = new HashMap<>();
        // 项目/商机编号
        map.put("projectNo", createProjectVo.getOaProjectId());
        // 项目/商机名称
        map.put("projectName", createProjectVo.getProjectName());


        OaProject oaProject;
        // 项目
        if (createProjectVo.getType() == 1) {
            oaProject = projectAndBoMapper.getProjectByNameAndNo(map);
            if (oaProject == null) {
                return null;
            }
            if (createProjectVo.getBusinessId() != null) {
                // 项目中的商机编号与oa中不一致
                if (!createProjectVo.getBusinessId().equals(oaProject.getBo())) {
                    throw new BaseCustomException(BASE_RESP_CODE_ENUM.PROJECT_BO_INFO_ERROR);
                }
                // 清除查询条件，查询商机信息
                map.clear();
                map.put("projectNo", createProjectVo.getBusinessId());
                oaProject = projectAndBoMapper.getBoByNameAndNo(map);
                // 校验对应商机信息
                checkBoInfo(oaProject, createProjectVo.getBusinessName());
            }
        }else { // 商机
            oaProject = projectAndBoMapper.getBoByNameAndNo(map);
        }
        return oaProject;
    }

    @Override
    public OaProject getByNameAndNo(ModifyProjectVo modifyProjectVo) {
        log.info("OaProjectServiceImpl[]getByNameAndNo[]input.param{}modifyProjectVo");
        if (StringUtils.isBlank(modifyProjectVo.getBusinessId())) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.PROJECT_BO_INFO_ERROR);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectNo", modifyProjectVo.getOaProjectId());
        // 根据项目中的oa编号查询项目信息，是否商机编号一致
        OaProject oaProject = projectAndBoMapper.getProjectByNo(modifyProjectVo.getOaProjectId());
        if (oaProject == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.PROJECT_OA_ID_IS_ERROR);
        }
        if (StringUtils.isBlank(oaProject.getBo())  || !modifyProjectVo.getBusinessId().equals(oaProject.getBo())) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.PROJECT_BO_INFO_ERROR);
        }

        // 根据商机编号查询商机信息，为空则提示
        OaProject oaProject1 = projectAndBoMapper.getBoByNo(modifyProjectVo.getBusinessId());
        if (oaProject1 == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.BO_INFO_NOT_FIND);
        }
        // 修改的商机名称与OA中的商机名称不一致
        if (StringUtils.isBlank(modifyProjectVo.getBusinessName()) || !StringUtil.replaceBlank(oaProject1.getName()).equals(modifyProjectVo.getBusinessName())) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.PROJECT_BO_INFO_ERROR);
        }

        return oaProject;
    }

    //校验对应商机信息
    private void checkBoInfo(OaProject oaProject, String businessName) {
        log.info("checkBoInfo[]input.param{}oaProject,businessName" + oaProject + "," + businessName);
        if (oaProject == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.BO_INFO_NOT_FIND);
        }
        if (!StringUtil.replaceBlank(oaProject.getName()).equals(StringUtil.replaceBlank(businessName)) ){
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.PROJECT_BO_INFO_ERROR);
        }
    }
}
