package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ActSuppliesImportCleaningAcc;
import com.ruoyi.system.domain.ActSuppliesMaterialDetails;
import com.ruoyi.system.domain.ActSuppliesMaterialInfo;
import com.ruoyi.system.mapper.ActSuppliesCleaningRuleMapper;
import com.ruoyi.system.mapper.ActSuppliesMaterialMapper;
import com.ruoyi.system.service.ActMaterialService;
import com.ruoyi.system.service.ActSuppliesMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author haiwe
 * @date 2024/7/8
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ActSuppliesMaterialServiceImpl implements ActSuppliesMaterialService {
    private final ActSuppliesMaterialMapper actSuppliesMaterialMapper;
    private final ActSuppliesCleaningRuleMapper actSuppliesCleaningRuleMapper;


    @Override
    public Boolean save(List<ActSuppliesMaterialDetails> materials) {

        Stream<ActSuppliesMaterialDetails> materialStream = materials.stream();
        //将mineId和dateColumn拼接之后进行去重
        Set<String> uniqueCombos = materialStream
                .map(material -> material.getMineId() + "_" + material.getDateColumn()) // Create comboKey
                .collect(Collectors.toSet());
        for (String uniqueCombo : uniqueCombos) {
            String[] part = uniqueCombo.split("_");
            String mineId = part[0];
            String dateColumn = part[1];
            //执行删除操作
            actSuppliesMaterialMapper.delete(mineId, dateColumn);
            actSuppliesCleaningRuleMapper.deleteByMineIdAndDateColumn(mineId,dateColumn);
        }
        List<ActSuppliesImportCleaningAcc> cleaningAccList = new ArrayList<>();
        for (ActSuppliesMaterialDetails material : materials) {
            if (material != null) { // 检查 material 是否为 null
                ActSuppliesImportCleaningAcc cleaningAcc = new ActSuppliesImportCleaningAcc();
                cleaningAcc.setId(UUID.randomUUID().toString().replace("-", ""));
                cleaningAcc.setSskId(material.getMineId() != null ? material.getMineId() : ""); // 处理可能的 null
                cleaningAcc.setHdate(material.getDateColumn() != null ? material.getDateColumn() : "");
                cleaningAcc.setNameColumn(material.getNameColumn() != null ? material.getNameColumn() : "");
                cleaningAcc.setCodeColumn(material.getCodeColumn() != null ? material.getCodeColumn() : "");
                cleaningAcc.setDepartmentColumn(material.getDepartmentColumn() != null ? material.getDepartmentColumn() : "");
                cleaningAcc.setNumberColumn(material.getNumberColumn() != null ? String.valueOf(material.getNumberColumn()) : "");
                cleaningAcc.setMoneyColumn(material.getMoneyColumn() != null ? material.getMoneyColumn().toString() : "0");
                cleaningAcc.setMoneyColumns(material.getMoney() != null ? material.getMoney().toString() : "0");
                cleaningAcc.setsOfExpenses(material.getSourceColoumn() != null ? material.getSourceColoumn() : "");
                cleaningAcc.setUnit(material.getUnit() != null ? material.getUnit() : "");
                cleaningAcc.setModel(material.getModel() != null ? material.getModel() : "");
                cleaningAccList.add(cleaningAcc);
            }
        }

        actSuppliesCleaningRuleMapper.insert(cleaningAccList);
        Boolean saved = actSuppliesMaterialMapper.save(materials);
        if (!saved) {
            return false;
        }
        return true;
    }
}
