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.SGConPlantBMapper;
import com.njcn.relational.pojo.po.SGConPlantB;
import com.njcn.relational.service.SGConPlantBService;
import org.springframework.stereotype.Service;

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

@Service
public class SGConPlantBServiceImpl extends ServiceImpl<SGConPlantBMapper, SGConPlantB>
        implements SGConPlantBService {

    private static final int BATCH_SIZE = 500;

    @Override
    public boolean batchSyncFromDcloud(JSONArray dataArr) {
        if (CollUtil.isEmpty(dataArr)) {
            return true;
        }
        int batchSize = 500;
        List<SGConPlantB> entityList = new ArrayList<>(dataArr.size());
        for (Object obj : dataArr) {
            JSONObject json = (JSONObject) obj;
            SGConPlantB plant = new SGConPlantB();

            // 主键必存在
            if (json.containsKey("ID")) {
                plant.setId(json.getString("ID"));
            }

            // 字符串字段，存在则赋值
            if (json.containsKey("ADDRESS")) plant.setAddress(json.getString("ADDRESS"));
            if (json.containsKey("CHECK_CODE")) plant.setCheckCode(json.getString("CHECK_CODE"));
            if (json.containsKey("COMM_TAG")) plant.setCommTag(json.getString("COMM_TAG"));
            if (json.containsKey("COMPANY_ID")) plant.setCompanyId(json.getString("COMPANY_ID"));
            if (json.containsKey("CONNECTIVE_PG_ID")) plant.setConnectivePgId(json.getString("CONNECTIVE_PG_ID"));
            if (json.containsKey("DCC_ID")) plant.setDccId(json.getString("DCC_ID"));
            if (json.containsKey("FAX_NO")) plant.setFaxNo(json.getString("FAX_NO"));
            if (json.containsKey("GENCOMPANY_ID")) plant.setGencompanyId(json.getString("GENCOMPANY_ID"));
            if (json.containsKey("NAME")) plant.setName(json.getString("NAME"));
            if (json.containsKey("NAME_ABBREVIATION")) plant.setNameAbbreviation(json.getString("NAME_ABBREVIATION"));
            if (json.containsKey("OWNER")) plant.setOwner(json.getString("OWNER"));
            if (json.containsKey("PHONE_NO")) plant.setPhoneNo(json.getString("PHONE_NO"));
            if (json.containsKey("POSTCODE")) plant.setPostcode(json.getString("POSTCODE"));
            if (json.containsKey("REGISTER_NAME")) plant.setRegisterName(json.getString("REGISTER_NAME"));
            if (json.containsKey("REMARKS")) plant.setRemarks(json.getString("REMARKS"));
            if (json.containsKey("SCS_TAG")) plant.setScsTag(json.getString("SCS_TAG"));
            if (json.containsKey("STAMP")) plant.setStamp(json.getString("STAMP"));
            if (json.containsKey("STATE_CODE")) plant.setStateCode(json.getString("STATE_CODE"));
            if (json.containsKey("SYS_FLAG")) plant.setSysFlag(json.getString("SYS_FLAG"));
            if (json.containsKey("TAG")) plant.setTag(json.getString("TAG"));

            // Int数字字段
            if (json.containsKey("ASSETS_OWNERSHIP")) plant.setAssetsOwnership(json.getInteger("ASSETS_OWNERSHIP"));
            if (json.containsKey("LANDFORM")) plant.setLandform(json.getInteger("LANDFORM"));
            if (json.containsKey("MAX_VOLTAGE_TYPE")) plant.setMaxVoltageType(json.getInteger("MAX_VOLTAGE_TYPE"));
            if (json.containsKey("OPERATE_STATE")) plant.setOperateState(json.getInteger("OPERATE_STATE"));
            if (json.containsKey("PLANT_DETAIL_TYPE")) plant.setPlantDetailType(json.getInteger("PLANT_DETAIL_TYPE"));
            if (json.containsKey("PLANT_TYPE")) plant.setPlantType(json.getInteger("PLANT_TYPE"));
            if (json.containsKey("REGION")) plant.setRegion(json.getInteger("REGION"));

            // BigDecimal 浮点字段
            if (json.containsKey("ALTITUDE")) plant.setAltitude(json.getBigDecimal("ALTITUDE"));
            if (json.containsKey("LATITUDE")) plant.setLatitude(json.getBigDecimal("LATITUDE"));
            if (json.containsKey("LONGITUDE")) plant.setLongitude(json.getBigDecimal("LONGITUDE"));

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

            entityList.add(plant);
        }
        // 根据主键ID存在更新，不存在新增
        return saveOrUpdateBatch(entityList, batchSize);
    }
}
