package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.OrderGoodsMapper;
import com.gxwebsoft.shop.service.OrderGoodsService;
import com.gxwebsoft.shop.entity.OrderGoods;
import com.gxwebsoft.shop.param.OrderGoodsParam;
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-26 15:42:03
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements OrderGoodsService {

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

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

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

    @Override
    public int countByMerchant(Integer merchantId) {
        OrderGoods orderGoods = getOne(
                new QueryWrapper<OrderGoods>()
                        .select("SUM(`total_num`) AS `total_num`")
                        .eq("merchant_id", merchantId)
        );
        if (orderGoods == null) return 0;
        return orderGoods.getTotalNum();
    }

}
