package com.njcn.relational.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.relational.mapper.SGConSubstationBMapper;
import com.njcn.relational.pojo.po.SGConSubstationB;
import com.njcn.relational.service.SGConSubstationBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SGConSubstationBServiceImpl extends ServiceImpl<SGConSubstationBMapper, SGConSubstationB>
        implements SGConSubstationBService {

    private static final int BATCH_SIZE = 500;

    @Override
    public boolean batchSyncFromDcloud(JSONArray dataArr) throws Exception{
        if (CollUtil.isEmpty(dataArr)) {
            return true;
        }
        List<SGConSubstationB> entityList = new ArrayList<>(dataArr.size());
        for (Object obj : dataArr) {
            JSONObject json = (JSONObject) obj;
            SGConSubstationB sub = new SGConSubstationB();

            // 主键ID必传
            if (json.containsKey("ID")) {
                sub.setId(json.getString("ID"));
            }

            // 字符串字段，存在才赋值
            if (json.containsKey("ASSETS_OWNERSHIP_COM_ID")) {
                sub.setAssetsOwnershipComId(json.getString("ASSETS_OWNERSHIP_COM_ID"));
            }
            if (json.containsKey("CHECK_CODE")) {
                sub.setCheckCode(json.getString("CHECK_CODE"));
            }
            if (json.containsKey("COMM_TAG")) {
                sub.setCommTag(json.getString("COMM_TAG"));
            }
            if (json.containsKey("DCC_ID")) {
                sub.setDccId(json.getString("DCC_ID"));
            }
            if (json.containsKey("MANAGE_DEPT_ID")) {
                sub.setManageDeptId(json.getString("MANAGE_DEPT_ID"));
            }
            if (json.containsKey("NAME")) {
                sub.setName(json.getString("NAME"));
            }
            if (json.containsKey("OWNER")) {
                sub.setOwner(json.getString("OWNER"));
            }
            if (json.containsKey("PG_ID")) {
                sub.setPgId(json.getString("PG_ID"));
            }
            if (json.containsKey("SCS_TAG")) {
                sub.setScsTag(json.getString("SCS_TAG"));
            }
            if (json.containsKey("STATE_CODE")) {
                sub.setStateCode(json.getString("STATE_CODE"));
            }
            if (json.containsKey("SYS_FLAG")) {
                sub.setSysFlag(json.getString("SYS_FLAG"));
            }
            if (json.containsKey("VIDEOSCENEID")) {
                sub.setVideosceneid(json.getString("VIDEOSCENEID"));
            }

            // 整型数字字段
            if (json.containsKey("ASSETS_OWNERSHIP")) {
                sub.setAssetsOwnership(json.getInteger("ASSETS_OWNERSHIP"));
            }
            if (json.containsKey("DC_VOLTAGE_TYPE")) {
                sub.setDcVoltageType(json.getInteger("DC_VOLTAGE_TYPE"));
            }
            if (json.containsKey("OPERATE_STATE")) {
                sub.setOperateState(json.getInteger("OPERATE_STATE"));
            }
            if (json.containsKey("REGION")) {
                sub.setRegion(json.getInteger("REGION"));
            }
            if (json.containsKey("TOP_AC_VOLTAGE_TYPE")) {
                sub.setTopAcVoltageType(json.getInteger("TOP_AC_VOLTAGE_TYPE"));
            }
            if (json.containsKey("TYPE")) {
                sub.setType(json.getInteger("TYPE"));
            }

            // 高精度小数
            if (json.containsKey("ALTITUDE")) {
                sub.setAltitude(json.getBigDecimal("ALTITUDE"));
            }
            if (json.containsKey("LATITUDE")) {
                sub.setLatitude(json.getBigDecimal("LATITUDE"));
            }
            if (json.containsKey("LONGITUDE")) {
                sub.setLongitude(json.getBigDecimal("LONGITUDE"));
            }

            // 日期字段 字符串转Date
            if (json.containsKey("EXPIRY_DATE")) {
                String dateStr = json.getString("EXPIRY_DATE");
                if (StrUtil.isNotBlank(dateStr)) {
                    sub.setExpiryDate(DateUtil.parseDate(dateStr));
                }
            }
            if (json.containsKey("OPERATE_DATE")) {
                String dateStr = json.getString("OPERATE_DATE");
                if (StrUtil.isNotBlank(dateStr)) {
                    sub.setOperateDate(DateUtil.parseDate(dateStr));
                }
            }

            entityList.add(sub);
        }
        // 根据主键ID判断新增/更新
        return saveOrUpdateBatch(entityList, BATCH_SIZE);
    }
}
