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

import cn.com.countrygarden.bi.main.dao.performance.PerformanceMonthlyMapper;
import cn.com.countrygarden.bi.main.dao.performance.PerformanceMonthlyStatisticsMapper;
import cn.com.countrygarden.bi.main.model.performance.PerformanceMonthly;
import cn.com.countrygarden.bi.main.service.performance.PerformanceMonthlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 绩效考核月度表
 *
 * @author Max  Date:2018/09/18
 * @version 1.0
 * @since 1.8
 */
@Service
public class PerformanceMonthlyServiceImpl implements PerformanceMonthlyService {

    @Autowired
    private PerformanceMonthlyMapper dao;

    @Autowired
    private PerformanceMonthlyStatisticsMapper performanceMonthlyStatisticsMapper;

    @Override
    public int deleteByPrimaryKey(Long monthId) {
        return dao.deleteByPrimaryKey(monthId);
    }

    @Override
    public int insertSelective(PerformanceMonthly record) {
        return dao.insertSelective(record);
    }

    @Override
    public PerformanceMonthly selectByPrimaryKey(Long monthId) {
        return dao.selectByPrimaryKey(monthId);
    }

    @Override
    public int updateByPrimaryKeySelective(PerformanceMonthly record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    //通过年，月度，区域，指标查询唯一一条记录
    @Override
    public PerformanceMonthly getSelect(PerformanceMonthly record) {
        return dao.getSelect(record);
    }

    @Override
    public void saveMonthAverageAndRank(long elementId, int year, int month) {
        //删除0和1用于计算存储的指标
        dao.deleteMonthlyAvgScore(year, month);
        //重新计算区域平均分
        dao.insertMonthAvgScoreRegion(year, month);
        //重新计算城市公司平均分
        dao.insertMonthAvgScoreCompany(year, month);

        //删除当前指标/年/月平均完成率
        performanceMonthlyStatisticsMapper.deleteAverage(year, month);
        //重新计算当前指标/年/月平均完成率
        performanceMonthlyStatisticsMapper.insertMonthlyAverageScore(year, month);

        //计算该指标排名
        calcRanges(elementId, year, month);

        calcRanges(0, year, month);//计算区域排名

        calcRanges(1, year, month);//计算城市公司排名

    }

    private void calcRanges(long elementId, int year, int month) {
        List<PerformanceMonthly> lasts = null;
        //计算排名
        if (month == 1) {//如果是1月，上个月就是上一年的第12个月
            lasts = dao.getMonthSort(elementId, year - 1, 12);//上个月
        } else {
            lasts = dao.getMonthSort(elementId, year, month - 1);//上个月
        }

        List<PerformanceMonthly> nows = dao.getMonthSort(elementId, year, month);

        if (lasts.size() == 0) {//上个月无数据
            //直接按照当前排名进行更新
            for (PerformanceMonthly performanceMonthly : nows) {
                updateByPrimaryKeySelective(performanceMonthly);
            }
        } else {//上个月有数据则进行对比更新
            for (PerformanceMonthly last : lasts) {
                for (PerformanceMonthly now : nows) {
                    if (last.getHierarchyId().equals(now.getHierarchyId())) {
                        int lastNo = last.getRanges();
                        int nowNo = now.getRanges();
                        now.setRanges(lastNo - nowNo);
                        updateByPrimaryKeySelective(now);
                    }
                }
            }
        }
    }

    @Override
    public Map<String, Integer> selectOrganYearAndMonth() {
        return dao.selectOrganYearAndMonth();
    }

    @Override
    public Map<String, Integer> selectCompanyYearAndMonth() {
        return dao.selectCompanyYearAndMonth();
    }
}