package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.HyInspectionSettingMappingDO;
import com.cool.store.mapper.HyInspectionSettingMappingMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: HyInspectionSettingMappingDAO
 * @Description:
 * @date 2023-07-18 16:29
 */
@Repository
public class HyInspectionSettingMappingDAO {

    @Resource
    private HyInspectionSettingMappingMapper hyInspectionSettingMappingMapper;


    /**
     * 获取区域ids
     * @param inspectionSettingId
     * @return
     */
    public List<Long> getOpenAreaMappingIds(Long inspectionSettingId){
        if(Objects.isNull(inspectionSettingId)){
            return Lists.newArrayList();
        }
        return hyInspectionSettingMappingMapper.getOpenAreaMappingIds(inspectionSettingId);
    }

    /**
     * 新增映射关系
     * @param inspectionSettingId
     * @param openAreaMappingIds
     * @return
     */
    public Integer addInspectionSettingMapping(Long inspectionSettingId, List<Long> openAreaMappingIds){
        if(CollectionUtils.isEmpty(openAreaMappingIds) || Objects.isNull(inspectionSettingId)){
            return CommonConstants.ZERO;
        }
        hyInspectionSettingMappingMapper.deleteInOpenAreaMappingIds(inspectionSettingId, openAreaMappingIds);
        List<HyInspectionSettingMappingDO> insertList = new ArrayList<>();
        for (Long openAreaMappingId : openAreaMappingIds) {
            HyInspectionSettingMappingDO insert = new HyInspectionSettingMappingDO();
            insert.setInspectionSettingId(inspectionSettingId);
            insert.setOpenAreaMappingId(openAreaMappingId);
            insert.setCreateTime(new Date());
            insert.setDeleted(Boolean.FALSE);
            insertList.add(insert);
        }
        return hyInspectionSettingMappingMapper.batchInsertSelective(insertList);
    }

    /**
     * 更新映射关系
     * @param inspectionSettingId
     * @param openAreaMappingIds
     * @return
     */
    public Integer updateInspectionSettingMapping(Long inspectionSettingId, List<Long> openAreaMappingIds){
        hyInspectionSettingMappingMapper.deleteNotInOpenAreaMappingIds(inspectionSettingId, openAreaMappingIds);
        return addInspectionSettingMapping(inspectionSettingId, openAreaMappingIds);
    }

    /**
     * 删除映射关系
     * @param inspectionSettingId
     * @return
     */
    public Integer deleteInspectionSettingMapping(Long inspectionSettingId){
        if(Objects.isNull(inspectionSettingId)){
            return CommonConstants.ZERO;
        }
        return hyInspectionSettingMappingMapper.deleteInspectionSettingMapping(inspectionSettingId);
    }

    /**
     * 获取冲突的映射关系
     * @param inspectionSettingId
     * @param openAreaMappingIds
     * @return
     */
    public List<HyInspectionSettingMappingDO> getConflictInspectionSetting(Long inspectionSettingId, List<Long> openAreaMappingIds){
        if(CollectionUtils.isEmpty(openAreaMappingIds)){
            return Lists.newArrayList();
        }
        return hyInspectionSettingMappingMapper.getConflictInspectionSetting(inspectionSettingId, openAreaMappingIds);
    }

}
