package com.mortals.xhx.module.perform.service.impl;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.base.system.idgenerator.service.IdgeneratorService;
import com.mortals.xhx.common.code.CommentTypeEnum;
import com.mortals.xhx.common.code.PerformRulesTypeEnum;
import com.mortals.xhx.common.code.PerformTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.perform.dao.PerformRulesDao;
import com.mortals.xhx.module.perform.model.PerformRulesEntity;
import com.mortals.xhx.module.perform.service.PerformRulesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import static com.mortals.xhx.base.system.idgenerator.service.impl.IdgeneratorServiceImpl.IdGeneratorKey.*;

/**
 * PerformRulesService
 * 绩效规则信息 service实现
 *
 * @author zxfei
 * @date 2023-05-16
 */
@Service("performRulesService")
@Slf4j
public class PerformRulesServiceImpl extends AbstractCRUDCacheServiceImpl<PerformRulesDao, PerformRulesEntity, Long> implements PerformRulesService {

    @Autowired
    private IdgeneratorService idgeneratorService;

    @Override
    protected String getExtKey(PerformRulesEntity data) {
        return data.getRuleCode();
    }

    @Override
    protected void saveBefore(PerformRulesEntity entity, Context context) throws AppException {
        if (ObjectUtils.isEmpty(entity.getRuleCode())) {
            String ruleCode = "";
/*
            if (PerformRulesTypeEnum.考勤绩效.getValue() == entity.getType()) {
                ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + "_" + idgeneratorService.getLongId(ATTEND_KEY);
            }
*/
            switch (PerformRulesTypeEnum.getByValue(entity.getType())) {
                case 考勤绩效:
                    ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + idgeneratorService.getLongId(ATTEND_KEY);
                    break;
                case 评价差评绩效:
                    ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + idgeneratorService.getLongId(REVIEW_KEY);
                    break;
                case 评价投诉绩效:
                    ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + idgeneratorService.getLongId(COMPLAIN_KEY);
                    break;
                case 办件绩效:
                    ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + idgeneratorService.getLongId(GOWORK_KEY);
                    break;
                case 效能绩效:
                    ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + idgeneratorService.getLongId(EFFECT_KEY);
                    break;
                case 其它绩效:
                    ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + idgeneratorService.getLongId(OTHER_KEY);
                    break;
                default:
                    throw new AppException("绩效类型不支持！");
            }
            //自定义默认规则code
            //PerformTypeEnum
            // String ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc() + "_" + PinyinUtil.getFirstLetter(ReUtil.replaceAll(entity.getName(), "[^\\u4E00-\\u9FA5]", ""), "");
            //String ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc().toUpperCase() + "_" + idgeneratorService.getLongId();

            entity.setRuleCode(ruleCode);
        }

        super.saveBefore(entity, context);
    }

    public static void main(String[] args) {

        String str = "评价器评议、短信、电话回访等结果经核实为“不满意”的/次";

        str = ReUtil.replaceAll(str, "[^\\u4E00-\\u9FA5]", "");

        System.out.println(PinyinUtil.getPinyin(str));
        System.out.println(PinyinUtil.getPinyin(str, ""));
        System.out.println(PinyinUtil.getFirstLetter(str, ""));

    }
}