package com.unis.detectionnum.server.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.unis.common.utils.ResponseWrapper;
import com.unis.detectionnum.common.vo.*;
import com.unis.detectionnum.server.message.entity.MessageVo;
import com.unis.detectionnum.server.message.utils.MessageAsyncSendUtils;
import com.unis.detectionnum.server.schedule.job.DetectionNumNewJob;
import com.unis.detectionnum.server.service.DeviceParamService;
import com.unis.detectionnum.server.service.ScanPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * @ClassName: ScanDataController 
 * @Description: 数据查询-扫描数据管理 
 * @author LiuFeng 
 * @date 2019年10月16日 下午4:55:23  
 */
@RestController
@Slf4j
@Api(value = "数据查询-扫描数据管理", tags = "数据查询-扫描数据管理")
public class ScanDataController {

	@Autowired
    private ScanPointsService pointsService;

/*
	@Autowired
    private DetectionNumJob detectionNumJob;
*/
	@Autowired
	private DetectionNumNewJob detectionNumNewJob;
	@Autowired
	private DeviceParamService deviceParamService;

	@ApiOperation(value = "数据查询->扫描纪录【分页查询】")
//    @PostMapping(value = "/scanData/record/{pageNum}/{pageSize}")
    @RequestMapping(value = "/scanData/recordPage", method = RequestMethod.POST)
    public ResponseWrapper recordPage(Integer pageNum,Integer pageSize,@RequestBody ScanDataQueryVO scanDataQueryVO){
    	
    	try {

    		PageHelper.startPage(pageNum, pageSize);
    		List<ScanDataRecordVO> list = pointsService.recordList(scanDataQueryVO);
   		    PageInfo<ScanDataRecordVO> recordPageList = new PageInfo<>(list);
        	return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), recordPageList);
    		 		
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
	
	@ApiOperation(value = "数据查询->根据记录id获取点数详情【三维点数据】")
	@RequestMapping(value = "/scanData/info", method = RequestMethod.GET)
    public ResponseWrapper getPointDataInfo(String operateId){
		HashMap<String, Object> map = null;
		try {
			map = Maps.newLinkedHashMap();
			Map<String, Object> cfByid = pointsService.getCfByid(operateId);
			map.put("zLXGD",cfByid.get("ZLXGD"));
			map.put("operateId",operateId);
			ArrayList<ArrayList<Double>> pointDataInfo = pointsService.getPointDataInfo(map);
			map.put("WareHouseLength",cfByid.get("smcd"));
			map.put("WareHouseWidth",cfByid.get("smkd"));
			map.put("data",pointDataInfo);
			/*StringBuffer str = new StringBuffer();
			str.append("[");
			for (int i=0;i<pointDataInfo.size();i++){
				str.append("[");
				ArrayList<Double> doubles = pointDataInfo.get(i);
				for (int j=0;j<doubles.size();j++){
					Double aDouble = doubles.get(j);
					if ((doubles.size()-1)==j){
						str.append(aDouble);
					}else {
						str.append(aDouble+",");
					}
				}
				str.append("],");
			}
			str.append("]");
			System.out.println(str);*/

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
		}
		return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), map);
    }
	
	@ApiOperation(value = "仓房展示->最新扫描数据【三维点数据】")
	@RequestMapping(value = "/scanData/info", method = RequestMethod.POST)
    public ResponseWrapper getPointDataInfo(@RequestBody ScanDataQueryVO scanDataQueryVO){
		HashMap<String, Object> map = null;
		try {
    		List<ScanDataRecordVO> list = pointsService.recordList(scanDataQueryVO);
    		if(list != null && list.size() > 0) {
				map = Maps.newLinkedHashMap();
				String id = list.get(0).getId();
				Map<String, Object> cfByid = pointsService.getCfByid(id);
				map.put("zLXGD",cfByid.get("ZLXGD"));
				map.put("operateId",id);
				ArrayList<ArrayList<Double>> pointDataInfo = pointsService.getPointDataInfo(map);
				map.put("WareHouseLength",cfByid.get("smcd"));
				map.put("WareHouseWidth",cfByid.get("smkd"));
				map.put("data",pointDataInfo);
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
		}
		return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), map);
    }


    @ApiOperation(value = "手动启动扫描", notes = "参数：仓房编号")
	@RequestMapping(value = "/scanData/scan/start", method = RequestMethod.POST)
    public ResponseWrapper scanStrat(@RequestBody DeviceInfoQueryVO deviceInfoQueryVO) {
        try {
 			DeviceInfoVO deviceInfoVO = deviceParamService.getDeviceProbe(deviceInfoQueryVO);
			String sbCode ="";
			//根据仓房编号 获取设备id
			String xybb = "V2.0";
			if(deviceInfoVO!=null){
				sbCode = deviceInfoVO.getSbCode();
			}

            //从 s_scan_points表 查询 z为null值 的三维点列表
            List<GetXYVO> NoZList = pointsService.getNotZList();

            //开启扫描
			if("V2.0".equals(xybb)){
				detectionNumNewJob.monitorStart(sbCode);
			}else{
//				detectionNumJob.scanStart(sbCode);
			}

            if (NoZList.size()==0){
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "开启扫描");

            }else{
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "正在扫描");
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");

    }
//=====================================================测试发送消息===================================================
	@Autowired
	MessageAsyncSendUtils messageAsyncSendUtils;

	@ApiOperation(value = "短信发送语音播报测试")
	@RequestMapping(value = "/scanData/sndMsg", method = RequestMethod.GET)
	public ResponseWrapper sndMsg(){
		String contens = "您好，商丘中转库3号仓发生粮食数量变化，请您及时确认！";
		String creater = "管理员";
		MessageVo messageVo = new MessageVo();
		messageVo.setContens(contens);
		messageVo.setCreater(creater);
		messageVo.setMobile("17600466577");
		messageAsyncSendUtils.asyncSend(messageVo);
		messageAsyncSendUtils.asyncSendPhone("17600466577", "商丘中转库3号仓");
		return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
	}
}
