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.extension.service.impl.ServiceImpl;

import com.njcn.relational.mapper.SgConPlantCMapper;
import com.njcn.relational.pojo.po.SgConPlantC;
import com.njcn.relational.service.SgConPlantCService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class SgConPlantCServiceImpl extends ServiceImpl<SgConPlantCMapper, SgConPlantC> implements SgConPlantCService {

    @Override
    public boolean batchSyncFromDcloud(JSONArray dataArr) {
        if (CollUtil.isEmpty(dataArr)) {
            return true;
        }
        List<SgConPlantC> entityList = new ArrayList<>(dataArr.size());
        for (Object obj : dataArr) {
            JSONObject json = (JSONObject) obj;
            SgConPlantC plant = new SgConPlantC();
            plant.setD5000Id(json.getString("D5000_ID"));
            plant.setD5000Name(json.getString("D5000_NAME"));
            plant.setDcloudId(json.getString("DCLOUD_ID"));
            plant.setDcloudName(json.getString("DCLOUD_NAME"));
            plant.setDcloudVoltagelevel(json.getString("DCLOUD_VOLTAGELEVEL"));
            plant.setOwner(json.getString("OWNER"));
            plant.setStatus(json.getString("STATUS"));
            plant.setUpdateTime(json.getString("UPDATE_TIME"));
            entityList.add(plant);
        }
        // 批次500条，复合主键冲突自动更新
        return saveOrUpdateBatch(entityList, 500);
    }

    /**
     * 根据双主键查询单条
     */
    public SgConPlantC getByPk(String d5000Id, String dcloudId) {
        LambdaQueryWrapper<SgConPlantC> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SgConPlantC::getD5000Id, d5000Id)
                .eq(SgConPlantC::getDcloudId, dcloudId);
        return getOne(wrapper);
    }

    /**
     * 根据双主键删除
     */
    public boolean removeByPk(String d5000Id, String dcloudId) {
        LambdaQueryWrapper<SgConPlantC> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SgConPlantC::getD5000Id, d5000Id)
                .eq(SgConPlantC::getDcloudId, dcloudId);
        return remove(wrapper);
    }

    /**
     * 根据双主键更新
     */
    public boolean updateByPk(SgConPlantC entity) {
        LambdaQueryWrapper<SgConPlantC> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SgConPlantC::getD5000Id, entity.getD5000Id())
                .eq(SgConPlantC::getDcloudId, entity.getDcloudId());
        return update(entity, wrapper);
    }
}