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

import cn.hutool.core.collection.CollUtil;
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.linefixedassets.LineFixedAssetsApi;
import cn.iocoder.yudao.module.smartfactory.api.linefixedassets.dto.LineFixedAssetsDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.AssetsDO;
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.linefixedassetstemporarytable.LineFixedAssetsTemporaryTableDO;
import cn.iocoder.yudao.module.system.dal.mysql.linefixedassetstemporarytable.LineFixedAssetsTemporaryTableMapper;
import cn.iocoder.yudao.module.system.service.assets.AssetsService;
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;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 打包线固定资产（excel 临时表） Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class LineFixedAssetsTemporaryTableServiceImpl extends ServiceImpl<LineFixedAssetsTemporaryTableMapper, LineFixedAssetsTemporaryTableDO> implements LineFixedAssetsTemporaryTableService {

    @Resource
    private LineFixedAssetsTemporaryTableMapper lineFixedAssetsTemporaryTableMapper;
    @Resource
    private FactoryInfoApi factoryInfoApi;
    @Resource
    private DeptService deptService;
    @Resource
    private LineFixedAssetsApi lineFixedAssetsApi;
    @Resource
    private AssetsService assetsService;
    @Resource
    private DeptAssetsService deptAssetsService;
    @Resource
    private DeptAssetsInOutStockService deptAssetsInOutStockService;

    @Override
    public void test() {
        List<LineFixedAssetsTemporaryTableDO> lineFixedAssetsTemporaryTableDOS = lineFixedAssetsTemporaryTableMapper.selectList();
        //首先进入资产表
        // 需要做新增操作的列表 ------ 开始
        List<AssetsDO> assetsDOS = new ArrayList<>();
        List<DeptAssetsInOutStockDO> deptAssetsInOutStockDOS = new ArrayList<>();
        List<DeptAssetsDO> deptAssetsDOS = new ArrayList<>();
        List<LineFixedAssetsDTO> lineFixedAssetsDTOS = 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));


        // -- 如果数量 大于 1 则再复制一条数据到列表中
        List<LineFixedAssetsTemporaryTableDO> resultList = lineFixedAssetsTemporaryTableDOS.stream()
                .flatMap(item -> {
                    if ("米".equals(item.getUnit())) {  // 判断单位为"米"
                        return Stream.of(item);          // 直接返回单个元素流
                    } else {
                        int copies = Math.max(Integer.parseInt(item.getNum()), 1);
                        return IntStream.range(0, copies).mapToObj(i -> item); // 正常复制
                    }
                })
                .collect(Collectors.toList());
        for (LineFixedAssetsTemporaryTableDO lineFixedAssetsTemporaryTableDO : resultList) {
            // -- 我这里只有工厂名称 - 我要通过工厂名称 找到工厂id 再通过工厂id 拿到对应的部门id
            Long factoryId = factoryMap.get(lineFixedAssetsTemporaryTableDO.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(lineFixedAssetsTemporaryTableDO.getName());
            // 公司名称 + 用途 + 类别 + 名称 + 日期 + 流水号
//            String assetsNo = ChineseConversionUtils.getChineseInitials(forkliftTemporaryTableDO.getFactoryName()) + "_" +
//                    ChineseConversionUtils.getChineseInitials(forkliftTemporaryTableDO.getType()) + "_" +
//                    ChineseConversionUtils.getChineseInitials();
            assetsDO.setAssetsNo(IdWorker.get32UUID());
            assetsDO.setStatus(0);
            assetsDO.setStock(0);
            // todo 这里的类型 到生产上可能会变 要注意
            assetsDO.setTypeId(37L);
            // -----------  资产表 -----------

            // -----------  资产出入库记录表 -----------
            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(lineFixedAssetsTemporaryTableDO.getStatus()) ? 1
                    : "维修".equals(lineFixedAssetsTemporaryTableDO.getStatus()) ? 2
                    : "备用".equals(lineFixedAssetsTemporaryTableDO.getStatus()) ? 0
                    : "停用".equals(lineFixedAssetsTemporaryTableDO.getStatus()) ? 4
                    : "报废".equals(lineFixedAssetsTemporaryTableDO.getStatus()) ? 3
                    : -1);
            deptAssetsDO.setStock(1);
            // -----------  部门资产表 -----------

            // -----------  工厂固定资产表 -----------
            LineFixedAssetsDTO lineFixedAssetsDTO = new LineFixedAssetsDTO();
            lineFixedAssetsDTO.setAssetsNo(assetsDO.getAssetsNo());
            lineFixedAssetsDTO.setFactoryId(factoryId);
            lineFixedAssetsDTO.setDeptId(deptId);
            lineFixedAssetsDTO.setName(lineFixedAssetsTemporaryTableDO.getName());
            lineFixedAssetsDTO.setSku(lineFixedAssetsTemporaryTableDO.getSku());
            lineFixedAssetsDTO.setLineNo(lineFixedAssetsTemporaryTableDO.getLineNo());
            lineFixedAssetsDTO.setModelNo(lineFixedAssetsTemporaryTableDO.getModelNo());
            lineFixedAssetsDTO.setManufacturer(lineFixedAssetsTemporaryTableDO.getManufacturer());
            lineFixedAssetsDTO.setUnit(lineFixedAssetsTemporaryTableDO.getUnit());
            lineFixedAssetsDTO.setNameplateDate(lineFixedAssetsTemporaryTableDO.getNameplateDate());
            lineFixedAssetsDTO.setUseDate(lineFixedAssetsTemporaryTableDO.getUseDate());
            lineFixedAssetsDTO.setRemark(lineFixedAssetsTemporaryTableDO.getRemark());
            // -----------  工厂固定资产表 -----------
            assetsDOS.add(assetsDO);
            deptAssetsInOutStockDOS.add(deptAssetsInOutStockDO);
            deptAssetsDOS.add(deptAssetsDO);
            lineFixedAssetsDTOS.add(lineFixedAssetsDTO);
        }
        if (CollUtil.isNotEmpty(assetsDOS)) {
            assetsService.saveBatch(assetsDOS);
        }
        if (CollUtil.isNotEmpty(deptAssetsDOS)) {
            deptAssetsService.saveBatch(deptAssetsDOS);
        }
        if (CollUtil.isNotEmpty(deptAssetsInOutStockDOS)) {
            deptAssetsInOutStockService.saveBatch(deptAssetsInOutStockDOS);
        }
        if (CollUtil.isNotEmpty(lineFixedAssetsDTOS)) {
            lineFixedAssetsApi.batchSave(lineFixedAssetsDTOS);
        }
    }
}
