package com.ghy.worker.service.impl;

import com.ghy.worker.domain.WorkerGoodsCategory;
import com.ghy.worker.mapper.WorkerGoodsCategoryMapper;
import com.ghy.worker.service.WorkerGoodsCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * 师傅服务类目Service
 *
 * @author HH 2022/6/7
 */
@Service
public class WorkerGoodsCategoryServiceImpl implements WorkerGoodsCategoryService {

    @Resource
    private WorkerGoodsCategoryMapper workerGoodsCategoryMapper;

    @Override
    public int insert(WorkerGoodsCategory workerGoodsCategory) {
        return workerGoodsCategoryMapper.insert(workerGoodsCategory);
    }

    @Override
    public int delete(Long[] ids) {
        return workerGoodsCategoryMapper.delete(ids);
    }

    @Override
    public int deleteByWorker(Long workerId) {
        return workerGoodsCategoryMapper.deleteByWorker(workerId);
    }

    @Override
    public List<WorkerGoodsCategory> getByWorker(Long workerId) {
        return workerGoodsCategoryMapper.getByWorker(workerId);
    }

    @Override
    public void updateWorkerGoodsCategory (Long workerId, List<WorkerGoodsCategory> categories) {
        List<WorkerGoodsCategory> existCategories = getByWorker(workerId);
        Set<Long> existCategoryIds = existCategories.stream().map(WorkerGoodsCategory::getGoodsCategoryId).collect(Collectors.toSet());
        Set<Long> categoryIds = categories.stream().map(WorkerGoodsCategory::getGoodsCategoryId).collect(Collectors.toSet());

        // 新的区域-已存在区域的差集，为需要新加入的区域
        List<WorkerGoodsCategory> categories2Insert = CollectionUtils.subtract(categoryIds, existCategoryIds).stream().map(x -> {
            WorkerGoodsCategory obj = new WorkerGoodsCategory();
            obj.setWorkerId(workerId);
            obj.setGoodsCategoryId(x);
            return obj;
        }).collect(Collectors.toList());
        // 已存在区域-新的区域的差集，为需要删除的区域
        List<Long> id2DelList = existCategories.stream().filter(x -> !categoryIds.contains(x.getGoodsCategoryId()))
            .map(WorkerGoodsCategory::getWorkerGoodsCategoryId).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(categories2Insert)) {
            workerGoodsCategoryMapper.batchInsert(categories2Insert);
        }
        if (!CollectionUtils.isEmpty(id2DelList)) {
            Long[] id2Del = new Long[]{};
            workerGoodsCategoryMapper.delete(id2DelList.toArray(id2Del));
        }
    }

    @Override
    public List<WorkerGoodsCategory> getWorkerGoodsCategory(WorkerGoodsCategory workerGoodsCategory) {
        return workerGoodsCategoryMapper.getWorkerGoodsCategory(workerGoodsCategory);
    }

    @Override
    public void updateWorkerGoodsCategoryByType(Long workerId, List<WorkerGoodsCategory> categories, Integer serviceType) {
        if (categories == null || categories.isEmpty()) {
            return;
        }
        
        // 获取该师傅该类型的已存在类目
        List<WorkerGoodsCategory> existCategories = workerGoodsCategoryMapper.getByWorkerIdAndType(workerId, serviceType);
        Set<Long> existCategoryIds = existCategories.stream().map(WorkerGoodsCategory::getGoodsCategoryId).collect(Collectors.toSet());
        Set<Long> categoryIds = categories.stream().map(WorkerGoodsCategory::getGoodsCategoryId).collect(Collectors.toSet());

        // 新的类目-已存在类目的差集，为需要新加入的类目
        List<WorkerGoodsCategory> categories2Insert = CollectionUtils.subtract(categoryIds, existCategoryIds).stream().map(x -> {
            WorkerGoodsCategory obj = new WorkerGoodsCategory();
            obj.setWorkerId(workerId);
            obj.setGoodsCategoryId(x);
            obj.setServiceType(serviceType);  // 设置类型：1=服务, 2=商品
            return obj;
        }).collect(Collectors.toList());
        
        // 已存在类目-新的类目的差集，为需要删除的类目
        List<Long> id2DelList = existCategories.stream().filter(x -> !categoryIds.contains(x.getGoodsCategoryId()))
            .map(WorkerGoodsCategory::getWorkerGoodsCategoryId).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(categories2Insert)) {
            workerGoodsCategoryMapper.batchInsert(categories2Insert);
        }
        if (!CollectionUtils.isEmpty(id2DelList)) {
            Long[] id2Del = new Long[]{};
            workerGoodsCategoryMapper.delete(id2DelList.toArray(id2Del));
        }
    }

    @Override
    public List<WorkerGoodsCategory> getByWorkerIdAndType(Long workerId, Integer serviceType) {
        return workerGoodsCategoryMapper.getByWorkerIdAndType(workerId, serviceType);
    }
}
