package com.yeejoin.amos.boot.module.jxiop.biz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jxiop.biz.entity.IndicatorData;
import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.CommonServiceImpl;
import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.TdengineTimeServiceImpl;
import com.yeejoin.amos.boot.module.jxiop.biz.tdMapper2.FanHealthIndexMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.tdMapper2.FanWaringRecordMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.tdMapper2.PvWaringRecordMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.tdengine.FanWarningRecord;
import com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvWarningRecord;
import com.yeejoin.amos.boot.module.jxiop.biz.tdmapper.IndicatorDataMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/test")
@Api(tags = "测试Api")
public class TestController extends BaseController {
	@Autowired
	IndicatorDataMapper indicatorDataMapper;
	@Autowired
	CommonServiceImpl commonService;

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(httpMethod = "GET", value = "test1", notes = "test1")
	@GetMapping("/test1")
	public List<IndicatorData> test1(@RequestParam(value = "startTime") String startTime,
			@RequestParam(value = "endTime") String endTime, @RequestParam(value = "address") String address,
			@RequestParam(value = "gatewayId") String gatewayId) {

		List<IndicatorData> indicatorData = indicatorDataMapper.selectDataByAddressAndtime(address, startTime, endTime,
				gatewayId);

		return indicatorData;
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(httpMethod = "GET", value = "test2", notes = "test1")
	@GetMapping("/test2")
	public void test2(@RequestParam(value = "startTime") String startTime, @Param("recDate") String recDate) {
//        List<FanHealthIndex> infoListByGroupByCD = fanHealthIndexMapper.getInfoListByGroupByCdFan(startTime, recDate);
//        commonService.healthWarningMinuteByFJ();
//        commonService.healthWarningMinuteByPv();
//        return infoListByGroupByCD;
	}

	@Autowired
	FanHealthIndexMapper fanHealthIndexMapper;

	@Autowired
	TdengineTimeServiceImpl tdengineTimeService;

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(httpMethod = "POST", value = "saveTest", notes = "saveTest")
	@PostMapping("/saveTest")
	public void saveTest() {

//        QueryWrapper<FanHealthIndex> fanHealthIndexQueryWrapper = new QueryWrapper<>();
//        List<FanHealthIndex> fanHealthIndices = fanHealthIndexMapper.selectList(fanHealthIndexQueryWrapper);
//
//        List<IdxBizFanHealthIndex> list = new ArrayList<>();
//        IdxBizFanHealthIndex fanHealthIndex = new IdxBizFanHealthIndex();
//        fanHealthIndex.setHealthIndex(2.0);
////        fanHealthIndex.setTs(System.currentTimeMillis());
//        fanHealthIndex.setArae("区域");
//        fanHealthIndex.setAnalysisEndTime(DateUtil.date());
//        fanHealthIndex.setAnalysisObjSeq("2");
//        fanHealthIndex.setHealthLevel("2321");
//        fanHealthIndex.setHealthIndex(25.0);
//        fanHealthIndex.setAnalysisStartTime(DateUtil.date());
//        fanHealthIndex.setAnalysisObjType("21");
//        fanHealthIndex.setANALYSISTIME("21");
//        fanHealthIndex.setANOMALY(2.0);
//        fanHealthIndex.setEquipmentName("21");
//        fanHealthIndex.setGatewayId("213412");
//        fanHealthIndex.setPointName("21312");
//        fanHealthIndex.setNumber("213");
//        fanHealthIndex.setKks("213");
//        fanHealthIndex.setRecDate(DateUtil.date());
//        fanHealthIndex.setSubSystem("213421");
//        fanHealthIndex.setWeigth(2.2);
//        fanHealthIndex.setAnalysisType("按时刻");
//        fanHealthIndex.setStation("rwwrrw");
//        fanHealthIndex.setIndexAddress("2321");
//        FanHealthIndex fanHealthIndex1 = new FanHealthIndex();
//        BeanUtil.copyProperties(fanHealthIndex, fanHealthIndex1);
//        fanHealthIndex1.setWeight(fanHealthIndex.getWeigth());
//        fanHealthIndex1.setAnomaly(fanHealthIndex.getANOMALY());
//        fanHealthIndex1.setRecDate(DateUtil.now());
//        fanHealthIndex1.setArea(fanHealthIndex.getArae());
//        fanHealthIndex1.setAnalysisTime(DateUtil.now());
//        fanHealthIndex1.setHealthLevel(fanHealthIndex.getHealthLevel());
//        fanHealthIndex1.setKks("123456");
//        ArrayList<FanHealthIndex> fanHealthIndices1 = new ArrayList<>();
//        fanHealthIndices1.add(fanHealthIndex1);
//        list.add(fanHealthIndex);
////        commonService.healthWarningMinuteByFJ();
//        fanHealthIndexMapper.saveBatchHealthIndexList(fanHealthIndices1, "fan_health_index_moment");
	}

	@Autowired
	private FanWaringRecordMapper fanWaringRecordMapper;

	@Autowired
	private PvWaringRecordMapper pvWaringRecordMapper;

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(httpMethod = "GET", value = "test2", notes = "test1")
	@GetMapping("/clear-record")
	public void clearRecord(@RequestParam(value = "startTime", required = false) String startTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String d = sdf.format(new Date());
		LambdaQueryWrapper<FanWarningRecord> wf = new LambdaQueryWrapper<>();
		wf.ne(FanWarningRecord::getDisposotionState, "已确认");
		List<FanWarningRecord> list = fanWaringRecordMapper.selectList(wf);
		if (!list.isEmpty()) {
			list.forEach(i -> {
				i.setDisposotionState("已确认");
				i.setDisposotionDate(d);
			});
			fanWaringRecordMapper.saveBatchWarningRecords(list);
		}

		LambdaQueryWrapper<PvWarningRecord> pf = new LambdaQueryWrapper<>();
		pf.ne(PvWarningRecord::getDisposotionState, "已确认");
		List<PvWarningRecord> list1 = pvWaringRecordMapper.selectList(pf);
		if (!list1.isEmpty()) {
			list1.forEach(i -> {
				i.setDisposotionState("已确认");
				i.setDisposotionDate(d);
			});
			pvWaringRecordMapper.saveBatchWarningRecords(list1);
		}
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(httpMethod = "GET", value = "test2", notes = "test1")
	@GetMapping("/set-date")
	public void clearRecord() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String d = sdf.format(new Date());
		LambdaQueryWrapper<FanWarningRecord> wf = new LambdaQueryWrapper<>();
		wf.isNull(FanWarningRecord::getDisposotionDate);
		List<FanWarningRecord> list = fanWaringRecordMapper.selectList(wf);
		if (!list.isEmpty()) {
			list.forEach(i -> {
				i.setDisposotionDate(d);
			});
			fanWaringRecordMapper.saveBatchWarningRecords(list);
		}

		LambdaQueryWrapper<PvWarningRecord> pf = new LambdaQueryWrapper<>();
		pf.isNull(PvWarningRecord::getDisposotionDate);
		List<PvWarningRecord> list1 = pvWaringRecordMapper.selectList(pf);
		if (!list1.isEmpty()) {
			list1.forEach(i -> {
				i.setDisposotionDate(d);
			});
			pvWaringRecordMapper.saveBatchWarningRecords(list1);
		}
	}
}
