package com.ningdatech.carapi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.ningdatech.cache.repository.CachePlusOps;
import com.ningdatech.carapi.sys.entity.OperatorRegionHead;
import com.ningdatech.carapi.sys.entity.dto.OperatorRegionHeadSaveDTO;
import com.ningdatech.carapi.sys.mapper.OperatorRegionHeadMapper;
import com.ningdatech.carapi.sys.service.OperatorRegionHeadService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 运营商区域负责人
 * </p>
 *
 * @author PoffyZhang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OperatorRegionHeadServiceImpl extends ServiceImpl<OperatorRegionHeadMapper, OperatorRegionHead>
        implements OperatorRegionHeadService {

    private final CachePlusOps cachePlusOps;
    private final OperatorRegionHeadMapper operatorRegionHeadMapper;

    /**
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIdWithCache(List<Long> ids) {
        if (ids.isEmpty()) {
            return true;
        }
        // 运营商
        boolean removeFlag = removeByIds(ids);

        return removeFlag;
    }

    @Override
    public boolean removeByRegionId(Long operatorId) {
        if (Objects.isNull(operatorId)) {
            return Boolean.FALSE;
        }

        Map<String,Object> columnsMap = Maps.newHashMap();
        columnsMap.put("operator_id",operatorId);
        removeByMap(columnsMap);

        return Boolean.TRUE;
    }


    /**
     * 保存用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<OperatorRegionHeadSaveDTO> datas,Long operatorId, Long userId) {
        if(CollUtil.isEmpty(datas)){
            return;
        }
        datas.forEach(data -> {
            OperatorRegionHead operatorRegionHead = BeanUtil.toBean(data, OperatorRegionHead.class);
            operatorRegionHead.setOperatorId(operatorId);
            //创建人 最后修改人
            operatorRegionHead.setCreateBy(userId);
            operatorRegionHead.setUpdateBy(userId);
            save(operatorRegionHead);
        });
    }
}
