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

import com.chinaitop.depot.intelligent.grainNumberMonitor.mapper.TNumberWarningHistoryMapper;
import com.chinaitop.depot.intelligent.grainNumberMonitor.mapper.TNumberWarningMapper;
import com.chinaitop.depot.intelligent.grainNumberMonitor.model.TNumberWarning;
import com.chinaitop.depot.intelligent.grainNumberMonitor.model.TNumberWarningExample;
import com.chinaitop.depot.intelligent.grainNumberMonitor.model.TNumberWarningHistory;
import com.chinaitop.depot.intelligent.grainNumberMonitor.model.TNumberWarningHistoryExample;
import com.chinaitop.depot.intelligent.grainNumberMonitor.service.MonitorWarningService;
import com.chinaitop.depot.intelligent.grainsituation.service.TemperatureRecordService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.RemoteControlUtil;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.wsClientJc.jc3.MyWebServiceSoap;
import com.unissoft.model.ResponseEntity;
import com.unissoft.model.SendEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qingsong.han
 * @description: 数量检测分析预警
 * @create 2019-02-22 16:01
 */
@Service
public class MonitorWarningServiceImpl implements MonitorWarningService {

    @Value("${num_version3}")
    private String num_version3 = ":20013";

    @Resource
    private TNumberWarningMapper numberWarningMapper;

    @Resource
    private TNumberWarningHistoryMapper numberWarningHistoryMapper;

    @Resource
    private UuidUtils uUidUtils;
    @Autowired
    private TemperatureRecordService temperatureRecordService;
    @Resource
    private RemoteControlUtil remoteControlUtil;

    /**
     * 列表
     *
     * @param vcfcode
     * @return
     */
    @Override
    public List<Map<String, Object>> getList(String vcfcode,Integer orgId) {
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isNotBlank(vcfcode))
            map.put("vcfcode", vcfcode);
        if (StringUtils.isNotBlank(orgId+""))
            map.put("orgId", orgId+"");


        return numberWarningMapper.getThisAndSetList(map);
    }

    /**
     * 根据外键添加或更新数据
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(String cfcode, String devcode, String operator, String orgId, String[] num) {
        String cfcode1 = numberWarningMapper.getCfName(cfcode).get(0).get("cfcode").toString();
        TNumberWarning nw = new TNumberWarning();
        nw.setId(uUidUtils.getCodeId(orgId, "t_number_warning"));
        nw.setOrgId(orgId);
        nw.setVcfcode(cfcode1);
        nw.setVdevcode(devcode);
        nw.setMonitorNumber(num[0]);
        nw.settVolume(num[1]);
        nw.settError(num[2]);
        nw.setDepotType(num[3]);
        nw.setDepotLong(num[4]);
        nw.setDepotWide(num[5]);
        nw.setAvgHeightv(num[6]);
        nw.setOperator(operator);
        nw.setMonitorTime(DateUtils.getDate());
        TNumberWarningHistory nwh = new TNumberWarningHistory();
        nwh.setId(uUidUtils.getCodeId(orgId, "t_number_warning_history"));
        nwh.setOrgId(orgId);
        nwh.setVcfcode(cfcode1);
        nwh.setVdevcode(devcode);
        nwh.setMonitorNumber(num[0]);
        nwh.settVolume(num[1]);
        nwh.settError(num[2]);
        nwh.setDepotType(num[3]);
        nwh.setDepotLong(num[4]);
        nwh.setDepotWide(num[5]);
        nwh.setAvgHeightv(num[6]);
        nwh.setOperator(operator);
        nwh.setMonitorTime(DateUtils.getDate());
        TNumberWarningExample example = new TNumberWarningExample();
        TNumberWarningExample.Criteria criteria = example.createCriteria();
        criteria.andVcfcodeEqualTo(cfcode1);
        numberWarningMapper.deleteByExample(example);
        numberWarningMapper.insert(nw);
        numberWarningHistoryMapper.insert(nwh);
    }

    /**
     * 获取摄像头信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getVideoInfo() {
        return numberWarningMapper.getVideoInfo();
    }

    @Override
    public List<Map<String, Object>> getCfName(String cfname) {
        return numberWarningMapper.getCfName(cfname);
    }

    @Override
    public List<TNumberWarningHistory> getListHistory(String orgId, String vCfCode, String startTime, String endTime) {
        TNumberWarningHistoryExample example = new TNumberWarningHistoryExample();
        TNumberWarningHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orgId)) {
            criteria.andOrgIdEqualTo(orgId);
        }
        if (StringUtils.isNotBlank(vCfCode)) {
            criteria.andVcfcodeEqualTo(vCfCode);
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            criteria.andMonitorTimeBetween(DateUtils.stringToData(startTime + DateUtils.startTime), DateUtils.stringToData(endTime + DateUtils.endTime));
        }
        example.setOrderByClause("monitor_time desc");
        List<TNumberWarningHistory> tNumberWarningHistories = numberWarningHistoryMapper.selectByExample(example);
        return tNumberWarningHistories;
    }

    @Override
    public ResponseEntity getScanResult(String vcfcode, String ws, Integer orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId+"");
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        Map<String, String> requestEntity = new HashMap<>();
        requestEntity.put("vcfcode", vcfcode);
        requestEntity.put("ws", ws);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.scanProgress),
                requestEntity
        );
    }

    @Override
    public ResponseEntity getmeasureweight(String vcfcode, String devcode, String operator, String orgId, String ws) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        Map<String, String> requestEntity = new HashMap<>();
        requestEntity.put("vcfcode", vcfcode);
        requestEntity.put("devcode", devcode);
        requestEntity.put("operator", operator);
        requestEntity.put("orgId", orgId);
        requestEntity.put("ws", ws);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.measureWeight),
                requestEntity
        );
    }

    public ResponseEntity sendNumberMonitor(String vcfcode, String ws, Integer orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId+"");
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        Map<String, String> requestEntity = new HashMap<>();
        requestEntity.put("vcfcode", vcfcode);
        requestEntity.put("ws", ws);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.sendOrder),
                requestEntity
        );
    }

    @Override
    public TNumberWarningHistory getTWarning(String orgId, String vCfCode) {
        List<TNumberWarningHistory> listHistory = this.getListHistory(orgId, vCfCode, null, null);
        if (listHistory.size() > 0) {
            return listHistory.get(0);
        }
        return null;
    }

}
