package com.ygtx.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ygtx.common.constant.UserConstants;
import com.ygtx.common.core.domain.entity.SysAutoCodePart;
import com.ygtx.common.core.domain.entity.SysAutoCodeRule;
import com.ygtx.common.enums.PartTypeEnum;
import com.ygtx.common.utils.StringUtils;
import com.ygtx.system.mapper.SysAutoCodePartMapper;
import com.ygtx.system.mapper.SysAutoCodeRuleMapper;
import com.ygtx.system.mapper.SysDictDataMapper;
import com.ygtx.system.service.IAutoCodeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysAutoCodeRuleServiceImpl implements IAutoCodeRuleService {

    @Autowired
    private SysAutoCodeRuleMapper sysAutoCodeRuleMapper;
    @Autowired
    private SysAutoCodePartMapper sysAutoCodePartMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public SysAutoCodeRule getOne(String ruleCode) {
        SysAutoCodeRule param = new SysAutoCodeRule();
        param.setRuleCode(ruleCode);
        List<SysAutoCodeRule> rules = sysAutoCodeRuleMapper.selectSysAutoCodeResultList(param);
        if(CollectionUtil.isNotEmpty(rules)){
            return rules.get(0);
        }
        return null;
    }

    @Override
    public List<SysAutoCodeRule> selectAutoCodeList(SysAutoCodeRule sysAutoCodeRule) {
        return sysAutoCodeRuleMapper.selectSysAutoCodeResultList(sysAutoCodeRule);
    }

    @Override
    public SysAutoCodeRule findById(Long ruleId) {
        return sysAutoCodeRuleMapper.findById(ruleId);
    }

    @Override
    public Boolean checkRuleCodeUnique(SysAutoCodeRule sysAutoCodeRule) {
        SysAutoCodeRule rule = sysAutoCodeRuleMapper.checkRuleCodeUnique(sysAutoCodeRule.getRuleCode());
        if (StringUtils.isNotNull(rule) && rule.getRuleId().longValue() != sysAutoCodeRule.getRuleId().longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }

        return UserConstants.UNIQUE;
    }

    @Override
    public Boolean checkRuleNameUnique(SysAutoCodeRule sysAutoCodeRule) {
        SysAutoCodeRule rule = sysAutoCodeRuleMapper.checkRuleNameUnique(sysAutoCodeRule.getRuleName());
        if (StringUtils.isNotNull(rule) && rule.getRuleId().longValue() != sysAutoCodeRule.getRuleId().longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }

        return UserConstants.UNIQUE;
    }

    @Override
    public int insertInfo(SysAutoCodeRule rule) {
        return sysAutoCodeRuleMapper.add(rule);
    }

    @Override
    public int updateInfo(SysAutoCodeRule rule) {
        return sysAutoCodeRuleMapper.updateSysAutoCodeRule(rule);
    }

    @Override
    public int deleteById(Long ruleId) {

        return sysAutoCodeRuleMapper.deleteById(ruleId);
    }

    @Override
    public int deleteByIds(Long[] ruleIds) {
        for (Long ruleId: ruleIds
             ) {
            sysAutoCodeRuleMapper.deleteById(ruleId);
        }
        return 1;
    }

    @Override
    public String getExample(Long ruleId) {
        SysAutoCodePart sysAutoCodePart = new SysAutoCodePart();
        sysAutoCodePart.setRuleId(ruleId);
        List<SysAutoCodePart> sysAutoCodePartList = sysAutoCodePartMapper.selectSysAutoCodePartList(sysAutoCodePart);
        StringBuilder stringBuilder = new StringBuilder();
        sysAutoCodePartList.forEach(item->{
            String partType = item.getPartType();
            String bizType = item.getBizType();
            String bizItem = item.getBizItem();
            if(PartTypeEnum.PART_TYPE_FIXCHAR.getCode().equals(partType)){
                stringBuilder.append("[").append(item.getFixCharacter()).append("]");
            }else if(PartTypeEnum.PART_TYPE_DEFINE.getCode().equals(partType)){
                String label = sysDictDataMapper.selectDictLabel(bizType+"_autocode", bizItem);
                stringBuilder.append("[").append(label).append("]");
            }else if(PartTypeEnum.PART_TYPE_INPUTCHAR.getCode().equals(partType)){
                stringBuilder.append("[").append(PartTypeEnum.PART_TYPE_INPUTCHAR.getName()).append("]");
            }else if(PartTypeEnum.PART_TYPE_NOWDATE.getCode().equals(partType)){
                stringBuilder.append("[").append(PartTypeEnum.PART_TYPE_NOWDATE.getName()).append("]");
            }else if(PartTypeEnum.PART_TYPE_SERIALNO.getCode().equals(partType)){
                stringBuilder.append("[").append(PartTypeEnum.PART_TYPE_SERIALNO.getName()).append("]");
            }else{
                stringBuilder.append("[").append(PartTypeEnum.PART_TYPE_OTHER.getName()).append("]");
            }
        });
        return stringBuilder.toString();
    }
}
