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

import cn.com.countrygarden.bi.main.constant.wcx.enumeration.WcxBaselineEnum;
import cn.com.countrygarden.bi.main.dao.wcx.WcxReportMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReportDto;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.service.wcx.WcxReportService;
import cn.com.countrygarden.bi.main.util.wcx.PageUtil;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * Created by ADMS on 2018/8/14.
 */
@Service
public class WcxReportServiceImpl implements WcxReportService {
    @Autowired
    private WcxReportMapper wcxReportMapper;

    @Override
    public PageData selectProjectReportPageData(WcxInnovationDto param) {
        int pageOffset = PageUtil.getPageOffset(param.getPageIndex(), param.getPageSize());
        param.setPageOffset(pageOffset);

        List<WcxReportDto> data = wcxReportMapper.selectProjectReportPageData(param);

        if (data != null && data.size() > 0) {
            for (WcxReportDto datum : data) {
                Integer innNum = datum.getInnNum();
                Integer thumbNum = datum.getThumbNum();
                Integer regNum = datum.getRegNum();
                if (innNum == null) {
                    datum.setInnNum(0);
                }
                if (thumbNum == null) {
                    datum.setThumbNum(0);
                }
                if (regNum == null) {
                    datum.setRegNum(0);
                }
            }
        }

        Long total = wcxReportMapper.selectProjectReportPageCount(param);

        PageRowBounds pageRowBounds = new PageRowBounds(param.getPageIndex(), param.getPageSize());
        pageRowBounds.setTotal(total);
        pageRowBounds.setCount(true);

        PageData pageData = new PageData(data, pageRowBounds);
        return pageData;
    }

    @Override
    public PageData selectRegionReportPageData(WcxInnovationDto param) {
        int pageOffset = PageUtil.getPageOffset(param.getPageIndex(), param.getPageSize());
        param.setPageOffset(pageOffset);

        List<WcxReportDto> data = wcxReportMapper.selectRegionReportPageData(param);

        if (data != null && data.size() > 0) {
            for (WcxReportDto datum : data) {
                Integer innNum = datum.getInnNum();
                Integer thumbNum = datum.getThumbNum();
                Integer regNum = datum.getRegNum();
                if (innNum == null) {
                    datum.setInnNum(0);
                }
                if (thumbNum == null) {
                    datum.setThumbNum(0);
                }
                if (regNum == null) {
                    datum.setRegNum(0);
                }
            }
        }

        Long total = wcxReportMapper.selectRegionReportPageCount(param);

        PageRowBounds pageRowBounds = new PageRowBounds(param.getPageIndex(), param.getPageSize());
        pageRowBounds.setTotal(total);
        pageRowBounds.setCount(true);

        PageData pageData = new PageData(data, pageRowBounds);
        return pageData;
    }

    @Override
    public List<WcxReportDto> selectRegionReportList(WcxInnovationDto param) {
        List<WcxReportDto> data = wcxReportMapper.selectRegionReportPageData(param);
        if (data != null && data.size() > 0) {
            for (WcxReportDto datum : data) {
                Integer innNum = datum.getInnNum();
                Integer thumbNum = datum.getThumbNum();
                Integer regNum = datum.getRegNum();
                if (innNum == null) {
                    datum.setInnNum(0);
                }
                if (thumbNum == null) {
                    datum.setThumbNum(0);
                }
                if (regNum == null) {
                    datum.setRegNum(0);
                }
            }
        }
        return data;
    }

    @Override
    public List<WcxReportDto> selectProjectReportList(WcxInnovationDto param) {
        List<WcxReportDto> data = wcxReportMapper.selectProjectReportPageData(param);
        if (data != null && data.size() > 0) {
            for (WcxReportDto datum : data) {
                Integer innNum = datum.getInnNum();
                Integer thumbNum = datum.getThumbNum();
                Integer regNum = datum.getRegNum();
                if (innNum == null) {
                    datum.setInnNum(0);
                }
                if (thumbNum == null) {
                    datum.setThumbNum(0);
                }
                if (regNum == null) {
                    datum.setRegNum(0);
                }
            }
        }
        return data;
    }

    @Override
    public PageData selectBaselineReportPageData(WcxInnovationDto param) {
        int pageIndex = param.getPageIndex();
        int pageSize = param.getPageSize();

        List<WcxReportDto> data = selectBaselineReportList(param);
        // 假分页展示
        PageData pageData = PageUtil.spiltPage(data, pageIndex, pageSize);
        return pageData;
    }

    @Override
    public List<WcxReportDto> selectBaselineReportList(WcxInnovationDto param) {
        List<WcxReportDto> data = wcxReportMapper.findBaselineReportPageData(param);

        // 经过处理后的数据
        Long total = 0L;
        List<WcxReportDto> dataDeal = null;
        if (data != null && data.size() > 0) {
            // key = regionName(不包含房产及物业) + ":@:" + baselineCode
            Map<String, WcxReportDto> reportMap = new HashMap<>();

            for (WcxReportDto datum : data) {
                // 数据初始化
                Integer innNum = datum.getInnNum();
                Integer thumbNum = datum.getThumbNum();
                Integer regNum = datum.getRegNum();
                String baselineCode = datum.getBaselineCode();

                if (innNum == null) {
                    datum.setInnNum(0);
                }
                if (thumbNum == null) {
                    datum.setThumbNum(0);
                }
                if (regNum == null) {
                    datum.setRegNum(0);
                }
                if (baselineCode != null) {
                    String baselineName = WcxBaselineEnum.getBaselineName(baselineCode);
                    datum.setBaselineName(baselineName);
                }

                // 数据分类放置
                String regionName = datum.getRegionName();
                String ncRegionSimpleName = getNcRegionSimpleName(regionName);
                String key = ncRegionSimpleName + ":@:" + datum.getBaselineCode();
                WcxReportDto value = reportMap.get(key);
                if (value == null) {
                    datum.setRegionName(ncRegionSimpleName);
                    reportMap.put(key, datum);
                } else {
                    value.setInnNum(value.getInnNum() + datum.getInnNum());
                    value.setThumbNum(value.getThumbNum() + datum.getThumbNum());
                }
            }

            dataDeal = new ArrayList<>();
            for (Map.Entry<String, WcxReportDto> entry : reportMap.entrySet()) {
                dataDeal.add(entry.getValue());
            }
            total = (long)dataDeal.size();
        }
        return dataDeal;
    }

    /**
     * 获取Nc区域简易名称,去除区域最后的(房产),(物业)
     *
     * @param ncRegionName
     * @return
     */
    private String getNcRegionSimpleName(String ncRegionName) {
        if (!StringUtils.hasText(ncRegionName)) {
            return null;
        }

        int regionLength = ncRegionName.length();
        if (regionLength <= 4) {
            return ncRegionName;
        }

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