package com.willalp.number.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.core.domain.entity.SysDictData;
import com.willalp.common.lock.Locked;
import com.willalp.common.utils.DateUtils;
import com.willalp.number.service.IHsSysNumberRuleService;
import com.willalp.number.domain.HsSysNumberRule;
import com.willalp.number.domain.req.NumberRuleReq;
import com.willalp.number.domain.vo.NumberRuleVo;
import com.willalp.number.mapper.HsSysNumberRuleMapper;
import com.willalp.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 编号规则Service业务层处理
 *
 * @author songyu
 * @date 2021-11-24
 */
@Service
public class HsSysNumberRuleServiceImpl extends ServiceImpl<HsSysNumberRuleMapper, HsSysNumberRule>  implements IHsSysNumberRuleService
{
    @Autowired
    private HsSysNumberRuleMapper hsSysNumberRuleMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询编号规则
     *
     * @param ruleId 编号规则主键
     * @return 编号规则
     */
    @Override
    public HsSysNumberRule selectHsSysNumberRuleByRuleId(String ruleId)
    {
        return hsSysNumberRuleMapper.selectHsSysNumberRuleByRuleId(ruleId);
    }

    /**
     * 查询编号规则列表
     *
     * @param hsSysNumberRule 编号规则
     * @return 编号规则
     */
    @Override
    public List<HsSysNumberRule> selectHsSysNumberRuleList(HsSysNumberRule hsSysNumberRule)
    {
        return hsSysNumberRuleMapper.selectHsSysNumberRuleList(hsSysNumberRule);
    }

    /**
     * 新增编号规则
     *
     * @param hsSysNumberRule 编号规则
     * @return 结果
     */
    @Override
    public int insertHsSysNumberRule(HsSysNumberRule hsSysNumberRule)
    {
        hsSysNumberRule.setCreateTime(DateUtils.getNowDate());
        return hsSysNumberRuleMapper.insertHsSysNumberRule(hsSysNumberRule);
    }

    /**
     * 修改编号规则
     *
     * @param hsSysNumberRule 编号规则
     * @return 结果
     */
    @Override
    public int updateHsSysNumberRule(HsSysNumberRule hsSysNumberRule)
    {
        hsSysNumberRule.setUpdateTime(DateUtils.getNowDate());
        return hsSysNumberRuleMapper.updateHsSysNumberRule(hsSysNumberRule);
    }

    /**
     * 批量删除编号规则
     *
     * @param ruleIds 需要删除的编号规则主键
     * @return 结果
     */
    @Override
    public int deleteHsSysNumberRuleByRuleIds(String[] ruleIds)
    {
        return hsSysNumberRuleMapper.deleteHsSysNumberRuleByRuleIds(ruleIds);
    }

    /**
     * 删除编号规则信息
     *
     * @param ruleId 编号规则主键
     * @return 结果
     */
    @Override
    public int deleteHsSysNumberRuleByRuleId(String ruleId)
    {
        return hsSysNumberRuleMapper.deleteHsSysNumberRuleByRuleId(ruleId);
    }

    @Locked(value = {"getNumber_#[numberRuleReq.ruleId]"},block = 10,expire = 10)
    @Override
    public synchronized String getNumber(NumberRuleReq numberRuleReq) {
        //定义返回字符串
        StringBuffer number = new StringBuffer();
        //获取规则
        HsSysNumberRule hsSysNumberRule = hsSysNumberRuleMapper.selectHsSysNumberRuleByRuleId(numberRuleReq.getRuleId());
        if (hsSysNumberRule != null){
            //规则列表
            List<NumberRuleVo> ruleVoList = new ArrayList<>();
            //规则第一部分
            if (hsSysNumberRule.getPartOneId() != null){
                NumberRuleVo numberRuleVo = new NumberRuleVo();
                numberRuleVo.setRuleId(hsSysNumberRule.getPartOneId());
                numberRuleVo.setRuleLength(hsSysNumberRule.getPartOneLength());
                numberRuleVo.setRuleValue(hsSysNumberRule.getPartOneValue());
                ruleVoList.add(numberRuleVo);
            }
            //规则第二部分
            if (hsSysNumberRule.getPartTwoId() != null){
                NumberRuleVo numberRuleVo = new NumberRuleVo();
                numberRuleVo.setRuleId(hsSysNumberRule.getPartTwoId());
                numberRuleVo.setRuleLength(hsSysNumberRule.getPartTwoLength());
                numberRuleVo.setRuleValue(hsSysNumberRule.getPartTwoValue());
                ruleVoList.add(numberRuleVo);
            }
            //规则第三部分
            if (hsSysNumberRule.getPartThreeId() != null){
                NumberRuleVo numberRuleVo = new NumberRuleVo();
                numberRuleVo.setRuleId(hsSysNumberRule.getPartThreeId());
                numberRuleVo.setRuleLength(hsSysNumberRule.getPartThreeLength());
                numberRuleVo.setRuleValue(hsSysNumberRule.getPartThreeValue());
                ruleVoList.add(numberRuleVo);
            }

            ruleVoList.stream().forEach(e ->{
                if (e.getRuleId() == 1){//固定值
                    number.append(e.getRuleValue());
                }else if (e.getRuleId() == 2){//流水(自增)
                    if (e.getRuleValue() == null){
                        e.setRuleValue("1");
                    }
                    String part_value = String.format("%0"+e.getRuleLength()+"d",Integer.parseInt(e.getRuleValue()));
                    number.append(part_value);
                    int index = ruleVoList.indexOf(e);
                    if (index == 0){
                        hsSysNumberRuleMapper.updatePartOneValue(hsSysNumberRule.getRuleId(),Long.valueOf(e.getRuleValue())+1);
                    }else if (index == 1){
                        hsSysNumberRuleMapper.updatePartTwoValue(hsSysNumberRule.getRuleId(),Long.valueOf(e.getRuleValue())+1);
                    }else if (index == 2){
                        hsSysNumberRuleMapper.updatePartThreeValue(hsSysNumberRule.getRuleId(),Long.valueOf(e.getRuleValue())+1);
                    }
                }else if (e.getRuleId() == 3){//年月
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(numberRuleReq.getDate());
                    int year = calendar.get(Calendar.YEAR);
                    int month = calendar.get(Calendar.MONTH);
                    //年月字典
                    List<SysDictData> yearList = sysDictDataMapper.selectDictDataByType("year_dic");
                    List<SysDictData> monthList = sysDictDataMapper.selectDictDataByType("month_dic");
                    yearList.stream().filter(t -> (t.getDictLabel().equals(String.valueOf(year)))).forEach(t ->number.append(t.getDictValue()));
                    monthList.stream().filter(t -> (t.getDictLabel().equals(String.valueOf(month)))).forEach(t ->number.append(t.getDictValue()));

                }else if (e.getRuleId() == 4){//固定入参
                    number.append(numberRuleReq.getInput());
                }
            });

        }

        return String.valueOf(number);
    }
}
