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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapIdNumMapVO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapLinePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapLineSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapItemDO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapLineDO;
import cn.iocoder.yudao.module.system.dal.mysql.positionmap.PositionMapItemMapper;
import cn.iocoder.yudao.module.system.dal.mysql.positionmap.PositionMapLineMapper;
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.*;
import java.util.stream.Collectors;

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 PositionMapLineServiceImpl extends ServiceImpl<PositionMapLineMapper, PositionMapLineDO> implements PositionMapLineService {

    @Resource
    private PositionMapLineMapper positionMapLineMapper;
    @Resource
    private PositionMapItemMapper positionMapItemMapper;

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

    @Override
    public PositionMapLineDO getPositionMapLine(Long id) {
        return positionMapLineMapper.selectById(id);
    }

    @Override
    public PageResult<PositionMapLineDO> getPositionMapLinePage(PositionMapLinePageReqVO pageReqVO) {
        return positionMapLineMapper.selectPage(pageReqVO);
    }

    @Override
    public void createOrEditPositionMapLine(Long positionMapId, List<PositionMapLineSaveReqVO> createReqVO) {
        List<PositionMapLineDO> newList = BeanUtils.toBean(createReqVO, PositionMapLineDO.class);
        // 获取到所有新增的并且（没有起始点位或者没有结束点位序号）的数据
        List<PositionMapLineDO> addList = newList.stream().filter(a -> a.getId() == null
                && (a.getStartingSortNum() == null || a.getEndPointSortNum() == null)).collect(Collectors.toList());
        Map<Long, Long> map = new HashMap<>();
        if (CollUtil.isNotEmpty(addList)) {
            List<Long> startPointIds = addList.stream().map(PositionMapLineDO::getStartingPointId).collect(Collectors.toList());
            List<Long> endPointIds = addList.stream().map(PositionMapLineDO::getEndPointId).collect(Collectors.toList());
            //合并startPointIds 和 endPointIds 并且去重
            Set<Long> pointIds = CollUtil.unionDistinct(startPointIds, endPointIds);
            List<PositionMapItemDO> positionMapItemDOS = positionMapItemMapper.selectSortNumByIds(new ArrayList<>(pointIds));
            if (ObjectUtil.isNotEmpty(positionMapItemDOS)) {
                 map = positionMapItemDOS.stream().collect(Collectors.toMap(PositionMapItemDO::getId, PositionMapItemDO::getSortNum));
            }
        }
        for (PositionMapLineDO positionMapLineDO : newList) {
            positionMapLineDO.setPositionMapId(positionMapId);
            if (MapUtil.isNotEmpty(map) && positionMapLineDO.getId() == null &&
                    (positionMapLineDO.getStartingSortNum() == null || positionMapLineDO.getEndPointSortNum() == null)) {
                Long startingSortNum = map.get(positionMapLineDO.getStartingPointId());
                Long endPointSortNum = map.get(positionMapLineDO.getEndPointId());
                if (startingSortNum != null) {
                    positionMapLineDO.setStartingSortNum(startingSortNum.intValue());
                }
                if (endPointSortNum != null) {
                    positionMapLineDO.setEndPointSortNum(endPointSortNum.intValue());
                }
            }
        }
        // todo 判断是否有重复数据
        List<PositionMapLineDO> oldList = this.getByMapId(positionMapId);
        List<List<PositionMapLineDO>> list = CollectionUtils.compareLists(oldList, newList,
                (oldVal, newVal) -> ObjectUtil.equal(oldVal.getId(), newVal.getId()));
        this.batchSaveOrEditOrDel(list);
    }

    @Override
    public List<PositionMapLineDO> getByMapId(Long positionMapId) {
        return positionMapLineMapper.selectList(new LambdaQueryWrapper<PositionMapLineDO>()
                .eq(PositionMapLineDO::getPositionMapId, positionMapId));
    }

    @Override
    public void batchSaveOrEditOrDel(List<List<PositionMapLineDO>> list) {
        //批量添加、修改、删除
        if (isNotEmpty(list.get(0))) {
            positionMapLineMapper.insertBatch(list.get(0));
        }
        if (isNotEmpty(list.get(1))) {
            positionMapLineMapper.updateBatch(list.get(1));
        }
        if (isNotEmpty(list.get(2))) {
            positionMapLineMapper.deleteByIds(convertList(list.get(2), PositionMapLineDO::getId));
        }
    }

    @Override
    public void batchSaveLines(List<PositionMapLineDO> lineList) {
        positionMapLineMapper.insertBatch(lineList);
    }

    @Override
    public void deleteByMapId(Long mapId) {
        positionMapLineMapper.deleteByMapId(mapId);
    }

}
