package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ChannelProd;
import com.yami.shop.bean.model.Label;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopSku;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.*;
import com.yami.shop.service.ShopSkuService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ShopSkuServiceImpl extends ServiceImpl<ShopSkuMapper, ShopSku> implements ShopSkuService {

    private final ShopSkuMapper shopProdMapper;
    private final ChannelProdMapper channelProdMapper;
    private final SkuMapper skuMapper;
    private final ShopDetailMapper shopDetailMapper;


    @Override
    public void insertSku(ShopSku shopSku) {
        shopProdMapper.insert(shopSku);
    }

    @Override
    public IPage<ShopSku> shopSkuPage(Integer current, Integer size, ShopSku shopSku) {
        PageParam<ShopSku> pageParam = new PageParam<>();
        pageParam.setCurrent(current);
        pageParam.setSize(size);

        LambdaQueryWrapper<ShopSku> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ObjectUtils.isNotEmpty(shopSku.getShopId()),ShopSku::getShopId, shopSku.getShopId());
                queryWrapper.eq(ObjectUtils.isNotEmpty(shopSku.getSkuId()),ShopSku::getSkuId, shopSku.getSkuId());
                queryWrapper.eq(ShopSku::getIsDelete, 0);
        IPage<ShopSku> page = shopProdMapper.selectPage(pageParam, queryWrapper);


        //循环获取sku和渠道价格设置信息
        for (ShopSku record : page.getRecords()) {
            ShopDetail shopDetail = shopDetailMapper.selectById(record.getShopId());
            record.setShopName(shopDetail.getShopName());
            record.setSku(skuMapper.selectById(record.getSkuId()));
            List<ChannelProd> channelProds = channelProdMapper.selectListAll(record.getSkuId(), record.getShopId());
            record.setChannelProdList(channelProds);
        }
        return page;
    }
}
