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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.constant.robot.RobotTaskChcheConstant;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.dto.PositionMapConditionDTO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapItemPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapItemSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.positionmap.PositionMapItemMapper;
import cn.iocoder.yudao.module.system.util.redis.RedisUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

import static cn.hutool.core.collection.CollUtil.isNotEmpty;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 仓库点位地图子表 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PositionMapItemServiceImpl extends ServiceImpl<PositionMapItemMapper, PositionMapItemDO> implements PositionMapItemService {

    @Resource
    private PositionMapItemMapper positionMapItemMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public Long createPositionMapItem(PositionMapItemSaveReqVO createReqVO) {
        // 插入
        PositionMapItemDO positionMapItem = BeanUtils.toBean(createReqVO, PositionMapItemDO.class);
        positionMapItemMapper.insert(positionMapItem);
        // 返回
        return positionMapItem.getId();
    }

    @Override
    public void updatePositionMapItem(PositionMapItemSaveReqVO updateReqVO) {
        // 更新
        PositionMapItemDO updateObj = BeanUtils.toBean(updateReqVO, PositionMapItemDO.class);
        positionMapItemMapper.updateById(updateObj);
    }

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

    @Override
    public PositionMapItemDO getPositionMapItem(Long id) {
        return positionMapItemMapper.selectById(id);
    }

    @Override
    public PageResult<PositionMapItemDO> getPositionMapItemPage(PositionMapItemPageReqVO pageReqVO) {
        return positionMapItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PositionMapItemDO> getByMapId(Long positionMapId) {
        return positionMapItemMapper.selectList(new LambdaQueryWrapperX<PositionMapItemDO>()
                .eq(PositionMapItemDO::getPositionMapId, positionMapId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveOrEditOrDel(Long positionMapId, List<List<PositionMapItemDO>> list) {
        //批量添加、修改、删除
        if (isNotEmpty(list.get(0))) {
            list.get(0).forEach(a -> a.setPositionMapId(positionMapId));
            positionMapItemMapper.insertBatch(list.get(0));
        }
        if (isNotEmpty(list.get(1))) {
            list.get(1).forEach(a -> a.setPositionMapId(positionMapId));
            positionMapItemMapper.updateBatch(list.get(1));
        }
        if (isNotEmpty(list.get(2))) {
            positionMapItemMapper.deleteByIds(convertList(list.get(2), PositionMapItemDO::getId));
        }
    }


    @Override
    public List<PositionMapItemDO> getByCondition(PositionMapConditionDTO dto) {
        return positionMapItemMapper.selectList(new LambdaQueryWrapperX<PositionMapItemDO>()
                .eqIfPresent(PositionMapItemDO::getPositionMapId, dto.getPositionMapId())
                .eqIfPresent(PositionMapItemDO::getType, dto.getType())
                .inIfPresent(PositionMapItemDO::getId, dto.getIds())
                .eqIfPresent(PositionMapItemDO::getAreaId, dto.getAreaId())
                .eqIfPresent(PositionMapItemDO::getLaneId, dto.getLaneId()));
    }

    @Override
    public void emptyAreaId(Long positionMapId, Long areaId) {
        positionMapItemMapper.emptyAreaId(positionMapId, areaId);
    }

    @Override
    public void emptyLaneId(Long mapId, Long laneId) {
        positionMapItemMapper.emptyLaneId(mapId, laneId);
    }

    @Override
    public Object getAGVPointInformation(String macAddress) {
        String pose2dKey = RobotTaskChcheConstant.ROBOT_INFORMATION_POSE_BAT_SOC + macAddress;
        try {
            // 睡眠 0.5s 以确保获取到是最新的数据
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return redisUtil.get(pose2dKey);
    }
}
