package cn.wisenergy.oaService.impl;

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.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);
        Map<String, Object> map = new HashMap<>();
        // 项目/商机编号
        map.put("projectNo", createProjectVo.getOaProjectId());
        // 项目/商机名称
        map.put("projectName", createProjectVo.getProjectName());
        // 负责人编号
        map.put("managerNo", managerNo);
        // 项目对应的商机不为空
        if (StringUtils.isNotBlank(createProjectVo.getBusinessId())) {
            map.put("boNo", createProjectVo.getBusinessId());
        }

        OaProject oaProject;
        // 项目
        if (createProjectVo.getType() == 1) {
            oaProject = projectAndBoMapper.getProjectByNameAndNo(map);
        }else { // 商机
            oaProject = projectAndBoMapper.getBoByNameAndNo(map);
        }
        return oaProject;
    }
}
