package com.ghy.goods.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ghy.common.utils.DateUtils;
import com.ghy.goods.domain.DeptCategoryInsuranceRelation;
import com.ghy.goods.domain.DeptGoodsCategory;
import com.ghy.goods.service.DeptGoodsCategoryService;
import com.ghy.goods.service.IDeptCategoryInsuranceRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ghy.goods.mapper.CategoryInsuranceRelationMapper;
import com.ghy.goods.domain.CategoryInsuranceRelation;
import com.ghy.goods.service.ICategoryInsuranceRelationService;
import com.ghy.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 总类目保险关联关系Service业务层处理
 *
 * @author clunt
 * @date 2024-09-25
 */
@Service
public class CategoryInsuranceRelationServiceImpl implements ICategoryInsuranceRelationService
{
    @Autowired
    private CategoryInsuranceRelationMapper categoryInsuranceRelationMapper;

    @Resource
    private IDeptCategoryInsuranceRelationService deptCategoryInsuranceRelationService;

    @Resource
    private DeptGoodsCategoryService deptGoodsCategoryService;

    /**
     * 查询总类目保险关联关系
     *
     * @param id 总类目保险关联关系主键
     * @return 总类目保险关联关系
     */
    @Override
    public CategoryInsuranceRelation selectCategoryInsuranceRelationById(Long id)
    {
        return categoryInsuranceRelationMapper.selectCategoryInsuranceRelationById(id);
    }

    /**
     * 查询总类目保险关联关系列表
     *
     * @param categoryInsuranceRelation 总类目保险关联关系
     * @return 总类目保险关联关系
     */
    @Override
    public List<CategoryInsuranceRelation> selectCategoryInsuranceRelationList(CategoryInsuranceRelation categoryInsuranceRelation)
    {
        return categoryInsuranceRelationMapper.selectCategoryInsuranceRelationList(categoryInsuranceRelation);
    }

    /**
     * 新增总类目保险关联关系
     *
     * @param categoryInsuranceRelation 总类目保险关联关系
     * @return 结果
     */
    @Override
    public int insertCategoryInsuranceRelation(CategoryInsuranceRelation categoryInsuranceRelation)
    {
        categoryInsuranceRelation.setCreateTime(DateUtils.getNowDate());
        return categoryInsuranceRelationMapper.insertCategoryInsuranceRelation(categoryInsuranceRelation);
    }

    /**
     * 修改总类目保险关联关系
     *
     * @param categoryInsuranceRelation 总类目保险关联关系
     * @return 结果
     */
    @Override
    public int updateCategoryInsuranceRelation(CategoryInsuranceRelation categoryInsuranceRelation)
    {
        categoryInsuranceRelation.setUpdateTime(DateUtils.getNowDate());
        return categoryInsuranceRelationMapper.updateCategoryInsuranceRelation(categoryInsuranceRelation);
    }

    /**
     * 批量删除总类目保险关联关系
     *
     * @param ids 需要删除的总类目保险关联关系主键
     * @return 结果
     */
    @Override
    public int deleteCategoryInsuranceRelationByIds(String ids)
    {
        return categoryInsuranceRelationMapper.deleteCategoryInsuranceRelationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除总类目保险关联关系信息
     *
     * @param id 总类目保险关联关系主键
     * @return 结果
     */
    @Override
    public int deleteCategoryInsuranceRelationById(Long id)
    {
        return categoryInsuranceRelationMapper.deleteCategoryInsuranceRelationById(id);
    }

    @Override
    public void updateCategoryInsuranceRelation(String insuranceIds, Long categoryId) {
        CategoryInsuranceRelation param = new CategoryInsuranceRelation();
        param.setGoodsCategoryId(categoryId);
        List<CategoryInsuranceRelation> oldRecord = categoryInsuranceRelationMapper.selectCategoryInsuranceRelationList(param);
        Map<Long, CategoryInsuranceRelation> insuranceRelationMap = oldRecord.stream().collect(Collectors.toMap(CategoryInsuranceRelation::getInsuranceId, Function.identity(), (x1, x2)->x1));
        // 已存在的id
        List<Long> oldIds = oldRecord.stream().map(CategoryInsuranceRelation::getInsuranceId).collect(Collectors.toList());
        // 新的
        List<Long> newIds = new ArrayList<>();
        if(StringUtils.isNotEmpty(insuranceIds)){
            for (String id : insuranceIds.split(",")) {
                newIds.add(Long.valueOf(id));
            }
        }
        // 删除旧的
        List<Long> delOlds = oldIds.stream().filter(x->!newIds.contains(x)).collect(Collectors.toList());
        // 增加新的
        List<Long> addNews = newIds.stream().filter(x->!oldIds.contains(x)).collect(Collectors.toList());

        // 删除历史记录
        if(CollectionUtil.isNotEmpty(delOlds)){
            String [] arr = new String[delOlds.size()];
            for (int index = 0; index < delOlds.size(); index ++){
                arr[index] = insuranceRelationMap.get(delOlds.get(index)).getId().toString();
                // 删除关联的平台类目的
                // 查找类目
                DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(categoryId);
                if(deptGoodsCategory != null){
                    DeptCategoryInsuranceRelation deptCategoryInsuranceRelation = new DeptCategoryInsuranceRelation();
                    deptCategoryInsuranceRelation.setDeptCategoryId(deptGoodsCategory.getDeptGoodsCategoryId());
                    deptCategoryInsuranceRelation.setInsuranceId(delOlds.get(index));
                    List<DeptCategoryInsuranceRelation> delList = deptCategoryInsuranceRelationService.selectDeptCategoryInsuranceRelationList(deptCategoryInsuranceRelation);
                    delList.forEach(model->{
                        deptCategoryInsuranceRelationService.deleteDeptCategoryInsuranceRelationById(model.getId());
                    });
                }
            }
            categoryInsuranceRelationMapper.deleteCategoryInsuranceRelationByIds(arr);
        }
        // 增加新记录
        if(CollectionUtil.isNotEmpty(addNews)){
            for (Long id : addNews) {
                CategoryInsuranceRelation model = new CategoryInsuranceRelation();
                model.setInsuranceId(id);
                model.setGoodsCategoryId(categoryId);
                categoryInsuranceRelationMapper.insertCategoryInsuranceRelation(model);
            }
        }
    }
}
