package com.ruoyi.brewery.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.ruoyi.bar.service.impl.WyBeerQrcodeScanLogServiceImpl;
import com.ruoyi.brewery.domain.PopularStyleVO;
import com.ruoyi.brewery.domain.WyActivity;
import com.ruoyi.brewery.domain.WyBrewery;
import com.ruoyi.brewery.service.IWyActivityBeerService;
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.WyBeerMapper;
import com.ruoyi.brewery.domain.WyBeer;
import com.ruoyi.brewery.service.IWyBeerService;

import javax.annotation.Resource;

/**
 * 啤酒Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-21
 */
@Service
public class WyBeerServiceImpl implements IWyBeerService {
    @Autowired
    private WyBeerMapper wyBeerMapper;
    @Resource
    private IWyActivityBeerService wyActivityBeerService;
    @Resource
    private WyBeerQrcodeScanLogServiceImpl wyBeerQrcodeScanLogService;

    /**
     * 查询啤酒
     *
     * @param id 啤酒主键
     * @return 啤酒
     */
    @Override
    public WyBeer selectWyBeerById(Long id) {
        return wyBeerMapper.selectWyBeerById(id);
    }

    /**
     * 查询啤酒列表
     *
     * @param wyBeer 啤酒
     * @return 啤酒
     */
    @Override
    public List<WyBeer> selectWyBeerList(WyBeer wyBeer) {
        return wyBeerMapper.selectWyBeerList(wyBeer);
    }

    @Override
    public List<WyBeer> top10BeersList(WyBeer wyBeer){
        HashMap<String, Object> params = new HashMap<>();
        params.put("orderByColumn", "beer_overall_rating");
        params.put("isAsc", "desc");
        params.put("pageNum", 1);
        params.put("pageSize", 10);
        wyBeer.setParams(params);
        return wyBeerMapper.selectWyBeerList(wyBeer);
    }

    @Override
    public List<WyBeer> selectWyBeerListWithData(WyBeer wyBeer) {
        final List<WyBeer> wyBeers = wyBeerMapper.selectWyBeerList(wyBeer);
        HashMap<String, Object> params = new HashMap<>();
        params.put("startDate", DateUtils.parseDateToStr("yyyy-MM-dd", DateUtils.addDays(DateUtils.getNowDate(), -30)));
        params.put("endDate", DateUtils.parseDateToStr("yyyy-MM-dd", DateUtils.getNowDate()));
        wyBeers.forEach(beer -> {
            final int beerScanCount = wyBeerQrcodeScanLogService.getBeerScanCount(beer.getId(), params);
            beer.setScanCount(beerScanCount);
            final List<WyActivity> wyActivities = wyActivityBeerService.selectWyActivityListByBeerId(beer.getId(), beer.getBreweryId(), 1);
            if (Objects.nonNull(wyActivities) && wyActivities.size() > 0) {
                beer.setActivityCount(wyActivities.size());
            } else {
                beer.setActivityCount(0);
            }
        });
        return wyBeers;
    }

    @Override
    public List<PopularStyleVO> selectPopularStyles() {
        return wyBeerMapper.selectPopularStyles();
    }

    /**
     * 新增啤酒
     *
     * @param wyBeer 啤酒
     * @return 结果
     */
    @Override
    public int insertWyBeer(WyBeer wyBeer) {
        wyBeer.setBeerSn(generateSn());
        wyBeer.setCreateTime(DateUtils.getNowDate());
        return wyBeerMapper.insertWyBeer(wyBeer);
    }

    /**
     * 修改啤酒
     *
     * @param wyBeer 啤酒
     * @return 结果
     */
    @Override
    public int updateWyBeer(WyBeer wyBeer) {
        wyBeer.setUpdateTime(DateUtils.getNowDate());
        return wyBeerMapper.updateWyBeer(wyBeer);
    }

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

    /**
     * 删除啤酒信息
     *
     * @param id 啤酒主键
     * @return 结果
     */
    @Override
    public int deleteWyBeerById(Long id) {
        return wyBeerMapper.deleteWyBeerById(id);
    }

    @Override
    public List<WyBeer> seleteWyBeerByIds(Long[] ids) {
        return wyBeerMapper.seleteWyBeerByIds(ids);
    }

    /**
     * 生成编号
     */
    private String generateSn() {
        final Integer integer = wyBeerMapper.selectBeerCount();
        String sn = CodeGeneratorUtils.generateBeerCode(integer);
        //生成订单号
        final WyBeer query = new WyBeer();
        query.setBeerSn(sn);
        final List<WyBeer> beers = selectWyBeerList(query);
        if (beers.size() > 0) {
            return generateSn();
        }
        return sn;
    }
}
