package com.shkj.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.core.domain.Result;
import com.shkj.wms.bo.BasRuleAddBo;
import com.shkj.wms.bo.BasRuleBo;
import com.shkj.wms.domain.BasRule;
import com.shkj.wms.domain.BasRuleDetail;
import com.shkj.wms.domain.BaseAutoCode;
import com.shkj.wms.mapper.BasRuleDetailMapper;
import com.shkj.wms.mapper.BasRuleMapper;
import com.shkj.wms.mapper.BaseAutoCodeMapper;
import com.shkj.wms.service.IBasRuleDetailService;
import com.shkj.wms.service.IBasRuleService;
import com.shkj.wms.vo.BasRuleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 编码规则Service业务层处理
 *
 * @author j.liang
 * @date 2022-12-08
 */
@Slf4j
@Service
public class BasRuleServiceImpl extends ServiceImpl<BasRuleMapper, BasRule> implements IBasRuleService {
    public static ThreadLocal<Boolean> threadLocal = new ThreadLocal<>();


    @Autowired
    BasRuleMapper basRuleMapper;

    @Autowired
    IBasRuleDetailService iBasRuleDetailService;

    @Autowired
    BasRuleDetailMapper basRuleDetailMapper;

    @Autowired
    BaseAutoCodeMapper baseAutoCodeMapper;

    /**
     * 查询编码规则
     */
    @Override
    public BasRule queryById(Long ruleId) {
        return basRuleMapper.selectById(ruleId);
    }

    /**
     * 查询编码规则列表
     */
    @Override
    public List<BasRuleVo> queryPageList(BasRuleBo bo) {
        return basRuleMapper.queryPageList(bo);
    }


    /**
     * 新增编码规则
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> insertBasRuleByBo(BasRuleAddBo bo) {

        Boolean aBoolean = checkNameUnique(bo);
        if (aBoolean) {
            return Result.err().msg("规则编码或名称已存在");
        }

        BasRule add = BeanUtil.toBean(bo, BasRule.class);

        baseMapper.insert(add);
        if (CollectionUtils.isNotEmpty(bo.getList())) {
            iBasRuleDetailService.saveBatchRuleDetail(add.getId(), bo.getList());
        }

        return Result.ok();
    }

    /**
     * 修改编码规则
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(BasRuleAddBo bo) {
        BasRule update = BeanUtil.toBean(bo, BasRule.class);

        boolean flag = baseMapper.updateById(update) > 0;

        if (CollectionUtils.isNotEmpty(bo.getList())) {
            iBasRuleDetailService.saveBatchRuleDetail(update.getId(), bo.getList());
        }
        return flag;
    }


    /**
     * 删除编码规则
     */
    @Override
    public Boolean deleteWithValidById(Long id) {
        basRuleDetailMapper.delete(new LambdaQueryWrapper<BasRuleDetail>().eq(BasRuleDetail::getRuleId, id));
        return baseMapper.deleteById(id) > 0;
    }


    public Boolean checkNameUnique(BasRuleAddBo basRuleBo) {

        LambdaQueryWrapper<BasRule> wrapper = new LambdaQueryWrapper<BasRule>()
                .eq(BasRule::getRuleNum, basRuleBo.getRuleNum())
                .eq(BasRule::getRuleName, basRuleBo.getRuleName());
        BasRule one = getOne(wrapper);
        if (one != null) {
            return true;
        }
        return false;
    }


    /**
     * 修改编码规则状态
     */
    @Override
    public Result<Boolean> switchStatus(Long id, String status) {
        BasRule rule = new BasRule();
        rule.setId(id);
        rule.setStatus(status);
        return this.updateById(rule) ? Result.ok() : Result.err();
    }

    /**
     * 自动生成业务编号
     */
    @Override
    synchronized
    public String genSerialCode(String ruleCode) {
        // 暂时废弃
        BasRule rule = baseMapper.selectOne(new LambdaQueryWrapper<BasRule>().eq(BasRule::getRuleNum, ruleCode));
        Assert.notNull(rule, "未获取到指定类型:[{}]的业务编码生成规则", ruleCode);
        List<BasRuleDetail> ruleDetailList = basRuleDetailMapper.selectList(new LambdaQueryWrapper<BasRuleDetail>().eq(BasRuleDetail::getRuleId, rule.getId()));
        return System.currentTimeMillis() + "";
//        if (rule.getRuleItem().equals("0")) {
//            //自动生成固定值编码
//            Long numLength = basRule.get(0).getRuleLength();
//            String code = basRule.get(0).getRuleValue() + System.currentTimeMillis();
//            return saveAutoCode(basRule.get(0).getRuledetailId(), code, inputCharacter, "0", 1L);
//        } else if (basRule.get(0).getRuleItem().equals("1")) {
//            //自动生成变量编码
//            String code = inputCharacter + System.currentTimeMillis();
//            saveAutoCode(basRule.get(0).getRuledetailId(), code, inputCharacter, "1", 1L);
//            return code;
//        } else if (basRule.get(0).getRuleItem().equals("2")) {
//            //自动生成日期编码
//            if (basRule.get(0).getRuleValue().equals("3")) {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//                String code = sdf.format(System.currentTimeMillis());
//                return code;
//            } else if (basRule.get(0).getRuleValue().equals("4")) {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
//                String code = sdf.format(System.currentTimeMillis());
//                return code;
//            } else if (basRule.get(0).getRuleValue().equals("5")) {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                String code = sdf.format(System.currentTimeMillis());
//                return code;
//            } else if (basRule.get(0).getRuleValue().equals("6")) {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
//                String code = sdf.format(System.currentTimeMillis());
//                return code;
//            } else if (basRule.get(0).getRuleValue().equals("7")) {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
//                String code = sdf.format(System.currentTimeMillis());
//                return code;
//            } else if (basRule.get(0).getRuleValue().equals("8")) {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//                String code = sdf.format(System.currentTimeMillis());
//                return code;
//            }
//        } else if (basRule.get(0).getRuleItem().equals("3")) {
//            //自动生成流水号
//            if (basRule.get(0).getRuleValue().equals("9")) {
//                Long numLength = basRule.get(0).getRuleLength();
//                String code = StringUtils.leftPad("1", numLength.intValue(), "0");
//                return saveAutoCode(basRule.get(0).getRuledetailId(), code, inputCharacter, "3", numLength);
//            } else if (basRule.get(0).getRuleValue().equals("10")) {
//                Long numLength = basRule.get(0).getRuleLength();
//                String code = StringUtils.leftPad("10", numLength.intValue(), "0");
//                return saveAutoCode(basRule.get(0).getRuledetailId(), code, inputCharacter, "3", numLength);
//            }
//
//        }
//        return "没有找到对应的规则";
    }


    private String saveAutoCode(Long ruledetailId, String autoCode, String inputChar, String codeType, Long numLength) {
        Boolean flag = threadLocal.get(); //针对当前线程的判断 flag = true则数据库中没有当前规则的生成记录
        List<BaseAutoCode> list = baseAutoCodeMapper.selectAutoCodeListByName(ruledetailId, codeType);
        if (codeType.equals("3")) {
            if (list.size() == 0) {
                BaseAutoCode rs = new BaseAutoCode();
                rs.setAutoCode(autoCode);
                rs.setRuledetailId(ruledetailId);
                rs.setCodeType(codeType);
                baseAutoCodeMapper.insert(rs);
                return autoCode;
            } else if (codeType.equals("3")) {
                BaseAutoCode rs = new BaseAutoCode();
                String s = list.get(0).getAutoCode();
                BigDecimal bigDecimal = new BigDecimal(s);
                String code = StringUtils.leftPad(bigDecimal.add(new BigDecimal("1")).toString(), numLength.intValue(), "0");
                bigDecimal.add(new BigDecimal("1"));
                rs.setAutoCode(code);
                rs.setRuledetailId(ruledetailId);
                rs.setCodeType(codeType);
                baseAutoCodeMapper.insert(rs);
                return code;
            }
            return autoCode;
        } else {
            BaseAutoCode rs = new BaseAutoCode();
            rs.setAutoCode(autoCode);
            rs.setRuledetailId(ruledetailId);
            rs.setCodeType(codeType);
            baseAutoCodeMapper.insert(rs);
            return autoCode;
        }
    }
}
