package com.njcn.relational.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.relational.mapper.SgDevAclineBMapper;
import com.njcn.relational.pojo.po.SgDevAclineB;
import com.njcn.relational.service.SgDevAclineBService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cdf
 * @CreateTime: 2026-07-22
 * @Description:
 */
@Service
public class SgDevAclineBServiceImpl
        extends ServiceImpl<SgDevAclineBMapper, SgDevAclineB>
        implements SgDevAclineBService {


    @Override
    public boolean batchSyncFromDcloud(JSONArray dataArr) {
        if (CollUtil.isEmpty(dataArr)) {
            return true;
        }
        List<SgDevAclineB> entityList = new ArrayList<>(dataArr.size());
        for (Object obj : dataArr) {
            JSONObject json = (JSONObject) obj;
            SgDevAclineB entity = new SgDevAclineB();
            entity.setId(json.getString("ID"));
            entity.setName(json.getString("NAME"));
            entity.setVoltageType(json.getInteger("VOLTAGE_TYPE"));
            entity.setGridId(json.getString("GRID_ID"));
            entity.setSupplierId(json.getString("SUPPLIER_ID"));
            entity.setSysFlag(json.getString("SYS_FLAG"));
            entity.setTag(json.getString("TAG"));
            entity.setAssetsCompanyId(json.getString("ASSETS_COMPANY_ID"));
            entity.setCheckCode(json.getString("CHECK_CODE"));
            entity.setCompactZone(json.getString("COMPACT_ZONE"));
            entity.setDevId(json.getString("DEV_ID"));
            entity.setDispatchOrgId(json.getString("DISPATCH_ORG_ID"));
            entity.setStartBayId(json.getString("START_BAY_ID"));
            entity.setStartStId(json.getString("START_ST_ID"));
            entity.setStartTowerId(json.getString("START_TOWER_ID"));
            entity.setEndBayId(json.getString("END_BAY_ID"));
            entity.setEndStId(json.getString("END_ST_ID"));
            entity.setEndTowerId(json.getString("END_TOWER_ID"));
            entity.setErectingmethod(json.getInteger("ERECTINGMETHOD"));
            entity.setLength(json.getBigDecimal("LENGTH"));
            entity.setLicenseOrgId(json.getString("LICENSE_ORG_ID"));
            entity.setLinetype(json.getInteger("LINETYPE"));
            entity.setMemberFeatureTag(json.getString("MEMBER_FEATURE_TAG"));
            entity.setStrideFeatureTag(json.getString("STRIDE_FEATURE_TAG"));
            entity.setIsopticfiberequiped(json.getString("ISOPTICFIBEREQUIPED"));
            entity.setIsCompact(json.getString("IS_COMPACT"));
            entity.setOwner(json.getString("OWNER"));
            entity.setRunningState(json.getInteger("RUNNING_STATE"));
            entity.setStamp(json.getString("STAMP"));

            // LocalDate 日期字段判空处理
            if(json.containsKey("EXPIRY_DATE") && json.get("EXPIRY_DATE") != null){
                entity.setExpiryDate(json.getString("EXPIRY_DATE"));
            }
            if(json.containsKey("OFF_TIME") && json.get("OFF_TIME") != null){
                entity.setOffTime(json.getString("OFF_TIME"));
            }
            if(json.containsKey("ON_TIME") && json.get("ON_TIME") != null){
                entity.setOnTime(json.getString("ON_TIME"));
            }
            if(json.containsKey("OPERATE_DATE") && json.get("OPERATE_DATE") != null){
                entity.setOperateDate(json.getString("OPERATE_DATE"));
            }
            entityList.add(entity);
        }
        // 批次500条，主键冲突自动更新
        return saveOrUpdateBatch(entityList, 500);
    }

}
