package com.steerinfo.ems.ifmesemswatermonitor.service.impl;

import com.steerinfo.ems.ifmesemswatermonitor.mapper.IfMesEmsWatermonitorMapper;
import com.steerinfo.ems.ifmesemswatermonitor.model.IfMesEmsWatermonitor;
import com.steerinfo.ems.ifmesemswatermonitor.service.IIfMesEmsWatermonitorService;
import com.steerinfo.ems.Utils.HttpRequestMes;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * IfMesEmsWatermonitor服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2019-12-02 02:52
 * 类描述
 * 修订历史：
 * 日期：2019-12-02
 * 作者：generator
 * 参考：
 * 描述：IfMesEmsWatermonitor服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "ifMesEmsWatermonitorService")
public class IfMesEmsWatermonitorServiceImpl extends BaseServiceImpl<IfMesEmsWatermonitor, String> implements IIfMesEmsWatermonitorService {

    private static final Logger logger = LoggerFactory.getLogger(IfMesEmsWatermonitorServiceImpl.class);

    @Autowired
    private IfMesEmsWatermonitorMapper ifMesEmsWatermonitorMapper;

    @Override
    protected IBaseMapper<IfMesEmsWatermonitor, String> getMapper() {
        return ifMesEmsWatermonitorMapper;
    }

    public List<IfMesEmsWatermonitor> getNewData(){
        return ifMesEmsWatermonitorMapper.getNewData();
    }

    public int updateStatus(Map<String, Object> params){
        return ifMesEmsWatermonitorMapper.updateStatus(params);
    }

    public int synchronousMesAnEmsData() {
        int addSize = 0;
        int updateSize = 0;
        int errorSize = 0;
        String url = "http://172.16.100.91:8093/mes_interface/qcdata"; // 水质监测数据
        String res = HttpRequestMes.httpRequestMes(url, "get", null);
        if(res == null || "".equals(res)){
            logger.error("mes系统未传数据过来");
            return -1;
        }
        JSONArray ja = JSONArray.parseArray(res);
        logger.info("获取水质监测数据条数：" + ja.size());
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jo = (JSONObject) ja.get(i);
            if(!jo.containsKey("wtbh") || !jo.containsKey("materialno") || !jo.containsKey("itemcode")){
                errorSize += 1;
                continue;
            }
            IfMesEmsWatermonitor model = new IfMesEmsWatermonitor();
            model.jsonobjectToIfMesEmsWatermonitor(jo);
            if (ifMesEmsWatermonitorMapper.updateByPrimaryKey(model) == 0){
                addSize += ifMesEmsWatermonitorMapper.insert(model);
            }else{
                updateSize +=1;
            }
        }
        logger.info("本次总共新增水质监测数据条数：" + addSize);
        logger.info("本次总共更新水质监测数据条数：" + updateSize);
        logger.info("本次总共错误水质监测数据条数：" + errorSize);
        return (addSize + updateSize);
    }
}
