package com.zcxk.evaluation.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.dict.DictUtil;
import com.zcxk.evaluation.dao.EvaluationGradeMapper;
import com.zcxk.evaluation.entity.EvaluationGradeEntity;
import com.zcxk.evaluation.service.EvaluationGradeService;
import com.zcxk.model.LoginUser;
import com.zcxk.org.OrgInfoUtil;
import com.zcxk.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 考评等级Service接口
 *
 * @author: WYY
 * @date 2021-05-08 09:23
 */
@Service
public class EvaluationGradeServiceImpl extends ServiceImpl<EvaluationGradeMapper, EvaluationGradeEntity> implements EvaluationGradeService {

    @Resource
    private EvaluationGradeMapper evaluationGradeMapper;
    @Autowired
    private OrgInfoUtil orgInfoUtil;
    @Autowired
    private DictUtil dictUtil;
    /**
     * 自定义分页查询，含关联实体对像
     */
    @Override
    public IPage<EvaluationGradeEntity> findPage(IPage<EvaluationGradeEntity> page, EvaluationGradeEntity evaluationGradeEntity) {

        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            evaluationGradeEntity.setTenantId(currentUser.getTenantId());
            evaluationGradeEntity.setProgramItems(currentUser.getProgramItemList());
            evaluationGradeEntity.setUserType(currentUser.getType());
            //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
            evaluationGradeEntity.setPermissonType(currentUser.getPermissonType());
        }
        Page<EvaluationGradeEntity> iPage = evaluationGradeMapper.findPage(page, evaluationGradeEntity);
        for (EvaluationGradeEntity item : iPage.getRecords()) {
            item.setCompanyOrgName(orgInfoUtil.getOrgName(item.getCompanyOrgId()));
            if (item.getType() != null) {
                item.setTypeName(dictUtil.getDictName("SC_EVALUATION_GRADE_TYPE", item.getType().intValue()));
            }
            if (item.getEvaluationGrade() != null) {
                item.setEvaluationGradeName(dictUtil.getDictName("SC_EVALUATION_GRADE", item.getEvaluationGrade().intValue()));
            }
        }
        return iPage;
    }

    /**
     * 查列表
     */
    @Override
    public List<EvaluationGradeEntity> findList(EvaluationGradeEntity evaluationGradeEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if(currentUser!=null) {
            evaluationGradeEntity.setTenantId(currentUser.getTenantId());
            evaluationGradeEntity.setProgramItems(currentUser.getProgramItemList());
            evaluationGradeEntity.setUserType(currentUser.getType());
            //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
            evaluationGradeEntity.setPermissonType(currentUser.getPermissonType());
        }
        return evaluationGradeMapper.findList(evaluationGradeEntity);
    }

    /**
     * 批量删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delEvaluationGradeByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 单个删除
     */
    @Override
    public boolean delEvaluationGradeById(Long id) {
        return this.removeById(id);
    }

    /**
     * 保存
     */
    @Override
    public boolean addEvaluationGrade(EvaluationGradeEntity evaluationGrade) {
        if (this.save(evaluationGrade)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean addBatchEvaluationGrade(List<EvaluationGradeEntity> evaluationGrades) {
        for (EvaluationGradeEntity item : evaluationGrades) {
            this.save(item);
        }
        return true;
    }

    /**
     * 修改根居ID
     */
    @Override
    public boolean updateEvaluationGradeById(EvaluationGradeEntity evaluationGrade) {
        if (this.updateById(evaluationGrade)) {
            return true;
        }
        return false;
    }

    /**
     * 根居ID获取对象
     */
    @Override
    public EvaluationGradeEntity findEvaluationGradeById(Long id) {
        return evaluationGradeMapper.findEvaluationGradeById(id);
    }

    @Override
    public String getScoreGrade(String tenantId, Integer companyOrgId, BigDecimal score, boolean formatName){
        EvaluationGradeEntity gradeEntity = new EvaluationGradeEntity();
        gradeEntity.setTenantId(tenantId);
        gradeEntity.setCompanyOrgId(companyOrgId);
        List<EvaluationGradeEntity> list = evaluationGradeMapper.findList(gradeEntity);
        if (list == null) {
            return null;
        }
        // 0大于等于 2小于 1介于
        Long type = null;
        BigDecimal valueOne= null;
        BigDecimal valueTwo= null;
        Long grade = null;
        for (EvaluationGradeEntity evaluationGradeEntity: list) {
            type  = evaluationGradeEntity.getType();
            valueOne  = new BigDecimal(evaluationGradeEntity.getValueOne() == null ? 0 : evaluationGradeEntity.getValueOne());
            valueTwo  = new BigDecimal(evaluationGradeEntity.getValueTwo() == null ? 0 : evaluationGradeEntity.getValueTwo());
            // 大于等于 0大于等于 2小于 1介于
            if (type == 0 && score.compareTo(valueOne) > -1) {
                grade = evaluationGradeEntity.getEvaluationGrade();
                break;
            }
            // 小于
            if (type == 2 &&  score.compareTo(valueOne) == -1) {
                grade = evaluationGradeEntity.getEvaluationGrade();
                break;
            }
            // 介于 ： 比如取8-10，就是≥8，＜10
            if (type == 1 && score.compareTo(valueOne) > -1  && score.compareTo(valueTwo) == -1) {
                grade = evaluationGradeEntity.getEvaluationGrade();
                break;
            }
        }
        if (grade == null) {
            return null;
        }
        return formatName ? dictUtil.getDictName("SC_EVALUATION_GRADE", grade.intValue()) : grade.toString();
    }

    public static void main(String[] args) {
        BigDecimal score = new BigDecimal("60");
        System.out.println(score.compareTo(new BigDecimal("40")) > -1 && score.compareTo(new BigDecimal("50"))== -1);
    }
}
