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.UserBalanceLogMapper;
import com.gxwebsoft.shop.service.UserBalanceLogService;
import com.gxwebsoft.shop.entity.UserBalanceLog;
import com.gxwebsoft.shop.param.UserBalanceLogParam;
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-05-29 15:57:44
 */
@Service
public class UserBalanceLogServiceImpl extends ServiceImpl<UserBalanceLogMapper, UserBalanceLog> implements UserBalanceLogService {

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

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

    @Override
    public UserBalanceLog getByIdRel(Integer logId) {
        UserBalanceLogParam param = new UserBalanceLogParam();
        param.setLogId(logId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public List<UserBalanceLog> unSettleList(Integer orderId) {
        return list(
                new LambdaQueryWrapper<UserBalanceLog>()
                        .eq(UserBalanceLog::getOrderId, orderId)
                        .eq(UserBalanceLog::getStatus, 0)
        );
    }

}
