package cn.iocoder.yudao.module.wms.service.productcategory;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.productcategory.vo.ProductCategoryPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.productcategory.vo.ProductCategorySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.productcategory.ProductCategoryDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.productcategoryproperty.ProductCategoryPropertyDO;
import cn.iocoder.yudao.module.wms.dal.mysql.productcategory.ProductCategoryMapper;
import cn.iocoder.yudao.module.wms.dal.mysql.productcategoryproperty.ProductCategoryPropertyMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 产品分类 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductCategoryPropertyMapper productCategoryPropertyMapper;

    @Override
    public Long createProductCategory(ProductCategorySaveReqVO createReqVO) {
        // 插入
        ProductCategoryDO productCategory = BeanUtils.toBean(createReqVO, ProductCategoryDO.class);
        productCategoryMapper.insert(productCategory);

        List<ProductCategoryPropertyDO> items = BeanUtils.toBean(createReqVO.getItems(), ProductCategoryPropertyDO.class);
        for (ProductCategoryPropertyDO item : items) {
            item.setProductCategoryId(productCategory.getId());
        }
        productCategoryPropertyMapper.insertBatch(items);
        // 返回
        return productCategory.getId();
    }

    @Override
    public void updateProductCategory(ProductCategorySaveReqVO updateReqVO) {
        // 更新
        ProductCategoryDO updateObj = BeanUtils.toBean(updateReqVO, ProductCategoryDO.class);
        productCategoryMapper.updateById(updateObj);


        List<ProductCategoryPropertyDO> list = BeanUtils.toBean(updateReqVO.getItems(), ProductCategoryPropertyDO.class);
        // -- 查询旧的
        List<ProductCategoryPropertyDO> oldList = productCategoryPropertyMapper.selectList(
                new LambdaQueryWrapper<ProductCategoryPropertyDO>().eq(ProductCategoryPropertyDO::getProductCategoryId, updateObj.getId())
        );
        List<Long> oldIds = oldList.stream().map(ProductCategoryPropertyDO::getId).collect(Collectors.toList());
        List<Long> newIds = list.stream().map(ProductCategoryPropertyDO::getId).filter(Objects::nonNull).collect(Collectors.toList());
        List<Long> delIds = CollectionUtil.subtractToList(oldIds, newIds);
        if (!delIds.isEmpty()) {
            productCategoryPropertyMapper.deleteBatchIds(delIds);
        }
        List<ProductCategoryPropertyDO> saveList = list.stream().filter(item -> item.getId() == null).collect(Collectors.toList());
        if (!saveList.isEmpty()) {
            for (ProductCategoryPropertyDO productCategoryPropertyDO : saveList) {
                productCategoryPropertyDO.setProductCategoryId(updateReqVO.getId());
            }
            productCategoryPropertyMapper.insertBatch(saveList);
        }
    }

    @Override
    public void deleteProductCategory(Long id) {
        // 删除
        productCategoryMapper.deleteById(id);
        productCategoryPropertyMapper.delete(new LambdaQueryWrapper<ProductCategoryPropertyDO>()
                .eq(ProductCategoryPropertyDO::getProductCategoryId, id));
    }

    @Override
    public ProductCategoryDO getProductCategory(Long id) {
        return productCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<ProductCategoryDO> getProductCategoryPage(ProductCategoryPageReqVO pageReqVO) {
        return productCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductCategoryDO> getByIndustryCategoryId(Long industryCategoryId) {
        return productCategoryMapper.selectList(new LambdaQueryWrapper<ProductCategoryDO>()
                .eq(ProductCategoryDO::getIndustryCategoryId, industryCategoryId));
    }

}
