package com.gxwebsoft.glt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.glt.mapper.GltUserTicketMapper;
import com.gxwebsoft.glt.service.GltUserTicketService;
import com.gxwebsoft.glt.entity.GltUserTicket;
import com.gxwebsoft.glt.param.GltUserTicketParam;
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 2026-02-03 18:55:55
 */
@Service
public class GltUserTicketServiceImpl extends ServiceImpl<GltUserTicketMapper, GltUserTicket> implements GltUserTicketService {

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

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

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

    @Override
    public Integer sumAvailableQtyByUserId(Integer userId, Integer tenantId) {
        Integer availableQty = baseMapper.sumAvailableQtyByUserId(userId, tenantId);
        return availableQty == null ? 0 : availableQty;
    }

}
