package com.yc.ship.module.product.service.productGroup;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupPageReqVO;
import com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupSaveReqVO;
import com.yc.ship.module.product.controller.admin.productSpu.skuVO.ProductSkuSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.productGroup.ProductGroupDO;
import com.yc.ship.module.product.dal.dataobject.productSku.ProductSkuDO;
import com.yc.ship.module.product.dal.mysql.productGroup.ProductGroupMapper;
import com.yc.ship.module.product.dal.mysql.productSku.ProductSkuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.GROUP_NOT_EXISTS;


/**
 * 产品组合 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProductGroupServiceImpl implements ProductGroupService {

    @Resource
    private ProductGroupMapper productGroupMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;

    @Override
    public Long createGroup(ProductGroupSaveReqVO createReqVO) {
        // 插入
        ProductGroupDO group = BeanUtils.toBean(createReqVO, ProductGroupDO.class);
        productGroupMapper.insert(group);
        // 返回
        return group.getId();
    }

    @Override
    public void updateGroup(ProductGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateGroupExists(updateReqVO.getId());
        // 更新
        ProductGroupDO updateObj = BeanUtils.toBean(updateReqVO, ProductGroupDO.class);
        productGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteGroup(Long id) {
        // 校验存在
        validateGroupExists(id);
        // 删除
        productGroupMapper.deleteById(id);
    }

    private void validateGroupExists(Long id) {
        if (productGroupMapper.selectById(id) == null) {
            throw exception(GROUP_NOT_EXISTS);
        }
    }

    @Override
    public ProductGroupDO getGroup(Long id) {
        return productGroupMapper.selectById(id);
    }

    @Override
    public PageResult<ProductGroupDO> getGroupPage(ProductGroupPageReqVO pageReqVO) {
        return productGroupMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional
    public void saveSkuAndGroupList(List<ProductGroupSaveReqVO> specGroupList, List<ProductSkuSaveReqVO> productSkuList, Long productId) {
        List<ProductGroupDO> groupDOs = BeanUtils.toBean(specGroupList, ProductGroupDO.class);
        groupDOs.forEach(group -> {
            group.setId(IdWorker.getId(group));
            group.setProductId(productId);
            group.setDeleted(false);
        });
        productGroupMapper.update(new LambdaUpdateWrapper<ProductGroupDO>()
                .eq(ProductGroupDO::getProductId, productId)
                .set(ProductGroupDO::getDeleted, 1)
        );
        productGroupMapper.insertBatch(groupDOs);

        List<ProductSkuDO> newSkuList = BeanUtils.toBean(productSkuList, ProductSkuDO.class);
        newSkuList.forEach(sku -> {
            sku.setId(IdWorker.getId(sku));
            sku.setProductId(productId);
            sku.setDeleted(false);
        });
        productSkuMapper.update(new LambdaUpdateWrapper<ProductSkuDO>()
                .set(ProductSkuDO::getDeleted, 1)
                .eq(ProductSkuDO::getProductId, productId)
        );
        productSkuMapper.insertBatch(newSkuList);
    }

}