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.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.relational.mapper.SGConSubstationCMapper;
import com.njcn.relational.pojo.po.SGConSubstationBind;
import com.njcn.relational.service.SGConSubstationCService;
import org.springframework.stereotype.Service;

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

@Service
public class SGConSubstationCServiceImpl extends ServiceImpl<SGConSubstationCMapper, SGConSubstationBind>
        implements SGConSubstationCService {

    /**
     * 根据联合主键(DCC_ID + DCLOUD_ID)查询单条映射
     */
    public SGConSubstationBind getByUnionKey(String dccId, String dcloudId) {
        LambdaQueryWrapper<SGConSubstationBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SGConSubstationBind::getDccId, dccId)
                .eq(SGConSubstationBind::getDcloudId, dcloudId)
                .eq(SGConSubstationBind::getIsDelete, 0);
        return getOne(wrapper);
    }

    /**
     * 逻辑删除：根据联合主键软删
     */
    public boolean logicRemoveByUnionKey(String dccId, String dcloudId) {
        LambdaUpdateWrapper<SGConSubstationBind> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SGConSubstationBind::getIsDelete, 1)
                .eq(SGConSubstationBind::getDccId, dccId)
                .eq(SGConSubstationBind::getDcloudId, dcloudId);
        return update(updateWrapper);
    }

    private static final int BATCH_SIZE = 500;

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

            // 联合主键两字段必存在
            if (json.containsKey("DCC_ID")) {
                bind.setDccId(json.getString("DCC_ID"));
            }
            if (json.containsKey("DCLOUD_ID")) {
                bind.setDcloudId(json.getString("DCLOUD_ID"));
            }

            // 字符串字段，存在key才赋值
            if (json.containsKey("D5000_ID")) bind.setD5000Id(json.getString("D5000_ID"));
            if (json.containsKey("D5000_NAME")) bind.setD5000Name(json.getString("D5000_NAME"));
            if (json.containsKey("DCLOUD_NAME")) bind.setDcloudName(json.getString("DCLOUD_NAME"));
            if (json.containsKey("DCLOUD_VOLTAGELEVEL")) bind.setDcloudVoltagelevel(json.getString("DCLOUD_VOLTAGELEVEL"));
            if (json.containsKey("OMS_ID")) bind.setOmsId(json.getString("OMS_ID"));
            if (json.containsKey("OMS_NAME")) bind.setOmsName(json.getString("OMS_NAME"));
            if (json.containsKey("OWNER")) bind.setOwner(json.getString("OWNER"));
            if (json.containsKey("PMS_ID")) bind.setPmsId(json.getString("PMS_ID"));
            if (json.containsKey("PSDB_DEV_TYPE")) bind.setPsdbDevType(json.getString("PSDB_DEV_TYPE"));
            if (json.containsKey("PSDB_ID")) bind.setPsdbId(json.getString("PSDB_ID"));
            if (json.containsKey("PSDB_NAME")) bind.setPsdbName(json.getString("PSDB_NAME"));
            if (json.containsKey("SETTING_ID")) bind.setSettingId(json.getString("SETTING_ID"));
            if (json.containsKey("SETTING_NAME")) bind.setSettingName(json.getString("SETTING_NAME"));
            if (json.containsKey("STATUS")) bind.setStatus(json.getString("STATUS"));

            // 数字字段
            if (json.containsKey("IS_DELETE")) bind.setIsDelete(json.getInteger("IS_DELETE"));

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