package com.ghy.goods.service.impl;

import java.util.List;
import com.ghy.common.utils.DateUtils;
import com.ghy.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ghy.goods.mapper.DeptCategoryInsuranceRelationMapper;
import com.ghy.goods.domain.DeptCategoryInsuranceRelation;
import com.ghy.goods.service.IDeptCategoryInsuranceRelationService;
import com.ghy.common.core.text.Convert;

/**
 * 平台类目保险关联关系Service业务层处理
 *
 * @author clunt
 * @date 2024-09-27
 */
@Service
public class DeptCategoryInsuranceRelationServiceImpl implements IDeptCategoryInsuranceRelationService
{
    @Autowired
    private DeptCategoryInsuranceRelationMapper deptCategoryInsuranceRelationMapper;

    /**
     * 查询平台类目保险关联关系
     *
     * @param id 平台类目保险关联关系主键
     * @return 平台类目保险关联关系
     */
    @Override
    public DeptCategoryInsuranceRelation selectDeptCategoryInsuranceRelationById(Long id)
    {
        return deptCategoryInsuranceRelationMapper.selectDeptCategoryInsuranceRelationById(id);
    }

    /**
     * 查询平台类目保险关联关系列表
     *
     * @param deptCategoryInsuranceRelation 平台类目保险关联关系
     * @return 平台类目保险关联关系
     */
    @Override
    public List<DeptCategoryInsuranceRelation> selectDeptCategoryInsuranceRelationList(DeptCategoryInsuranceRelation deptCategoryInsuranceRelation)
    {
        return deptCategoryInsuranceRelationMapper.selectDeptCategoryInsuranceRelationList(deptCategoryInsuranceRelation);
    }

    /**
     * 新增平台类目保险关联关系
     *
     * @param deptCategoryInsuranceRelation 平台类目保险关联关系
     * @return 结果
     */
    @Override
    public int insertDeptCategoryInsuranceRelation(DeptCategoryInsuranceRelation deptCategoryInsuranceRelation)
    {
        deptCategoryInsuranceRelation.setCreateTime(DateUtils.getNowDate());
        return deptCategoryInsuranceRelationMapper.insertDeptCategoryInsuranceRelation(deptCategoryInsuranceRelation);
    }

    /**
     * 修改平台类目保险关联关系
     *
     * @param deptCategoryInsuranceRelation 平台类目保险关联关系
     * @return 结果
     */
    @Override
    public int updateDeptCategoryInsuranceRelation(DeptCategoryInsuranceRelation deptCategoryInsuranceRelation)
    {
        deptCategoryInsuranceRelation.setUpdateTime(DateUtils.getNowDate());
        return deptCategoryInsuranceRelationMapper.updateDeptCategoryInsuranceRelation(deptCategoryInsuranceRelation);
    }

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

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

    @Override
    public void updateDeptCategoryInsuranceRelation(String pcInsuranceIds, String shopInsuranceIds, Long deptGoodsCategoryId) {
        // pc --
        if(StringUtils.isNotEmpty(pcInsuranceIds)){
            // 删除旧的
            DeptCategoryInsuranceRelation pcRelationParam = new DeptCategoryInsuranceRelation();
            pcRelationParam.setDeptCategoryId(deptGoodsCategoryId);
            pcRelationParam.setType("02");
            List<DeptCategoryInsuranceRelation> pcRelationOld = deptCategoryInsuranceRelationMapper.selectDeptCategoryInsuranceRelationList(pcRelationParam);
            if(CollectionUtils.isNotEmpty(pcRelationOld)){
                pcRelationOld.forEach(model->{
                    deptCategoryInsuranceRelationMapper.deleteDeptCategoryInsuranceRelationById(model.getId());
                });
            }
            // 增加新的
            for (String id : pcInsuranceIds.split(",")) {
                DeptCategoryInsuranceRelation pcRelation = new DeptCategoryInsuranceRelation();
                pcRelation.setDeptCategoryId(deptGoodsCategoryId);
                pcRelation.setType("02");
                pcRelation.setInsuranceId(Long.valueOf(id));
                deptCategoryInsuranceRelationMapper.insertDeptCategoryInsuranceRelation(pcRelation);
            }

        }
        // 商城 --
        if(StringUtils.isNotEmpty(shopInsuranceIds)){
            // 删除旧的
            DeptCategoryInsuranceRelation pcRelationParam = new DeptCategoryInsuranceRelation();
            pcRelationParam.setDeptCategoryId(deptGoodsCategoryId);
            pcRelationParam.setType("01");
            List<DeptCategoryInsuranceRelation> pcRelationOld = deptCategoryInsuranceRelationMapper.selectDeptCategoryInsuranceRelationList(pcRelationParam);
            if(CollectionUtils.isNotEmpty(pcRelationOld)){
                pcRelationOld.forEach(model->{
                    deptCategoryInsuranceRelationMapper.deleteDeptCategoryInsuranceRelationById(model.getId());
                });
            }
            // 增加新的
            for (String id : shopInsuranceIds.split(",")) {
                DeptCategoryInsuranceRelation pcRelation = new DeptCategoryInsuranceRelation();
                pcRelation.setDeptCategoryId(deptGoodsCategoryId);
                pcRelation.setType("01");
                pcRelation.setInsuranceId(Long.valueOf(id));
                deptCategoryInsuranceRelationMapper.insertDeptCategoryInsuranceRelation(pcRelation);
            }

        }

    }
}
