package com.qs.mp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.admin.domain.TicketAwardsPrize;
import com.qs.mp.admin.domain.vo.TicketAwardsPrizeVO;
import com.qs.mp.admin.mapper.TicketAwardsPrizeMapper;
import com.qs.mp.admin.service.ITicketAwardsPrizeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.qs.mp.common.enums.AwardsLabelEnum;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 盲票盒奖项对应的奖品设置 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-02
 */
@Service
public class TicketAwardsPrizeServiceImpl extends ServiceImpl<TicketAwardsPrizeMapper, TicketAwardsPrize> implements
    ITicketAwardsPrizeService {

  @Override
  public List<TicketAwardsPrize> listByQueryWrapper(QueryWrapper<TicketAwardsPrize> queryWrapper) {
    queryWrapper.eq("t1.is_deleted", 0);
    return getBaseMapper().listByQueryWrapper(queryWrapper);
  }

  @Override
  public List<TicketAwardsPrizeVO> listPrizeVO(Wrapper<TicketAwardsPrize> queryWrapper) {
    return getBaseMapper().listPrizeVO(queryWrapper);
  }

  @Override
  public List<TicketAwardsPrizeVO> listPrizeVO(String boxId) {
    StringBuilder sortField = new StringBuilder("FIELD(t1.awards_label,'");
    int i = 0;
    for (AwardsLabelEnum awardsLabelEnum : AwardsLabelEnum.values()) {
      i++;
      if (i == AwardsLabelEnum.values().length) {
        sortField.append(awardsLabelEnum.getValue()).append("'");
      }else {
        sortField.append(awardsLabelEnum.getValue()).append("','");
      }

    }
    sortField.append(")");

    return listPrizeVO(new QueryWrapper<TicketAwardsPrize>()
            .eq("t1.box_id", boxId)
            .eq("t2.is_deleted", 0)
            .orderByAsc(sortField.toString())
            .orderByAsc("t1.sort")
            .orderByDesc("t2.sort_weight")
            .orderByDesc("t2.value"));
  }
}
