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.common.code.PerformRulesTypeEnum;
import com.mortals.xhx.common.code.PerformTypeEnum;
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;

/**
 * PerformRulesService
 * 绩效规则信息 service实现
 *
 * @author zxfei
 * @date 2023-05-16
 */
@Service("performRulesService")
@Slf4j
public class PerformRulesServiceImpl extends AbstractCRUDCacheServiceImpl<PerformRulesDao, PerformRulesEntity, Long> implements PerformRulesService {
    @Override
    protected String getExtKey(PerformRulesEntity data) {
        return data.getRuleCode();
    }

    @Override
    protected void saveBefore(PerformRulesEntity entity, Context context) throws AppException {
        if (ObjectUtils.isEmpty(entity.getRuleCode())) {
            //自定义默认规则code
            String ruleCode = PerformRulesTypeEnum.getByValue(entity.getType()).getDesc() + "_" + PinyinUtil.getFirstLetter(ReUtil.replaceAll(entity.getName(), "[^\\u4E00-\\u9FA5]", ""), "");
            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, ""));

    }
}