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.UserBalanceInMerchantMapper;
import com.gxwebsoft.shop.service.UserBalanceInMerchantService;
import com.gxwebsoft.shop.entity.UserBalanceInMerchant;
import com.gxwebsoft.shop.param.UserBalanceInMerchantParam;
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 UserBalanceInMerchantServiceImpl extends ServiceImpl<UserBalanceInMerchantMapper, UserBalanceInMerchant> implements UserBalanceInMerchantService {

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

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

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

    @Override
    public UserBalanceInMerchant getBalance(Integer merchantId, Integer userId) {
        return getOne(
                new LambdaQueryWrapper<UserBalanceInMerchant>()
                        .eq(UserBalanceInMerchant::getMerchantId, merchantId)
                        .eq(UserBalanceInMerchant::getUserId, userId)
                        .last("limit 1")
        );
    }

}
