package org.ts.ddcs.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;
import org.ts.ddcs.business.lkszy.entity.FlowRateEntity;
import org.ts.ddcs.business.rtu.entity.RtuBaseInfo;
import org.ts.ddcs.log.LogHelper;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 龙口水资源
 *
 * @author dylan
 */
public class LkszyDataShareTask implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        try {
            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
            HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();

            Calendar calendar = Calendar.getInstance();

            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<RtuBaseInfo> rtuBaseInfoList = DataProcessHolder.newInstance().dataProcess.rtuService.rtuDataListByProjectId("2ffa0704d719439384d2e8cc2c99819d");
            for (RtuBaseInfo rtuBaseInfo : rtuBaseInfoList) {

                Date dt = new Date();
                calendar.setTime(dt);
                calendar.add(Calendar.HOUR_OF_DAY,-1);
                dt = calendar.getTime();
                if (opsHash.hasKey("lkszy.relaytime", rtuBaseInfo.getRtuCode())) {
                    String tm = opsHash.get("lkszy.relaytime", rtuBaseInfo.getRtuCode());
                    dt = timeFormat.parse(tm);
                }

                List<FlowRateEntity> flowRateEntityList = DataProcessHolder.newInstance().dataProcess.lkszyService.lastDetail(rtuBaseInfo.getRtuCode(), dt);
                if (null != flowRateEntityList && flowRateEntityList.size() > 0) {

                    for (FlowRateEntity flowRateEntity : flowRateEntityList) {
                        JSONObject postData = new JSONObject();
                        postData.put("sn", flowRateEntity.getMpCode());
                        JSONObject data = new JSONObject();
                        data.put("AddressCode", flowRateEntity.getMpCode());
                        if (flowRateEntity.getTs() != null) {
                            data.put("CreateDate", timeFormat.format(new Date(flowRateEntity.getTs().getTime())));
                        }
                        if (flowRateEntity.getTm() != null) {
                            data.put("DeviceDate", timeFormat.format(new Date(flowRateEntity.getTm().getTime())));
                        }
                        JSONArray fields = new JSONArray();
                        JSONObject field = new JSONObject();
                        field.put("Description", "瞬时流量");
                        field.put("FieldName", "F1");
                        field.put("FieldUnit", "");
                        if (flowRateEntity.getInstantFlowrateFirst() != null) {
                            field.put("FieldValue", flowRateEntity.getInstantFlowrateFirst());
                        } else {
                            field.put("FieldValue", "");
                        }
                        field.put("Index", 1L);
                        field.put("IsAlarm", false);
                        field.put("IsSetAlarm", false);
                        field.put("LowerLimit", 0.0);
                        field.put("UpperLimit", 0.0);
                        fields.add(field);
                        field = new JSONObject();
                        field.put("Description", "净累积流量");
                        field.put("FieldName", "F3");
                        field.put("FieldUnit", "");
                        if (flowRateEntity.getAccFlowrateFirst() != null) {
                            field.put("FieldValue", flowRateEntity.getAccFlowrateFirst());
                        } else {
                            field.put("FieldValue", "");
                        }
                        field.put("Index", 1L);
                        field.put("IsAlarm", false);
                        field.put("IsSetAlarm", false);
                        field.put("LowerLimit", 0.0);
                        field.put("UpperLimit", 0.0);
                        fields.add(field);
                        data.put("Fields", fields);
                        postData.put("data", data);
                        LogHelper.info("post: " + postData.toJSONString());

                        String httpUrl = "http://123.57.229.166:5001/device/push?project=1208lksl";
                        RestTemplate restTemplate = new RestTemplate();
                        JSONObject result = restTemplate.postForObject(new URI(httpUrl), postData, JSONObject.class);

                        LogHelper.info(result.toJSONString());
                    }

                    FlowRateEntity lastFlowRateEntity=  flowRateEntityList.get(flowRateEntityList.size()-1);
                    String relaytime = timeFormat.format(new Date(lastFlowRateEntity.getTm().getTime()));
                    opsHash.put("lkszy.relaytime",lastFlowRateEntity.getMpCode(),relaytime);

                }
            }

        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
