package com.ruoyi.brewery.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.brewery.domain.WyBeerQrcode;
import com.ruoyi.brewery.domain.WyGoods;
import com.ruoyi.brewery.mapper.WyBeerQrcodeMapper;
import com.ruoyi.common.utils.CodeGeneratorUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyBeerQrcodeBatchMapper;
import com.ruoyi.brewery.domain.WyBeerQrcodeBatch;
import com.ruoyi.brewery.service.IWyBeerQrcodeBatchService;

/**
 * 批次Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Service
public class WyBeerQrcodeBatchServiceImpl implements IWyBeerQrcodeBatchService 
{
    @Autowired
    private WyBeerQrcodeBatchMapper wyBeerQrcodeBatchMapper;
    @Autowired
    private WyBeerQrcodeMapper wyBeerQrcodeMapper;
    /**
     * 查询批次
     * 
     * @param id 批次主键
     * @return 批次
     */
    @Override
    public WyBeerQrcodeBatch selectWyBeerQrcodeBatchById(Long id)
    {
        final WyBeerQrcodeBatch batch = wyBeerQrcodeBatchMapper.selectWyBeerQrcodeBatchById(id);
        final WyBeerQrcode wyBeerQrcode = new WyBeerQrcode();
        wyBeerQrcode.setBatchId(id);
        final List<WyBeerQrcode> wyBeerQrcodes = wyBeerQrcodeMapper.selectWyBeerQrcodeList(wyBeerQrcode);
        batch.setWyBeerQrcodeList(wyBeerQrcodes);

        batch.setPrintCount(wyBeerQrcodes.size());

        wyBeerQrcode.setQrcodeStatus(1);
        final List<WyBeerQrcode> checkCodes = wyBeerQrcodeMapper.selectWyBeerQrcodeList(wyBeerQrcode);
        batch.setCheckCount(checkCodes.size());

        wyBeerQrcode.setQrcodeStatus(2);
        final List<WyBeerQrcode> invalidCodes = wyBeerQrcodeMapper.selectWyBeerQrcodeList(wyBeerQrcode);
        batch.setInvalidCount(invalidCodes.size());
        return batch;
    }

    /**
     * 查询批次列表
     * 
     * @param wyBeerQrcodeBatch 批次
     * @return 批次
     */
    @Override
    public List<WyBeerQrcodeBatch> selectWyBeerQrcodeBatchList(WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        final List<WyBeerQrcodeBatch> wyBeerQrcodeBatches = wyBeerQrcodeBatchMapper.selectWyBeerQrcodeBatchList(wyBeerQrcodeBatch);
        wyBeerQrcodeBatches.forEach(batch -> {
            batch.setPrintCount(Math.toIntExact(batch.getGenerateNum()));
        });
        return wyBeerQrcodeBatches;
    }

    @Override
    public List<WyBeerQrcodeBatch> selectWyBeerQrcodeBatchListWithData(WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        final List<WyBeerQrcodeBatch> wyBeerQrcodeBatches = wyBeerQrcodeBatchMapper.selectWyBeerQrcodeBatchList(wyBeerQrcodeBatch);
        wyBeerQrcodeBatches.forEach(batch -> {
            final WyBeerQrcode wyBeerQrcode = new WyBeerQrcode();
            wyBeerQrcode.setBatchId(batch.getId());
            final List<WyBeerQrcode> wyBeerQrcodes = wyBeerQrcodeMapper.selectWyBeerQrcodeList(wyBeerQrcode);
            batch.setWyBeerQrcodeList(wyBeerQrcodes);

            batch.setPrintCount(wyBeerQrcodes.size());

            wyBeerQrcode.setQrcodeStatus(1);
            final List<WyBeerQrcode> checkCodes = wyBeerQrcodeMapper.selectWyBeerQrcodeList(wyBeerQrcode);
            batch.setCheckCount(checkCodes.size());

            wyBeerQrcode.setQrcodeStatus(2);
            final List<WyBeerQrcode> invalidCodes = wyBeerQrcodeMapper.selectWyBeerQrcodeList(wyBeerQrcode);
            batch.setInvalidCount(invalidCodes.size());

        });
        return wyBeerQrcodeBatches;
    }

    /**
     * 新增批次
     * 
     * @param wyBeerQrcodeBatch 批次
     * @return 结果
     */
    @Override
    public int insertWyBeerQrcodeBatch(WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        if (wyBeerQrcodeBatch.getBeerId() == null) {
            throw new IllegalArgumentException("酒款ID不能为空");
        }
        if (wyBeerQrcodeBatch.getProductionDate() == null) {
            throw new IllegalArgumentException("生产日期不能为空");
        }
        if (wyBeerQrcodeBatch.getPrintCount() == null) {
            throw new IllegalArgumentException("打印数量不能为空");
        }
        if (wyBeerQrcodeBatch.getPrintCount()<=0 || wyBeerQrcodeBatch.getPrintCount()>100) {
            throw new IllegalArgumentException("打印数量必须在1-100之间");
        }
        wyBeerQrcodeBatch.setGenerateNum(Long.valueOf(wyBeerQrcodeBatch.getPrintCount()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        final Date productionDate = wyBeerQrcodeBatch.getProductionDate();
        String timestamp = sdf.format(productionDate);
        wyBeerQrcodeBatch.setBatchSn(generateSn(timestamp, wyBeerQrcodeBatch.getPrintCount()));
        wyBeerQrcodeBatch.setCreateTime(DateUtils.getNowDate());
        return wyBeerQrcodeBatchMapper.insertWyBeerQrcodeBatch(wyBeerQrcodeBatch);
    }

    /**
     * 修改批次
     * 
     * @param wyBeerQrcodeBatch 批次
     * @return 结果
     */
    @Override
    public int updateWyBeerQrcodeBatch(WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        wyBeerQrcodeBatch.setUpdateTime(DateUtils.getNowDate());
        return wyBeerQrcodeBatchMapper.updateWyBeerQrcodeBatch(wyBeerQrcodeBatch);
    }

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

    /**
     * 删除批次信息
     * 
     * @param id 批次主键
     * @return 结果
     */
    @Override
    public int deleteWyBeerQrcodeBatchById(Long id)
    {
        return wyBeerQrcodeBatchMapper.deleteWyBeerQrcodeBatchById(id);
    }

    /**
     * 生成编号
     */
    private String generateSn(String yyyyMMdd, Integer printCount) {
        final Integer integer = wyBeerQrcodeBatchMapper.selectBatchCount();
        String sn = CodeGeneratorUtils.generateQrBatchCode("A",yyyyMMdd,integer,printCount);
        //生成订单号
        final WyBeerQrcodeBatch query = new WyBeerQrcodeBatch();
        query.setBatchSn(sn);
        final List<WyBeerQrcodeBatch> data = selectWyBeerQrcodeBatchList(query);
        if (data.size() > 0) {
            return generateSn(yyyyMMdd,printCount);
        }
        return sn;
    }
}
