package com.zbf.system.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.zbf.system.mapper.TSeqMapper;
import com.zbf.system.domain.TSeq;
import com.zbf.system.service.ITSeqService;

/**
 * generateSeqService业务层处理
 *
 * @author wgs
 * @date 2024-06-21
 */
@Service
public class TSeqServiceImpl extends ServiceImpl<TSeqMapper, TSeq> implements ITSeqService //修改
{
    @Autowired
    private TSeqMapper tSeqMapper;

    /**
     * 生成临时流水号，数据库中的序列号不变
     *
     * @param preStr 流水号前缀
     * @param len    流水号长度
     * @return seqStr 流水号字符串
     */
    @Override
    public String generateTempSeq(String preStr, int len) {
        String seqStr = "";
        LambdaQueryWrapper<TSeq> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(TSeq::getK, preStr);
        List<TSeq> list = tSeqMapper.selectList(queryWrapper);
        TSeq tSeq;
        if (list.size() > 0) {
            tSeq = list.get(0);
            seqStr = preStr + StrUtil.padPre(tSeq.getV().toString(), len, '0');
        } else {
            seqStr = preStr + StrUtil.padPre("1", len, '0');
        }

        return seqStr;
    }
    /**
     * 生成正式流水号
     *
     * @param preStr 流水号前缀
     * @param len    流水号长度
     * @return seqStr 流水号字符串
     */
    @Override
    public String generateSeq(String preStr, int len) {
        String seqStr = "";
        LambdaQueryWrapper<TSeq> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(TSeq::getK, preStr);
        List<TSeq> list = tSeqMapper.selectList(queryWrapper);
        TSeq tSeq;
        if (list.size() > 0) {
            tSeq = list.get(0);
            tSeq.setV(tSeq.getV() + 1);
            tSeqMapper.updateById(tSeq);
        } else {
            tSeq = new TSeq();
            tSeq.setK(preStr);
            tSeq.setV(1L);
            tSeqMapper.insert(tSeq);
        }
        seqStr = preStr + StrUtil.padPre(tSeq.getV().toString(), len, '0');
        return seqStr;
    }

    /**
     * 查询generateSeq
     *
     * @param id generateSeq主键
     * @return generateSeq
     */
    @Override
    public TSeq selectTSeqById(Long id) {
        return tSeqMapper.selectTSeqById(id);
    }


    /**
     * 查询generateSeq列表
     *
     * @param tSeq generateSeq
     * @return generateSeq
     */
    @Override
    public List<TSeq> selectTSeqList(TSeq tSeq) {
        return tSeqMapper.selectTSeqList(tSeq);
    }

    /**
     * 新增generateSeq
     *
     * @param tSeq generateSeq
     * @return 结果
     */
    @Override
    public int insertTSeq(TSeq tSeq) {
        return tSeqMapper.insertTSeq(tSeq);
    }

    /**
     * 修改generateSeq
     *
     * @param tSeq generateSeq
     * @return 结果
     */
    @Override
    public int updateTSeq(TSeq tSeq) {
        return tSeqMapper.updateTSeq(tSeq);
    }

    /**
     * 批量删除generateSeq
     *
     * @param ids 需要删除的generateSeq主键
     * @return 结果
     */
    @Override
    public int deleteTSeqByIds(Long[] ids) {
        return tSeqMapper.deleteTSeqByIds(ids);
    }

    /**
     * 删除generateSeq信息
     *
     * @param id generateSeq主键
     * @return 结果
     */
    @Override
    public int deleteTSeqById(Long id) {
        return tSeqMapper.deleteTSeqById(id);
    }


    /**
     * 批量禁用
     *
     * @param ids generateSeq主键
     * @return 结果
     */
    @Override
    public int disabledTSeqByIds(Long[] ids) {
        return tSeqMapper.disabledTSeqByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param ids generateSeq主键
     * @return 结果
     */
    @Override
    public int enableTSeqByIds(Long[] ids) {
        return tSeqMapper.enableTSeqByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tSeq 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TSeq tSeq) {
        return tSeqMapper.updateTSeq(tSeq);
    }

}
