package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.UserGoodsInMerchantMapper;
import com.gxwebsoft.shop.service.UserGoodsInMerchantService;
import com.gxwebsoft.shop.entity.UserGoodsInMerchant;
import com.gxwebsoft.shop.param.UserGoodsInMerchantParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户在商家中的代金券Service实现
 *
 * @author 科技小王子
 * @since 2024-11-20 01:55:04
 */
@Service
public class UserGoodsInMerchantServiceImpl extends ServiceImpl<UserGoodsInMerchantMapper, UserGoodsInMerchant> implements UserGoodsInMerchantService {

    @Override
    public PageResult<UserGoodsInMerchant> pageRel(UserGoodsInMerchantParam param) {
        PageParam<UserGoodsInMerchant, UserGoodsInMerchantParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<UserGoodsInMerchant> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<UserGoodsInMerchant> listRel(UserGoodsInMerchantParam param) {
        List<UserGoodsInMerchant> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<UserGoodsInMerchant, UserGoodsInMerchantParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public UserGoodsInMerchant getByIdRel(Integer id) {
        UserGoodsInMerchantParam param = new UserGoodsInMerchantParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public UserGoodsInMerchant infoInMerchant(Integer packageId, Integer userId){
        return getOne(
                new LambdaQueryWrapper<UserGoodsInMerchant>()
                        .eq(UserGoodsInMerchant::getUserId, userId)
                        .eq(UserGoodsInMerchant::getPackageId, packageId)
                        .last("limit 1")
        );
    }
}
