package cn.ysk.cashier.mybatis.service.impl;

import cn.ysk.cashier.dto.shop.TbShopSongOrderQueryCriteria;
import cn.ysk.cashier.dto.shop.TbShopSongQueryCriteria;
import cn.ysk.cashier.mybatis.entity.TbShopSong;
import cn.ysk.cashier.mybatis.mapper.TbShopSongMapper;
import cn.ysk.cashier.mybatis.vo.SingerSongVo;
import cn.ysk.cashier.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ysk.cashier.mybatis.mapper.TbShopSongOrderMapper;
import cn.ysk.cashier.mybatis.entity.TbShopSongOrder;
import cn.ysk.cashier.mybatis.service.TbShopSongOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * (TbShopSongOrder)表服务实现类
 *
 * @author ww
 * @since 2024-07-08 09:24:27
 */
@Service("tbShopSongOrderService")
public class TbShopSongOrderServiceImpl extends ServiceImpl<TbShopSongOrderMapper, TbShopSongOrder> implements TbShopSongOrderService {

    @Autowired
    private TbShopSongOrderMapper tbShopSongOrderMapper;
    @Override
    public Map<String, Object> queryAll(TbShopSongOrderQueryCriteria criteria) {
        Page<TbShopSongOrder> page = new Page<>(
                criteria.getPage(),
                criteria.getSize());
        QueryWrapper<TbShopSongOrder> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(criteria.getShopId())) {
            wrapper.eq("shop_id", criteria.getShopId());
        }
        if (StringUtils.isNotBlank(criteria.getOrderNo())) {
            wrapper.eq("order_no", criteria.getOrderNo());
        }
        if (criteria.getState() != null) {
            wrapper.eq("state", criteria.getState());
        }
        if (StringUtils.isNotBlank(criteria.getName())) {
            wrapper.like("song_name", criteria.getName());
        }
        wrapper.orderByDesc("create_time");
        Page<TbShopSongOrder> tbShopSongPage = tbShopSongOrderMapper.selectPage(page, wrapper);
        return PageUtil.toPage(tbShopSongPage.getRecords(), tbShopSongPage.getTotal());
    }

    @Override
    public Map<String, Object> singerSongs(Integer shopId) {
        Map<String,Object> map = new LinkedHashMap<>(3);
        map.put("shopId",shopId);
        List<SingerSongVo> byState = tbShopSongOrderMapper.findByState(2, shopId);
        if(CollectionUtils.isEmpty(byState)){
            map.put("afoot","");//演唱中
        }else {
            map.put("afoot",byState.get(0));//演唱中
        }
        map.put("Waiting",tbShopSongOrderMapper.findByState(1,shopId));
        return map;
    }

    @Override
    @Transactional
    public void next(Integer shopId) {
        tbShopSongOrderMapper.upState(shopId);
        tbShopSongOrderMapper.setDuringSinging(shopId);
    }
}

