package com.ktg.system.service.impl;

import cn.hutool.core.lang.Assert;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.core.domain.entity.SysAutoCodePart;
import com.ktg.common.core.domain.entity.SysAutoCodeRule;
import com.ktg.common.utils.StringUtils;
import com.ktg.system.mapper.SysAutoCodePartMapper;
import com.ktg.system.mapper.SysAutoCodeRuleMapper;
import com.ktg.system.service.IAutoCodePartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SysAutoCodePartServiceImpl implements IAutoCodePartService {

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

    @Override
    public List<SysAutoCodePart> listPart(SysAutoCodePart sysAutoCodePart) {
        return sysAutoCodePartMapper.selectSysAutoCodePartList(sysAutoCodePart);
    }

    @Override
    public SysAutoCodePart findById(Long partId) {
        return sysAutoCodePartMapper.findById(partId);
    }

    @Override
    public String checkPartUnique(SysAutoCodePart sysAutoCodePart) {
        SysAutoCodePart part = sysAutoCodePartMapper.checkPartUnique(sysAutoCodePart);
        Long partId = StringUtils.isNull(sysAutoCodePart.getPartId())?-1L:sysAutoCodePart.getPartId();
        if(StringUtils.isNotNull(part) && partId.longValue() != part.getPartId().longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertPart(SysAutoCodePart sysAutoCodePart) {
        // 1.检测此次新增长度是否大于总长度
        SysAutoCodeRule sysAutoCodeRule = sysAutoCodeRuleMapper.findById(sysAutoCodePart.getRuleId());
        Integer maxLength = sysAutoCodeRule.getMaxLength();
        // 2.获取细则里的所有长度
        Integer lengthSum = sysAutoCodePart.getPartLength();

        SysAutoCodePart dto = new SysAutoCodePart();
        dto.setRuleId(sysAutoCodePart.getRuleId());
        List<SysAutoCodePart> sysAutoCodeParts = sysAutoCodePartMapper.selectSysAutoCodePartList(dto);
        if (!sysAutoCodeParts.isEmpty()) {
            long sum = sysAutoCodeParts.stream().mapToLong(SysAutoCodePart::getPartLength).sum();
            lengthSum = Math.toIntExact(lengthSum + sum);
        }
        Assert.isFalse(maxLength != null && lengthSum != null && maxLength < lengthSum, "最大长度" + maxLength + "小于分段长度总和" + lengthSum + ",请扩容最大长度!");
        return sysAutoCodePartMapper.add(sysAutoCodePart);
    }

    @Override
    public int updatePart(SysAutoCodePart sysAutoCodePart) {
        // 1.检测此次新增长度是否大于总长度
        SysAutoCodeRule sysAutoCodeRule = sysAutoCodeRuleMapper.findById(sysAutoCodePart.getRuleId());
        Integer maxLength = sysAutoCodeRule.getMaxLength();
        // 2.获取细则里的所有长度
        Integer lengthSum = sysAutoCodePart.getPartLength();

        SysAutoCodePart dto = new SysAutoCodePart();
        dto.setRuleId(sysAutoCodePart.getRuleId());
        List<SysAutoCodePart> sysAutoCodeParts = sysAutoCodePartMapper.selectSysAutoCodePartList(dto);
        if (!sysAutoCodeParts.isEmpty()) {
            long sum = sysAutoCodeParts.stream().filter(o -> !Objects.equals(o.getPartId(), sysAutoCodePart.getPartId())).mapToLong(SysAutoCodePart::getPartLength).sum();
            lengthSum = Math.toIntExact(lengthSum + sum);
        }
        Assert.isFalse(maxLength != null && lengthSum != null && maxLength < lengthSum, "最大长度" + maxLength + "小于分段长度总和" + lengthSum + ",请扩容最大长度!");
        return sysAutoCodePartMapper.updateSysAutoCodePart(sysAutoCodePart);
    }

    @Override
    public int deleteByIds(Long[] partIds) {
        for (Long partId:partIds
             ) {
            sysAutoCodePartMapper.deleteById(partId);
        }
        return 1;
    }


}
