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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.AssetsTypeCodeEnum;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.FactoryInfoApi;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
import cn.iocoder.yudao.module.smartfactory.api.factoryforklift.FactoryForkliftApi;
import cn.iocoder.yudao.module.smartfactory.api.factoryforklift.dto.FactoryForkliftDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.AssetsDO;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.AssetsTypeDO;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.DeptAssetsDO;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.DeptAssetsInOutStockDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.forklifttemporarytable.ForkliftTemporaryTableDO;
import cn.iocoder.yudao.module.system.dal.mysql.forklifttemporarytable.ForkliftTemporaryTableMapper;
import cn.iocoder.yudao.module.system.service.assets.AssetsService;
import cn.iocoder.yudao.module.system.service.assets.AssetsTypeService;
import cn.iocoder.yudao.module.system.service.assets.DeptAssetsInOutStockService;
import cn.iocoder.yudao.module.system.service.assets.DeptAssetsService;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工厂叉车资产（excel 临时表） Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class ForkliftTemporaryTableServiceImpl implements ForkliftTemporaryTableService {

    @Resource
    private ForkliftTemporaryTableMapper forkliftTemporaryTableMapper;
    @Resource
    private AssetsTypeService assetsTypeService;
    @Resource
    private AssetsService assetsService;
    @Resource
    private FactoryInfoApi factoryInfoApi;
    @Resource
    private DeptService deptService;
    @Resource
    private DeptAssetsService deptAssetsService;
    @Resource
    private DeptAssetsInOutStockService deptAssetsInOutStockService;
    @Resource
    private FactoryForkliftApi factoryForkliftApi;
    @Override
    public void test() {
        List<ForkliftTemporaryTableDO> forkliftTemporaryTableDOS = forkliftTemporaryTableMapper.selectList();
        // 获取所有叉车类型
        List<AssetsTypeDO> list = assetsTypeService.list(new LambdaQueryWrapper<AssetsTypeDO>()
                .like(AssetsTypeDO::getCode, AssetsTypeCodeEnum.FORKLIFT));
        //根据名字分组
        Map<String, Long> map = list.stream().collect(Collectors.toMap(AssetsTypeDO::getName, AssetsTypeDO::getId));
        // 需要做新增操作的列表 ------ 开始
        List<AssetsDO> assetsDOS = new ArrayList<>();
        List<DeptAssetsInOutStockDO> deptAssetsInOutStockDOS = new ArrayList<>();
        List<DeptAssetsDO> deptAssetsDOS = new ArrayList<>();
        List<FactoryForkliftDTO> factoryForkliftDTOS = new ArrayList<>();
        // 需要做新增操作的列表 ------ 结束


        // 获取工厂列表
        List<FactoryInfoDTO> factoryInfoDTOS = factoryInfoApi.getAllList().getCheckedData();
        Map<String, Long> factoryMap = factoryInfoDTOS.stream().collect(Collectors.toMap(FactoryInfoDTO::getShortName, FactoryInfoDTO::getId));
        // 获取工厂对应的部门
        // 获取部门列表
        List<DeptDO> deptDOList = deptService.getDeptListByFactoryIds(factoryInfoDTOS.stream().map(FactoryInfoDTO::getId).collect(Collectors.toList()));
        Map<Long, Long> factoryDeptMap = deptDOList.stream().collect(Collectors.toMap(DeptDO::getFactoryId, DeptDO::getId));

        for (ForkliftTemporaryTableDO forkliftTemporaryTableDO : forkliftTemporaryTableDOS) {
            // -----------  资产表 -----------
            AssetsDO assetsDO = new AssetsDO();
            assetsDO.setId(IdWorker.getId());
            assetsDO.setName(forkliftTemporaryTableDO.getType());
            // 公司名称 + 用途 + 类别 + 名称 + 日期 + 流水号

//            String assetsNo = ChineseConversionUtils.getChineseInitials(forkliftTemporaryTableDO.getFactoryName()) + "_" +
//                    ChineseConversionUtils.getChineseInitials(forkliftTemporaryTableDO.getType()) + "_" +
//                    ChineseConversionUtils.getChineseInitials();
            assetsDO.setAssetsNo("//");
            assetsDO.setStatus(0);
            assetsDO.setStock(0);
            Long typeId = map.get(forkliftTemporaryTableDO.getType());
            assetsDO.setTypeId(typeId);
            // -----------  资产表 -----------

            // -- 我这里只有工厂名称 - 我要通过工厂名称 找到工厂id 再通过工厂id 拿到对应的部门id
            Long factoryId = factoryMap.get(forkliftTemporaryTableDO.getFactoryName().trim());
            Long deptId = null;
            if (factoryId != null) {
                deptId = factoryDeptMap.get(factoryId);
            }
            if (deptId == null) {
                continue;
            }
            // -----------  资产出入库记录表 -----------
            DeptAssetsInOutStockDO deptAssetsInOutStockDO = new DeptAssetsInOutStockDO();
            deptAssetsInOutStockDO.setInDeptId(deptId);
            deptAssetsInOutStockDO.setOutDeptId(-1L);
            deptAssetsInOutStockDO.setAssetsId(assetsDO.getId());
            deptAssetsInOutStockDO.setNum(1);
            deptAssetsInOutStockDO.setInOutNo(IdWorker.get32UUID());
            // -----------  资产出入库记录表 -----------

            // -----------  部门资产表 -----------
            DeptAssetsDO deptAssetsDO = new DeptAssetsDO();
            deptAssetsDO.setId(IdWorker.getId());
            deptAssetsDO.setDeptId(deptId);
            deptAssetsDO.setAssetsId(assetsDO.getId());
            deptAssetsDO.setStatus("在用".equals(forkliftTemporaryTableDO.getStatus()) ? 1
                    : "维修".equals(forkliftTemporaryTableDO.getStatus()) ? 2
                    : "备用".equals(forkliftTemporaryTableDO.getStatus()) ? 0
                    : "停用".equals(forkliftTemporaryTableDO.getStatus()) ? 4
                    : "报废".equals(forkliftTemporaryTableDO.getStatus()) ? 3
                    : -1);
            deptAssetsDO.setStock(1);
            // -----------  部门资产表 -----------
            // -----------  叉车表 -----------
            FactoryForkliftDTO factoryForkliftDTO = new FactoryForkliftDTO();
            factoryForkliftDTO.setAssetsNo(assetsDO.getAssetsNo());
            factoryForkliftDTO.setFactoryId(factoryId);
            factoryForkliftDTO.setDeptId(deptId);
            factoryForkliftDTO.setName(forkliftTemporaryTableDO.getType());
            factoryForkliftDTO.setManufacturer(forkliftTemporaryTableDO.getBrand());
            factoryForkliftDTO.setEquipmentCode(forkliftTemporaryTableDO.getEquipmentNo());
            factoryForkliftDTO.setSerialNo(forkliftTemporaryTableDO.getFactoryNo());
            factoryForkliftDTO.setModelNo(forkliftTemporaryTableDO.getSku());
            factoryForkliftDTO.setLicensePlate(forkliftTemporaryTableDO.getLicensePlate());
            factoryForkliftDTO.setNameplateDate(forkliftTemporaryTableDO.getNameplateTime());
            factoryForkliftDTO.setRemark(forkliftTemporaryTableDO.getRemark());

            // -----------  叉车表 -----------
            factoryForkliftDTOS.add(factoryForkliftDTO);
            deptAssetsDOS.add(deptAssetsDO);
            deptAssetsInOutStockDOS.add(deptAssetsInOutStockDO);
            assetsDOS.add(assetsDO);
        }
        if (CollUtil.isNotEmpty(factoryForkliftDTOS)) {
            factoryForkliftApi.batchSave(factoryForkliftDTOS);
        }
        if (CollUtil.isNotEmpty(assetsDOS)) {
            assetsService.saveBatch(assetsDOS);
        }
        if (CollUtil.isNotEmpty(deptAssetsDOS)) {
            deptAssetsService.saveBatch(deptAssetsDOS);
        }
        if (CollUtil.isNotEmpty(deptAssetsInOutStockDOS)) {
            deptAssetsInOutStockService.saveBatch(deptAssetsInOutStockDOS);
        }
    }
}
