package com.hzs.system.config.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.system.config.BdBarCode;
import com.hzs.common.domain.system.config.ext.BdBarCodeExt;
import com.hzs.system.config.mapper.BdBarCodeMapper;
import com.hzs.system.config.service.IBdBarCodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 条码配置 服务实现类
 *
 * @author hzs
 * @since 2022-10-17
 */
@Service
public class BdBarCodeServiceImpl extends ServiceImpl<BdBarCodeMapper, BdBarCode> implements IBdBarCodeService {

    /**
     * @description: 查询条形码配置列表
     * @author: zhang jing
     * @date: 2022/10/18 17:37
     * @param: [barCode]
     * @return: java.util.List<com.hzs.common.domain.system.config.BdBarCode>
     **/
    @Override
    public List<BdBarCodeExt> queryBarCodeList(BdBarCodeExt barCodeExt) {
        return baseMapper.queryBarCodeList(barCodeExt);
    }

    /**
     * @description: 查询单条条形码配置信息
     * @author: zhang jing
     * @date: 2022/10/18 17:37
     * @param: [barCode]
     * @return: com.hzs.common.domain.system.config.BdBarCode
     **/
    public BdBarCode getOneBarCode(Long pkId) {
        return baseMapper.getOneBarCode(pkId);
    }

    /**
     * 定时初始化条形码配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updatWarehouseLog() {
        /**
         * 查询初始化条形码配置,将当前尾号初始化为初始尾号
         */
        try {
            BdBarCodeExt barCode = new BdBarCodeExt();
            barCode.setIsInitialize(EYesNo.YES.getIntValue());
            List<BdBarCodeExt> list = baseMapper.queryBarCodeList(barCode);
            if (!CollectionUtils.isEmpty(list)) {
                for (BdBarCodeExt bbc : list) {
                    bbc.setCurrentNumber(bbc.getEndNumber());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public int updateUsedBarCode(List<Integer> idList, Integer type, Long userId, Integer pkCountry) {
        return baseMapper.updateUsedBarCode(idList, type, userId, pkCountry);
    }

    /**
     * @description: 追溯码查询特殊产品
     * @author: zhang jing
     * @date: 2023/8/14 9:46
     * @param: [barCodeExt]
     * @return: com.hzs.common.domain.system.config.ext.BdBarCodeExt
     **/
    @Override
    public BdBarCodeExt querySpecialProduct(BdBarCode barCode) {
        return baseMapper.querySpecialProduct(barCode);
    }

}
