package com.ruoyi.brewery.service.impl;

import java.util.List;

import com.ruoyi.brewery.domain.WyRedeemOrder;
import com.ruoyi.common.utils.CodeGeneratorUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyBreweryMapper;
import com.ruoyi.brewery.domain.WyBrewery;
import com.ruoyi.brewery.service.IWyBreweryService;

/**
 * 品牌方Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Service
public class WyBreweryServiceImpl implements IWyBreweryService 
{
    @Autowired
    private WyBreweryMapper wyBreweryMapper;

    /**
     * 查询品牌方
     * 
     * @param id 品牌方主键
     * @return 品牌方
     */
    @Override
    public WyBrewery selectWyBreweryById(Long id)
    {
        return wyBreweryMapper.selectWyBreweryById(id);
    }

    /**
     * 查询品牌方列表
     * 
     * @param wyBrewery 品牌方
     * @return 品牌方
     */
    @Override
    public List<WyBrewery> selectWyBreweryList(WyBrewery wyBrewery)
    {
        return wyBreweryMapper.selectWyBreweryList(wyBrewery);
    }

    /**
     * 新增品牌方
     * 
     * @param wyBrewery 品牌方
     * @return 结果
     */
    @Override
    public int insertWyBrewery(WyBrewery wyBrewery)
    {
        String sn = generateSn();
        wyBrewery.setBrandCode(sn);
        wyBrewery.setCreateTime(DateUtils.getNowDate());
        return wyBreweryMapper.insertWyBrewery(wyBrewery);
    }

    /**
     * 修改品牌方
     * 
     * @param wyBrewery 品牌方
     * @return 结果
     */
    @Override
    public int updateWyBrewery(WyBrewery wyBrewery)
    {
        wyBrewery.setUpdateTime(DateUtils.getNowDate());
        return wyBreweryMapper.updateWyBrewery(wyBrewery);
    }

    /**
     * 批量删除品牌方
     * 
     * @param ids 需要删除的品牌方主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryByIds(Long[] ids)
    {
        return wyBreweryMapper.deleteWyBreweryByIds(ids);
    }

    /**
     * 删除品牌方信息
     * 
     * @param id 品牌方主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryById(Long id)
    {
        return wyBreweryMapper.deleteWyBreweryById(id);
    }

    /**
     * 生成编号
     */
    private String generateSn() {
        final Integer integer = wyBreweryMapper.selectBreweryCount();
        String sn = CodeGeneratorUtils.generateBreweryCode(integer);
        //生成订单号
        final WyBrewery query = new WyBrewery();
        query.setBrandCode(sn);
        final List<WyBrewery> breweries = selectWyBreweryList(query);
        if (breweries.size() > 0) {
            return generateSn();
        }
        return sn;
    }
}
