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.GoodsIncomeConfigMapper;
import com.gxwebsoft.shop.service.GoodsIncomeConfigService;
import com.gxwebsoft.shop.entity.GoodsIncomeConfig;
import com.gxwebsoft.shop.param.GoodsIncomeConfigParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.service.GoodsService;
import com.gxwebsoft.shop.service.GoodsSkuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分润配置Service实现
 *
 * @author 科技小王子
 * @since 2024-10-06 17:55:50
 */
@Service
public class GoodsIncomeConfigServiceImpl extends ServiceImpl<GoodsIncomeConfigMapper, GoodsIncomeConfig> implements GoodsIncomeConfigService {
    @Resource
    private GoodsSkuService goodsSkuService;
    @Resource
    private GoodsService goodsService;

    @Override
    public PageResult<GoodsIncomeConfig> pageRel(GoodsIncomeConfigParam param) {
        PageParam<GoodsIncomeConfig, GoodsIncomeConfigParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<GoodsIncomeConfig> list = baseMapper.selectPageRel(page, param);
        for (GoodsIncomeConfig goodsIncomeConfig : list) {
            goodsIncomeConfig.setGoods(goodsService.getById(goodsIncomeConfig.getGoodsId()));
            if (goodsIncomeConfig.getSkuId() != null) {
                goodsIncomeConfig.setGoodsSku(goodsSkuService.getById(goodsIncomeConfig.getSkuId()));
            }
        }
        return new PageResult<>(list, page.getTotal());
    }

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

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

    @Override
    public GoodsIncomeConfig check(Integer goodsId, Integer skuId, String merchantShopType) {
        LambdaQueryWrapper<GoodsIncomeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsIncomeConfig::getGoodsId, goodsId);
        wrapper.eq(GoodsIncomeConfig::getMerchantShopType, merchantShopType);
        if (skuId != null) wrapper.eq(GoodsIncomeConfig::getSkuId, skuId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

}
