package com.fjhx.item.service.product.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.entity.product.po.ProductSpu;
import com.fjhx.item.entity.product.vo.ProductInfoVo;
import com.fjhx.item.mapper.product.ProductSpuMapper;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.item.service.product.ProductSpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.item.entity.product.vo.ProductSpuVo;
import com.fjhx.item.entity.product.dto.ProductSpuSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.item.entity.product.dto.ProductSpuDto;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 产品spu 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-11
 */
//@DS(SourceConstant.ITEM)
@Service
public class ProductSpuServiceImpl extends ServiceImpl<ProductSpuMapper, ProductSpu> implements ProductSpuService {

    @Autowired
    ProductInfoService productInfoService;

    @Override
    public Page<ProductSpuVo> getPage(ProductSpuSelectDto dto) {
        IWrapper<ProductSpu> wrapper = getWrapper();
        wrapper.orderByDesc("ps", ProductSpu::getId);
        if(ObjectUtil.isNotEmpty(dto.getKeyword())){
            wrapper.and(q->q.like("ps",ProductSpu::getCode,dto.getKeyword()))
                    .or().like("ps",ProductSpu::getName,dto.getKeyword())
                    .or().like("ps",ProductSpu::getRemark,dto.getKeyword())
            ;
        }
        wrapper.groupBy("ps.id");
        Page<ProductSpuVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public ProductSpuVo detail(Long id) {
        ProductSpu ProductSpu = this.getById(id);
        ProductSpuVo result = BeanUtil.toBean(ProductSpu, ProductSpuVo.class);
        List<ProductInfo> list = productInfoService.list(q -> q.eq(ProductInfo::getProductSpuId, id));
        result.setProductInfoList(list);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ProductSpuDto productSpuDto) {
        this.save(productSpuDto);
        List<ProductInfo> productInfos = productSpuDto.getProductInfos();
        for (ProductInfo productInfo: productInfos){
            productInfo.setProductSpuId(productSpuDto.getId());
        }
        productInfoService.updateBatchById(productInfos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(ProductSpuDto productSpuDto) {
        this.updateById(productSpuDto);
        List<ProductInfo> productInfos = productSpuDto.getProductInfos();
        if(ObjectUtil.isNotEmpty(productInfos)){
            //移除被删除的条目
            List<Long> productIds = productInfos.stream().map(ProductInfo::getId).collect(Collectors.toList());
            List<ProductInfo> productInfos1 = productInfoService.list(q->q.notIn(ProductInfo::getId,productIds)
                    .eq(ProductInfo::getProductSpuId,productSpuDto.getId()));
            for (ProductInfo productInfo :productInfos1){
                productInfo.setProductSpuId(-1l);
            }
            productInfoService.updateBatchById(productInfos1);
            //添加新条目
            for (ProductInfo productInfo :productInfos){
                productInfo.setProductSpuId(productSpuDto.getId());
            }
            productInfoService.updateBatchById(productInfos);
        }
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
