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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.FactoryInfoApi;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
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.packagingtoolstable.PackagingToolsTableDO;
import cn.iocoder.yudao.module.system.dal.mysql.packagingtoolstable.PackagingToolsTableMapper;
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.toolkit.IdWorker;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 木托绑带资产 （excel 临时表） Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class PackagingToolsTableServiceImpl extends ServiceImpl<PackagingToolsTableMapper, PackagingToolsTableDO> implements PackagingToolsTableService {

    @Resource
    private PackagingToolsTableMapper packagingToolsTableMapper;
    @Resource
    private FactoryInfoApi factoryInfoApi;
    @Resource
    private DeptService deptService;
    @Resource
    private AssetsTypeService assetsTypeService;
    @Resource
    private AssetsService assetsService;
    @Resource
    private DeptAssetsService deptAssetsService;
    @Resource
    private DeptAssetsInOutStockService deptAssetsInOutStockService;
    @Override
    public void test() {
        List<PackagingToolsTableDO> packagingToolsTableDOS = packagingToolsTableMapper.selectList();
        //首先进入资产表
        // 需要做新增操作的列表 ------ 开始
        List<AssetsDO> assetsDOS = new ArrayList<>();
        List<DeptAssetsInOutStockDO> deptAssetsInOutStockDOS = new ArrayList<>();
        List<DeptAssetsDO> deptAssetsDOS = 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));

        List<AssetsTypeDO> assetsTypeDOS = assetsTypeService.getAllList(2);
        for (PackagingToolsTableDO packagingToolsTableDO : packagingToolsTableDOS) {
            // -- 我这里只有工厂名称 - 我要通过工厂名称 找到工厂id 再通过工厂id 拿到对应的部门id
            Long factoryId = factoryMap.get(packagingToolsTableDO.getFactoryName().trim());
            Long deptId = null;
            if (factoryId != null) {
                deptId = factoryDeptMap.get(factoryId);
            }
            if (deptId == null) {
                continue;
            }

            // -----------  资产表 -----------
            AssetsDO assetsDO = new AssetsDO();
            assetsDO.setId(IdWorker.getId());
            assetsDO.setName(packagingToolsTableDO.getName());
            // 公司名称 + 用途 + 类别 + 名称 + 日期 + 流水号
//            String assetsNo = ChineseConversionUtils.getChineseInitials(forkliftTemporaryTableDO.getFactoryName()) + "_" +
//                    ChineseConversionUtils.getChineseInitials(forkliftTemporaryTableDO.getType()) + "_" +
//                    ChineseConversionUtils.getChineseInitials();
            assetsDO.setAssetsNo(IdWorker.get32UUID());
            assetsDO.setStatus(-1);
            // -- 根据名称 + sku 计算得出类型id
            assetsDO.setStock(0);
            // -- 先根据名称过滤一道 - 再根据sku 过滤一道
            List<AssetsTypeDO> assetsTypeDOList = assetsTypeDOS.stream()
                    .filter(a -> a.getName().contains(packagingToolsTableDO.getName())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(assetsTypeDOList) && StrUtil.isNotEmpty(packagingToolsTableDO.getSku())) {
                assetsTypeDOList = assetsTypeDOList.stream().filter(a -> a.getName().contains(packagingToolsTableDO.getSku())).collect(Collectors.toList());
            }
            if (CollUtil.isEmpty(assetsTypeDOList)) {
                continue;
            }
            assetsDO.setTypeId(assetsTypeDOList.get(0).getId());
            // -----------  资产表 -----------

            // -----------  资产出入库记录表 -----------
            DeptAssetsInOutStockDO deptAssetsInOutStockDO = new DeptAssetsInOutStockDO();
            deptAssetsInOutStockDO.setInDeptId(deptId);
            deptAssetsInOutStockDO.setOutDeptId(-1L);
            deptAssetsInOutStockDO.setAssetsId(assetsDO.getId());
            deptAssetsInOutStockDO.setNum(packagingToolsTableDO.getTotalNum());
            deptAssetsInOutStockDO.setInOutNo(IdWorker.get32UUID());
            // -----------  资产出入库记录表 -----------


            // -----------  部门资产表 -----------
            DeptAssetsDO deptAssetsDO = new DeptAssetsDO();
            deptAssetsDO.setId(IdWorker.getId());
            deptAssetsDO.setDeptId(deptId);
            deptAssetsDO.setAssetsId(assetsDO.getId());
            deptAssetsDO.setStatus(1);
            deptAssetsDO.setStock(packagingToolsTableDO.getTotalNum());
            // -----------  部门资产表 -----------

            // -----------  工厂固定资产表 -----------
            assetsDOS.add(assetsDO);
            deptAssetsInOutStockDOS.add(deptAssetsInOutStockDO);
            deptAssetsDOS.add(deptAssetsDO);
        }

        if (CollUtil.isNotEmpty(assetsDOS)) {
            assetsService.saveBatch(assetsDOS);
        }
        if (CollUtil.isNotEmpty(deptAssetsDOS)) {
            deptAssetsService.saveBatch(deptAssetsDOS);
        }
        if (CollUtil.isNotEmpty(deptAssetsInOutStockDOS)) {
            deptAssetsInOutStockService.saveBatch(deptAssetsInOutStockDOS);
        }

    }
}
