package com.rongwei.bscommon.sys.service.impl;

import com.rongwei.bscommon.sys.service.EquipmentDisposeService;
import com.rongwei.rwadmincommon.system.domain.SysDictDo;
import com.rongwei.rwadmincommon.system.service.SysDictService;
import com.rongwei.rwcommon.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EquipmentDisposeServiceImpl implements EquipmentDisposeService {

    @Autowired
    private SysDictService dictService;
    @Override
    public R dealExcelData(Map<String, Object> map) {
        ArrayList<List<Map<String,Object>>> listall = new ArrayList<>();
        List<Map<String,Object>> main = new ArrayList<>();
        List<Map<String,Object>> list= (List<Map<String, Object>>) map.get("excelExportData");
        Map<String, Object> objectMap = list.get(0);
        // 处理时间年月日数据
        processTimeData(objectMap);
        //检查checkbox 数据
        processCheckBoxData(objectMap);

        main.add(objectMap);
        listall.add(main);
        return R.ok(listall);
    }

    private void processCheckBoxData(Map<String, Object> objectMap) {
       
        //1.处理类型  报废  转移    ☑
        if( objectMap.get("HANDLINGTYPE")!=null){
       //     log.info("objectMap.get(\"HANDLINGTYPE\") {} ",objectMap.get("HANDLINGTYPE").toString());
            StringBuilder stringBuilder = new StringBuilder();
            List<SysDictDo> dictList = dictService.getDictsByType("handling_type").stream().filter(dict ->!dict.getPid().equals("-1")).collect(Collectors.toList());
            String space ="         ";
            for (int i = 0; i < dictList.size(); i++) {
                SysDictDo item = dictList.get(i);
                if ( item.getName().trim().equals(objectMap.get("HANDLINGTYPE").toString().trim())){
                    stringBuilder.append("√").append(item.getName().trim());
                }else {
                    stringBuilder.append("□").append(item.getName().trim());
                };
                if (i==0){
                    stringBuilder.append(space);
                }
            }

            objectMap.put("HANDLINGTYPE",stringBuilder.toString());
         //   log.info("HANDLINGTYPE {} ",stringBuilder.toString());
        }

        //2.原因
        if( objectMap.get("REASONDISPOSAL")!=null){
            StringBuilder stringBuilder = new StringBuilder();
            List<SysDictDo> dictList = dictService.getDictsByType("refuse_disposal");
            dictList.forEach(item ->{
                if (!item.getPid().equals("-1") && item.getName().trim().equals(objectMap.get("REASONDISPOSAL").toString().trim())){
                    stringBuilder.append("√").append(item.getName().trim());
                }else if(!item.getPid().equals("-1") ){
                    stringBuilder.append("□").append(item.getName().trim());
                }
            });
            objectMap.put("REASONDISPOSAL",stringBuilder.toString());
         //   log.info("@REASONDISPOSAL {} ",stringBuilder.toString());
        }
    }

    /**
     * 处理时间数据
     * @param objectMap
     */
    private void processTimeData(Map<String, Object> objectMap) {
        String[] updateFields ={"ROLLINLOCATION","MSSMANAGERTIME","MTOMANAGERTIME","EQUMANAGERTIME","WDMANAGERTIME","VICEPRESIDENTTIME","GENERALMANAGERTIME","FINANCEOFFICETIME"};
        for (String fieldName : updateFields) {
            Object value = objectMap.get(fieldName);
            if (value != null && value instanceof String) {
                String dateStr = (String) value;
                String formattedDate = dateStr.replaceAll("-", "/").substring(0, 10);
                objectMap.put(fieldName, formattedDate);
            } else {
                objectMap.put(fieldName, ""); // 或者您可以选择不更新该字段
            }
        }
    }
}
