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

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.PositionMapLinePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapLineSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapLineDO;
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.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 PositionMapLineServiceImpl extends ServiceImpl<PositionMapLineMapper, PositionMapLineDO> implements PositionMapLineService {

    @Resource
    private PositionMapLineMapper positionMapLineMapper;

    @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);
        newList.forEach(a -> a.setPositionMapId(positionMapId));
        // 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));
        }
    }

}
