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

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.industrycategory.vo.IndustryCategoryPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.industrycategory.vo.IndustryCategorySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.industrycategory.IndustryCategoryDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.industrycategoryproperty.IndustryCategoryPropertyDO;
import cn.iocoder.yudao.module.wms.dal.mysql.industrycategory.IndustryCategoryMapper;
import cn.iocoder.yudao.module.wms.dal.mysql.industrycategoryproperty.IndustryCategoryPropertyMapper;
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 IndustryCategoryServiceImpl implements IndustryCategoryService {

    @Resource
    private IndustryCategoryMapper industryCategoryMapper;
    @Resource
    private IndustryCategoryPropertyMapper industryCategoryPropertyMapper;

    @Override
    public Long createIndustryCategory(IndustryCategorySaveReqVO createReqVO) {
        // 插入
        IndustryCategoryDO industryCategory = BeanUtils.toBean(createReqVO, IndustryCategoryDO.class);
        industryCategoryMapper.insert(industryCategory);
        List<IndustryCategoryPropertyDO> items = BeanUtils.toBean(createReqVO.getItems(), IndustryCategoryPropertyDO.class);
        for (IndustryCategoryPropertyDO item : items) {
            item.setIndustryCategoryId(industryCategory.getId());
        }
        industryCategoryPropertyMapper.insertBatch(items);
        // 返回
        return industryCategory.getId();
    }

    @Override
    public void updateIndustryCategory(IndustryCategorySaveReqVO updateReqVO) {
        // 更新
        IndustryCategoryDO updateObj = BeanUtils.toBean(updateReqVO, IndustryCategoryDO.class);
        industryCategoryMapper.updateById(updateObj);
        List<IndustryCategoryPropertyDO> list = BeanUtils.toBean(updateReqVO.getItems(), IndustryCategoryPropertyDO.class);
        // -- 查询旧的
        List<IndustryCategoryPropertyDO> oldList = industryCategoryPropertyMapper.selectList(
                new LambdaQueryWrapper<IndustryCategoryPropertyDO>().eq(IndustryCategoryPropertyDO::getIndustryCategoryId, updateObj.getId())
        );
        List<Long> oldIds = oldList.stream().map(IndustryCategoryPropertyDO::getId).collect(Collectors.toList());
        List<Long> newIds = list.stream().map(IndustryCategoryPropertyDO::getId).filter(Objects::nonNull).collect(Collectors.toList());
        List<Long> delIds = CollectionUtil.subtractToList(oldIds, newIds);
        if (!delIds.isEmpty()) {
            industryCategoryPropertyMapper.deleteBatchIds(delIds);
        }
        List<IndustryCategoryPropertyDO> saveList = list.stream().filter(item -> item.getId() == null).collect(Collectors.toList());
        if (!saveList.isEmpty()) {
            for (IndustryCategoryPropertyDO industryCategoryPropertyDO : saveList) {
                industryCategoryPropertyDO.setIndustryCategoryId(updateReqVO.getId());
            }
            industryCategoryPropertyMapper.insertBatch(saveList);
        }
    }

    @Override
    public void deleteIndustryCategory(Long id) {
        // 删除
        industryCategoryMapper.deleteById(id);
        industryCategoryPropertyMapper.delete(new LambdaQueryWrapper<IndustryCategoryPropertyDO>()
                .eq(IndustryCategoryPropertyDO::getIndustryCategoryId, id));
    }

    @Override
    public IndustryCategoryDO getIndustryCategory(Long id) {
        return industryCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<IndustryCategoryDO> getIndustryCategoryPage(IndustryCategoryPageReqVO pageReqVO) {
        return industryCategoryMapper.selectPage(pageReqVO);
    }

}
