package cn.iocoder.yudao.module.system.service.informationmapassociation;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.informationmapassociation.vo.InformationMapAssociationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.informationmapassociation.vo.InformationMapAssociationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.informationmapassociation.InformationMapAssociationDO;
import cn.iocoder.yudao.module.system.dal.mysql.informationmapassociation.InformationMapAssociationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.hutool.core.collection.CollUtil.isNotEmpty;

/**
 * 设备地图关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InformationMapAssociationServiceImpl extends ServiceImpl<InformationMapAssociationMapper, InformationMapAssociationDO> implements InformationMapAssociationService {

    @Resource
    private InformationMapAssociationMapper informationMapAssociationMapper;

    @Override
    public Long createInformationMapAssociation(InformationMapAssociationSaveReqVO createReqVO) {
        // 插入
        InformationMapAssociationDO informationMapAssociation = BeanUtils.toBean(createReqVO, InformationMapAssociationDO.class);
        informationMapAssociationMapper.insert(informationMapAssociation);
        // 返回
        return informationMapAssociation.getId();
    }

    @Override
    public void updateInformationMapAssociation(InformationMapAssociationSaveReqVO updateReqVO) {
        // 更新
        InformationMapAssociationDO updateObj = BeanUtils.toBean(updateReqVO, InformationMapAssociationDO.class);
        informationMapAssociationMapper.updateById(updateObj);
    }

    @Override
    public void deleteInformationMapAssociation(Long id) {
        // 删除
        informationMapAssociationMapper.deleteById(id);
    }

    @Override
    public InformationMapAssociationDO getInformationMapAssociation(Long id) {
        return informationMapAssociationMapper.selectById(id);
    }

    @Override
    public PageResult<InformationMapAssociationDO> getInformationMapAssociationPage(InformationMapAssociationPageReqVO pageReqVO) {
        return informationMapAssociationMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveOrUpdateOrDel(Long id, List<List<Long>> mapIds) {
        //批量添加、修改、删除
        if (isNotEmpty(mapIds.get(0))) {
            List<InformationMapAssociationDO> saveList = new ArrayList<>();
            for (Long mapId : mapIds.get(0)) {
                saveList.add(new InformationMapAssociationDO().setPositionMapId(mapId).setRobotInformationId(id));
            }
            informationMapAssociationMapper.insertBatch(saveList);
        }
        if (isNotEmpty(mapIds.get(2))) {
            informationMapAssociationMapper.delete(new LambdaQueryWrapper<InformationMapAssociationDO>()
                    .eq(InformationMapAssociationDO::getRobotInformationId, id)
                    .in(InformationMapAssociationDO::getPositionMapId, mapIds.get(2)));
        }
    }

}
