package com.chinaitop.depot.intelligent.basicdata.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TKtinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.mapper.TStationtypeMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfoExample;
import com.chinaitop.depot.intelligent.basicdata.model.TStationtype;
import com.chinaitop.depot.intelligent.basicdata.model.TStationtypeExample;
import com.chinaitop.depot.intelligent.basicdata.service.TStationtypeService;
import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergInfoLocal;
import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergyInfo;
import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergyInfoExample;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.unissoft.model.BasicEntity;
import com.unissoft.model.SendEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class TStationtypeServiceImpl implements TStationtypeService {

    @Resource
    private TStationtypeMapper tStationtypeMapper;

    @Override
    public List<TStationtype> getList() {
        TStationtypeExample example = new TStationtypeExample();
        return tStationtypeMapper.selectByExample(example);
    }

    @Transactional
    @Override
    public String synchronizationAll(String orgId) {
        //获取所有基础数据getSiteType
        String device = SendEntity.cxfFactor().getSiteType();
        //转换对象
        BasicEntity basicEntity = (BasicEntity) JsonToObjectUtils.jsonToObject(device, BasicEntity.class);
        if (basicEntity.getState().equals("0")) {
            List<Object> data = basicEntity.getData();
            if (data.size() > 0) {
                String tt = JSONObject.toJSONString(data);
                ArrayList<Object> objects = JsonToObjectUtils.jsonToObjectsDiverse(tt, TStationtype.class, "yyyy-MM-dd'T'HH:mm:ss");
                objects.forEach(t -> {
                    TStationtype tc = (TStationtype) t;
                    String vdevcode = tc.getvZdTypeCode();
                    TStationtype info1 = getVinfo(vdevcode);
                    if (info1 != null) {
                        tc.setId(info1.getId());
                        tStationtypeMapper.updateByPrimaryKey(tc);
                    } else {
                        tStationtypeMapper.insert(tc);
                    }
                });
            }
        } else {
            return basicEntity.getMsg();
        }
        return basicEntity.getMsg();
    }

    public TStationtype getVinfo(String vdevcode) {
        TStationtypeExample example = new TStationtypeExample();
        TStationtypeExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVZdTypeCodeEqualTo(vdevcode);
        }
        List<TStationtype> infos = tStationtypeMapper.selectByExample(example);
        if (infos.size() > 0) {
            return infos.get(0);
        }
        return null;
    }
}
