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.GoodsRoleCommissionMapper;
import com.gxwebsoft.shop.service.GoodsRoleCommissionService;
import com.gxwebsoft.shop.entity.GoodsRoleCommission;
import com.gxwebsoft.shop.param.GoodsRoleCommissionParam;
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-12-02 12:43:40
 */
@Service
public class GoodsRoleCommissionServiceImpl extends ServiceImpl<GoodsRoleCommissionMapper, GoodsRoleCommission> implements GoodsRoleCommissionService {

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

    @Override
    public List<GoodsRoleCommission> listRel(GoodsRoleCommissionParam param) {
        List<GoodsRoleCommission> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<GoodsRoleCommission, GoodsRoleCommissionParam> page = new PageParam<>();
        return page.sortRecords(list);
    }

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

    @Override
    public GoodsRoleCommission getByRoleNGoods(Integer roleId, Integer goodsId) {
        return getOne(
                new LambdaQueryWrapper<GoodsRoleCommission>()
                        .eq(GoodsRoleCommission::getRoleId, roleId)
                        .eq(GoodsRoleCommission::getGoodsId, goodsId)
                        .last("limit 1")
        );
    }

}
