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

import cn.com.countrygarden.bi.main.dao.performance.PerformanceScoreMapper;
import cn.com.countrygarden.bi.main.dao.performance.PerformanceStatisticsMapper;
import cn.com.countrygarden.bi.main.model.performance.PerformanceScore;
import cn.com.countrygarden.bi.main.service.performance.PerformanceScoreService;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import java.util.List;

/**
 * 指标得分
 *
 * @author leo 严明斌
 * @version 2018-4-3 16:58:33
 */
@Transactional
@Service
public class PerformanceScoreServiceImpl implements PerformanceScoreService {

    @Autowired
    private PerformanceScoreMapper performanceScoreMapper;

    @Autowired
    private PerformanceStatisticsMapper performanceStatisticsMapper;

    @Override
    public List<PerformanceScore> selectList(PerformanceScore score) {
        return this.performanceScoreMapper.selectScoreList(score);
    }

    @Override
    public List<PerformanceScore> selecnNewtList(PerformanceScore score) {
        return this.performanceScoreMapper.selectNewScoreList(score);
    }

    @Override
    public int insertScoreList(List<PerformanceScore> scoreList) {
        return this.performanceScoreMapper.insertScoreList(scoreList);
    }

    @Override
    @Transactional
    public void updateScoreList(List<PerformanceScore> scoreList) {
        for (PerformanceScore score : scoreList) {
            this.performanceScoreMapper.updateByPrimaryKeySelective(score);
        }
    }

    @Override
    public void save(PerformanceScore model) {

    }

    @Override
    public void save(List<PerformanceScore> models) {

    }

    @Override
    public void deleteById(Integer id) {

    }

    @Override
    public void deleteByIds(String ids) {

    }

    @Override
    public void update(PerformanceScore model) {

    }

    @Override
    public PerformanceScore findById(Integer id) {
        return null;
    }

    @Override
    public PerformanceScore findBy(String fieldName, Object value) throws TooManyResultsException {
        return null;
    }

    @Override
    public List<PerformanceScore> findByIds(String ids) {
        return null;
    }

    @Override
    public List<PerformanceScore> findByCondition(Condition condition) {
        return null;
    }

    @Override
    public List<PerformanceScore> findAll() {
        return null;
    }

    @Override
    public PerformanceScore getByHierarchyId(PerformanceScore score) {
        // TODO Auto-generated method stub
        return this.performanceScoreMapper.getByHierarchyId(score);
    }

    @Override
    public PerformanceScore getscoreByHierarchyId(PerformanceScore score) {
        // TODO Auto-generated method stub
        return this.performanceScoreMapper.getscoreByHierarchyId(score);
    }

    @Override
    public PerformanceScore getNewScore(PerformanceScore score) {
        return this.performanceScoreMapper.getNewScore(score);
    }

    @Transactional
    @Override
    public void insertScore(PerformanceScore score) {
        this.performanceScoreMapper.insertScore(score);
    }

    @Transactional
    @Override
    public void UpdateNewScore(PerformanceScore score) {
        this.performanceScoreMapper.UpdateNewScore(score);
    }

    @Override
    public PerformanceScore getTotalPage(PerformanceScore score) {
        // TODO Auto-generated method stub
        return this.performanceScoreMapper.getTotalPage(score);
    }

    /* (non-Javadoc)

     * <p>@author zhaolie Title: UpdateNewScoretwo</p>

     * <p>Description: </p>

     * @param score

     * @see cn.com.countrygarden.bi.main.service.performance.PerformanceScoreService#UpdateNewScoretwo(cn.com.countrygarden.bi.main.model.performance.PerformanceScore)

     */
    @Override
    @Transactional
    public void UpdateNewScoretwo(PerformanceScore score) {
        // TODO Auto-generated method stub
        this.performanceScoreMapper.UpdateNewScoretwo(score);
    }

    @Override
    public int insertScoreSelective(PerformanceScore score) {
        return performanceScoreMapper.insertScoreSelective(score);
    }


    @Override
    public void updateScoreSelective(PerformanceScore score) {
        this.performanceScoreMapper.updateScoreSelective(score);
    }

    @Override
    public void saveQuartlyAverageScoreAndSumScore(int year, int quarter) {
        //区域相关操作
        this.performanceScoreMapper.deleteQuarterlySumScore(year, quarter);
        this.performanceScoreMapper.insertQuarterlySumScore(year, quarter);//区域
        this.performanceScoreMapper.insertQuarterlySumScoreCompany(year, quarter);//城市公司
        this.performanceStatisticsMapper.deleteQuarterlyAverageScore(year, quarter);
        this.performanceStatisticsMapper.insertQuarterlyAverageScore(year, quarter);


        //区域
        List<PerformanceScore> last = this.performanceScoreMapper.getLastQuarterSort(0, quarter == 1 ? year - 1 : year, quarter == 1 ? 4 : quarter-1);
        List<PerformanceScore> now = this.performanceScoreMapper.getLastQuarterSort(0, year, quarter);

        for (PerformanceScore score : last) {
            for (PerformanceScore performanceScore : now) {
                if (score.getHierarchyId().equals(performanceScore.getHierarchyId())) {
                    int lastNo = score.getNum();
                    int nowNo = performanceScore.getNum();
                    performanceScore.setNum(lastNo - nowNo);
                    performanceScoreMapper.updateNum(performanceScore);
                }
            }
        }

        //城市公司
        List<PerformanceScore> lastCompany = this.performanceScoreMapper.getLastQuarterSortCompany(1, quarter == 1 ? year - 1 : year, quarter == 1 ? 4 : quarter-1);
        List<PerformanceScore> nowCompany = this.performanceScoreMapper.getLastQuarterSortCompany(1, year, quarter);

        for (PerformanceScore score : lastCompany) {
            for (PerformanceScore performanceScore : nowCompany) {
                if (score.getHierarchyId().equals(performanceScore.getHierarchyId())) {
                    int lastNo = score.getNum();
                    int nowNo = performanceScore.getNum();
                    performanceScore.setNum(lastNo - nowNo);
                    performanceScoreMapper.updateNum(performanceScore);
                }
            }
        }
    }

}
