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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.FactoryInfoApi;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
import cn.iocoder.yudao.module.wms.controller.admin.depot.vo.DepotRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotarea.vo.DepotAreaPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotarea.vo.DepotAreaRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotarea.vo.DepotAreaSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.depotarea.DepotAreaDO;
import cn.iocoder.yudao.module.wms.dal.mysql.depotarea.DepotAreaMapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 库区管理 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class DepotAreaServiceImpl implements DepotAreaService {

    @Resource
    private DepotAreaMapper depotAreaMapper;
    @Resource
    private FactoryInfoApi factoryInfoApi;

    @Override
    public Long createDepotArea(DepotAreaSaveReqVO createReqVO) {
        // 插入
        DepotAreaDO depotArea = BeanUtils.toBean(createReqVO, DepotAreaDO.class);
        depotArea.setDepotAreaNo(IdWorker.get32UUID());
        depotAreaMapper.insert(depotArea);
        // 返回
        return depotArea.getId();
    }

    @Override
    public void updateDepotArea(DepotAreaSaveReqVO updateReqVO) {
        // 更新
        DepotAreaDO updateObj = BeanUtils.toBean(updateReqVO, DepotAreaDO.class);
        depotAreaMapper.updateById(updateObj);
    }

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

    @Override
    public DepotAreaDO getDepotArea(Long id) {
        return depotAreaMapper.selectById(id);
    }

    @Override
    public PageResult<DepotAreaDO> getDepotAreaPage(DepotAreaPageReqVO pageReqVO) {
        PageResult<DepotAreaDO> depotAreaDOPageResult = depotAreaMapper.selectPage(pageReqVO);
        List<Long> factoryIds = depotAreaDOPageResult.getList().stream().map(DepotAreaDO::getFactoryId).collect(Collectors.toList());
        //获取工厂列表名称
        List<FactoryInfoDTO> factoryInfoList = factoryInfoApi.getFactoryInfoList(factoryIds).getCheckedData();
        Map<Long, String> map = factoryInfoList.stream().collect(Collectors.toMap(FactoryInfoDTO::getId, FactoryInfoDTO::getName));
        for (DepotAreaDO depotAreaDO : depotAreaDOPageResult.getList()) {
            depotAreaDO.setFactoryName(map.get(depotAreaDO.getFactoryId()));
        }
        return depotAreaDOPageResult;
    }

    @Override
    public List<DepotAreaRespVO> getDepotAreaList(DepotAreaPageReqVO pageReqVO) {
        return depotAreaMapper.getDepotAreaAllList(pageReqVO);
    }

}
