package cn.iocoder.yudao.module.wms.service.depotlocation;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.depotlocation.vo.DepotLocationPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotlocation.vo.DepotLocationSaveReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotlocationitem.vo.DepotLocationItemSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.depotlocation.DepotLocationDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.depotlocationitem.DepotLocationItemDO;
import cn.iocoder.yudao.module.wms.dal.mysql.depotlocation.DepotLocationMapper;
import cn.iocoder.yudao.module.wms.dal.mysql.depotlocationitem.DepotLocationItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 库位管理 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class DepotLocationServiceImpl implements DepotLocationService {

    @Resource
    private DepotLocationMapper depotLocationMapper;
    @Resource
    private DepotLocationItemMapper depotLocationItemMapper;

    @Override
    public Long createDepotLocation(DepotLocationSaveReqVO createReqVO) {
        // 插入
        DepotLocationDO depotLocation = BeanUtils.toBean(createReqVO, DepotLocationDO.class);
        depotLocationMapper.insert(depotLocation);
        List<DepotLocationItemDO> items = new ArrayList<>();
        for (DepotLocationItemSaveReqVO item : createReqVO.getItems()) {
            item.setLocationId(depotLocation.getId());
            DepotLocationItemDO itemDO = BeanUtils.toBean(item, DepotLocationItemDO.class);
            items.add(itemDO);
        }
        if (!items.isEmpty()) {
            depotLocationItemMapper.insertBatch(items);
        }
        // 返回
        return depotLocation.getId();
    }

    @Override
    public void updateDepotLocation(DepotLocationSaveReqVO updateReqVO) {
        // 更新
        DepotLocationDO updateObj = BeanUtils.toBean(updateReqVO, DepotLocationDO.class);
        depotLocationMapper.updateById(updateObj);


        List<DepotLocationItemDO> items = new ArrayList<>();
        for (DepotLocationItemSaveReqVO item : updateReqVO.getItems()) {
            item.setLocationId(updateReqVO.getId());
            DepotLocationItemDO itemDO = BeanUtils.toBean(item, DepotLocationItemDO.class);
            items.add(itemDO);
        }
        // 获取到老的items
        List<DepotLocationItemDO> depotLocationItemDOS = depotLocationItemMapper.selectList(new LambdaQueryWrapper<DepotLocationItemDO>().eq(DepotLocationItemDO::getLocationId, updateObj.getId()));
        if (depotLocationItemDOS.isEmpty() && !items.isEmpty()) {
            depotLocationItemMapper.insertBatch(items);
            return;
        }

        List<DepotLocationItemDO> saveList = items.stream().filter(a -> a.getId() == null).collect(Collectors.toList());
        List<DepotLocationItemDO> updateList = items.stream().filter(a -> a.getId() != null).collect(Collectors.toList());
        if (!saveList.isEmpty()) {
            depotLocationItemMapper.insertBatch(saveList);
        }
        if (!updateList.isEmpty()) {
            depotLocationItemMapper.updateBatch(updateList);
        }
        List<Long> oldIds = depotLocationItemDOS.stream().map(DepotLocationItemDO::getId).collect(Collectors.toList());
        List<Long> newIds = updateList.stream().map(DepotLocationItemDO::getId).collect(Collectors.toList());
        List<Long> delIds = new ArrayList<>(CollectionUtil.subtract(oldIds, newIds));
        if (!delIds.isEmpty()) {
            depotLocationItemMapper.deleteBatchIds(delIds);
        }
    }

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

    @Override
    public DepotLocationDO getDepotLocation(Long id) {
        DepotLocationDO depotLocationDO = depotLocationMapper.selectById(id);
        List<DepotLocationItemDO> items = depotLocationItemMapper.selectList(new LambdaQueryWrapper<DepotLocationItemDO>().eq(DepotLocationItemDO::getLocationId, id));
        depotLocationDO.setItems(items);
        return depotLocationDO;
    }

    @Override
    public PageResult<DepotLocationDO> getDepotLocationPage(DepotLocationPageReqVO pageReqVO) {
        return depotLocationMapper.selectPage(pageReqVO);
    }

}
