package cn.com.countrygarden.bi.main.service.wcx.impl;

import cn.com.countrygarden.bi.main.dao.dup.TbHsprContractMapper;
import cn.com.countrygarden.bi.main.dao.hmWygl.TbHSPRRegionAvatarInfoMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxRegionProjectMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxRegionProjectDto;
import cn.com.countrygarden.bi.main.model.hmWygl.TbHSPRRegionAvatarInfo;
import cn.com.countrygarden.bi.main.service.wcx.WcxRegionProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by ferren on 2018/6/23.
 */
@Service
public class WcxRegionProjectServiceImpl implements WcxRegionProjectService{
    @Autowired
    private TbHsprContractMapper tbHsprContractMapper;
    @Autowired
    private WcxRegionProjectMapper wcxRegionProjectMapper;
    @Autowired
    private TbHSPRRegionAvatarInfoMapper tbHSPRRegionAvatarInfoMapper;


    @Override
    public void bindRegionProject() {
        String isSelectProjectNameBlank = "0"; // 不处理项目名为空的项
        List<WcxRegionProjectDto> wcxRegProDtos = wcxRegionProjectMapper.selectAll(isSelectProjectNameBlank);
        for (WcxRegionProjectDto wcxRegProDto : wcxRegProDtos) {
            String projectName = wcxRegProDto.getProjectName();
            List<WcxRegionProjectDto> regionByProjectName = tbHsprContractMapper.findRegionByProjectName(projectName);
            if (regionByProjectName == null || regionByProjectName.size() > 1 || regionByProjectName.size() == 0) {
                // todo 存在重名的项,增加日志输出
                continue;
            }
            wcxRegionProjectMapper.updateRegionNameByRegionProjectId(wcxRegProDto.getRegionProjectId(), regionByProjectName.get(0).getRegionName());
        }
    }

    @Override
    public List<WcxRegionProjectDto> selectAllNCRegion() {
        List<WcxRegionProjectDto> ncRegions = wcxRegionProjectMapper.findAllNCRegion();
        return ncRegions;
    }

    @Override
    public List<WcxRegionProjectDto> selectNCProject(String regionName) {
        List<WcxRegionProjectDto> ncProjects = wcxRegionProjectMapper.findNCProject(regionName);
        return ncProjects;
    }

    @Override
    public WcxRegionProjectDto getByEmployeeCardId(String employeeCardId) {
        WcxRegionProjectDto condition = new WcxRegionProjectDto();
        condition.setEmployeeCardId(employeeCardId);
        WcxRegionProjectDto wcxRegProDto = wcxRegionProjectMapper.selectOne(condition);
        return wcxRegProDto;
    }

    @Override
    public WcxRegionProjectDto getProjectInfo(String ncProjectName) {
        String erpProjectName = wcxRegionProjectMapper.getErpProjectName(ncProjectName);
        if (!StringUtils.hasText(erpProjectName)) {
            return null;
        }
        String erpProjectCode = tbHsprContractMapper.getErpProjectCode(erpProjectName);
        if (!StringUtils.hasText(erpProjectCode)) {
            return null;
        }

        List<String> erpProjectCodes = new ArrayList<>();
        erpProjectCodes.add(erpProjectCode);
        List<TbHSPRRegionAvatarInfo> infos = tbHSPRRegionAvatarInfoMapper.selectCommList(erpProjectCodes);
        if (infos == null) {
            return null;
        }
        WcxRegionProjectDto rpDto = new WcxRegionProjectDto();
        rpDto.setProjectHeadPicUrl(infos.get(0).getImageOnePath());
        rpDto.setProjectEmployeeName(infos.get(0).getCommName());
        return rpDto;
    }

    public WcxRegionProjectDto getRegionInfo(String ncRegionName){
        if (!StringUtils.hasText(ncRegionName)) {
            return null;
        }

        String substr = ncRegionName.substring(ncRegionName.length() - 4, ncRegionName.length());
        String erpRegionName = null;
        if ("（物业）".equals(substr) || "（房产）".equals(substr)) {
            erpRegionName = ncRegionName.substring(0, ncRegionName.length() - 4);
        }

        String erpRegionCode = tbHsprContractMapper.getErpProjectCode(erpRegionName);
        if (!StringUtils.hasText(erpRegionCode)) {
            return null;
        }
        List<String> erpRegionCodes = new ArrayList<>();
        erpRegionCodes.add(erpRegionCode);
        List<TbHSPRRegionAvatarInfo> infos = tbHSPRRegionAvatarInfoMapper.selectOrganList(erpRegionCodes);
        if (infos == null) {
            return null;
        }

        WcxRegionProjectDto rpDto = new WcxRegionProjectDto();
        rpDto.setRegionHeadPicUrl(infos.get(0).getImageOnePath());
        rpDto.setRegionEmployeeName(infos.get(0).getCommName());
        return rpDto;
    }

    @Override
    public List<WcxRegionProjectDto> selectBestPassNumberProject(Date startDate, Date endDate) {
        List<WcxRegionProjectDto> rpDtos = wcxRegionProjectMapper.selectBestPassNumberProject(startDate, endDate);
        if (rpDtos == null) {
            return Collections.emptyList();
        }
        // 设置对应得项目总信息
        for (WcxRegionProjectDto rpDto : rpDtos) {
            WcxRegionProjectDto res = this.getProjectInfo(rpDto.getProjectName());
            if (res != null) {
                rpDto.setProjectEmployeeName(res.getProjectEmployeeName());
                rpDto.setProjectHeadPicUrl(res.getProjectHeadPicUrl());
            }
        }
        return rpDtos;
    }

    @Override
    public List<WcxRegionProjectDto> selectBestPassNumberRegion(Date startDate, Date endDate) {
        List<WcxRegionProjectDto> rpDtos = wcxRegionProjectMapper.selectBestPassNumberRegion(startDate, endDate);
        if (rpDtos == null) {
            return Collections.emptyList();
        }
        // 设置对应得项目总信息
        for (WcxRegionProjectDto rpDto : rpDtos) {
            WcxRegionProjectDto res = this.getProjectInfo(rpDto.getProjectName());
            if (res != null) {
                rpDto.setRegionEmployeeName(res.getRegionEmployeeName());
                rpDto.setRegionHeadPicUrl(res.getRegionHeadPicUrl());
            }
        }
        return rpDtos;
    }

    @Override
    public String selectErpRegionName(String userCode) {
        List<WcxRegionProjectDto> rp = tbHsprContractMapper.findErpRegionByUserCode(userCode);
        if (rp == null || rp.size() <= 0) {
            return null;
        }
        return rp.get(0).getRegionName();
    }

    @Override
    public List<String> selectErpRegionNameByIn(List<String> userCode, String regionName) {
        if (StringUtils.hasText(regionName)) {
            if (regionName.contains("广州东莞深区域")) {
                regionName = "广州东/莞深区域";
            }
            if (regionName.contains("广清佛山区域")) {
                regionName = "广清/佛山区域";
            }
            if (regionName.contains("粤东惠深区域")) {
                regionName = "粤东/惠深区域";
            }
            if (regionName.contains("广清佛山区域")) {
                regionName = "广清/佛山区域";
            }
            if (regionName.contains("广西云南区域")) {
                regionName = "广西/云南区域";
            }
            if (regionName.contains("广清莞深区域")) {
                regionName = "广清/莞深区域";
            }
        }
        List<String> userName = tbHsprContractMapper.findErpRegionByUserCodeIn(userCode,regionName);
        return userName;
    }

    @Override
    public List<String> selectErpRegionList(String userCode) {
        List<String> rp = tbHsprContractMapper.findErpRegionList(userCode);
        if (rp == null || rp.size() <= 0) {
            return null;
        }
        return rp;
    }
}
