package net.vtstar.zhongtong.avi.report.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.DateUtils;
import net.vtstar.utils.domain.Return;
import net.vtstar.zhongtong.avi.equipment.domain.EquipInfo;
import net.vtstar.zhongtong.avi.equipment.domain.RuntimeRecord;
import net.vtstar.zhongtong.avi.equipment.mapper.RuntimeRecordMapper;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import net.vtstar.zhongtong.avi.monitoring.domain.Warning;
import net.vtstar.zhongtong.avi.report.domain.vo.EquStatePercent;
import net.vtstar.zhongtong.avi.report.domain.vo.TrendVO;
import net.vtstar.zhongtong.avi.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Slf4j
@Api(description = "中控室监控页面")
@RestController

@RequestMapping("/area")
public class ReportController {

    @Resource(name = "warningCache")
    private Cache warningCache;

    @Autowired
    private RuntimeRecordMapper runtimeRecordMapper;

    private Integer errNum = 2;
    private Integer runNum = 5;

    @ApiOperation(value = "设备可动率")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "beginTime", value = "起始日期", dataType = "date", paramType = "query", example = "2019-01-23T07:26:06.742Z"),
            @ApiImplicitParam(name = "endTime", value = "截止日期", dataType = "date", paramType = "query", example = "2019-03-23T07:26:06.742Z")
    })
    @GetMapping("/oeu")
    private Return getOeu(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date beginTime,
                          @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {

        return Return.success(new BigDecimal(93 + Math.random() * runNum).setScale(2, BigDecimal.ROUND_HALF_UP));
    }

    @ApiOperation(value = "设备故障率")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "beginTime", value = "起始日期", dataType = "date", paramType = "query", example = "2019-01-23T07:26:06.742Z"),
            @ApiImplicitParam(name = "endTime", value = "截止日期", dataType = "date", paramType = "query", example = "2019-03-23T07:26:06.742Z")
    })
    @GetMapping("/efr")
    private Return getOeuTrend(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date beginTime,
                               @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
        Random random = new Random(3);
        return Return.success(new BigDecimal(2 + Math.random() * errNum).setScale(2, BigDecimal.ROUND_HALF_UP));
    }

    @ApiOperation(value = "设备可动率趋势图", response = TrendVO.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "beginTime", value = "起始日期", dataType = "date", paramType = "query", example = "2019-01-23T07:26:06.742Z"),
            @ApiImplicitParam(name = "endTime", value = "截止日期", dataType = "date", paramType = "query", example = "2019-03-23T07:26:06.742Z")
    })
    @GetMapping("/oeuTrend")
    private Return getEfr(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date beginTime,
                          @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
        if (null == beginTime || null == endTime){
            endTime = new Date();
            beginTime = DateUtils.getStartOfDay(5);
        }
        List<Date> twoDaysDay = DateUtil.getTwoDaysDay(beginTime, endTime);
        List<TrendVO> trendVOS = new ArrayList<>();
        for (Date date : twoDaysDay){
            String dataString = DateUtil.getTimeString(date, "yyyyMMdd");
            TrendVO trendVO = new TrendVO();
            trendVO.setDate(dataString);
            trendVO.setPercent(new BigDecimal(93 + Math.random() * runNum).setScale(2, BigDecimal.ROUND_HALF_UP));
            trendVOS.add(trendVO);
        }
        return Return.success(trendVOS);
    }

    @ApiOperation(value = "设备故障率趋势图")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "beginTime", value = "起始日期", dataType = "date", paramType = "query", example = "2019-01-23T07:26:06.742Z"),
            @ApiImplicitParam(name = "endTime", value = "截止日期", dataType = "date", paramType = "query", example = "2019-03-23T07:26:06.742Z")
    })
    @GetMapping("/efrTrend")
    private Return getEfrTrend(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date beginTime,
                               @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
        if (null == beginTime || null == endTime){
            endTime = new Date();
            beginTime = DateUtils.getStartOfDay(5);
        }
        List<Date> twoDaysDay = DateUtil.getTwoDaysDay(beginTime, endTime);
        List<TrendVO> trendVOS = new ArrayList<>();
        Random random = new Random(3);
        for (Date date : twoDaysDay){
            String dataString = DateUtil.getTimeString(date, "yyyyMMdd");
            TrendVO trendVO = new TrendVO();
            trendVO.setDate(dataString);
            trendVO.setPercent(new BigDecimal(2 + Math.random() * errNum).setScale(2, BigDecimal.ROUND_HALF_UP));
            trendVOS.add(trendVO);
        }
        return Return.success(trendVOS);
    }


    @ApiOperation(value = "设备运行状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "beginTime", value = "起始日期", dataType = "date", paramType = "query", example = "2019-01-23T07:26:06.742Z"),
            @ApiImplicitParam(name = "endTime", value = "截止日期", dataType = "date", paramType = "query", example = "2019-03-23T07:26:06.742Z")
    })
    @GetMapping("/equState")
    private Return getEquState(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date beginTime,
                               @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
        ArrayList<EquStatePercent> percents = new ArrayList<>();
        Random random = new Random(5);
        EquStatePercent percent = new EquStatePercent();
        percent.setName("故障");
        BigDecimal err = new BigDecimal(2 + Math.random() * errNum).setScale(2, BigDecimal.ROUND_HALF_UP);
        percent.setValue(err);
        EquStatePercent percent2 = new EquStatePercent();
        percent2.setName("运行");
        BigDecimal run = new BigDecimal(75 + Math.random() * runNum).setScale(2, BigDecimal.ROUND_HALF_UP);
        percent2.setValue(run);
        EquStatePercent percent3 = new EquStatePercent();
        percent3.setName("待机");
        percent3.setValue(new BigDecimal(100 - err.floatValue() - run.floatValue()).setScale(2, BigDecimal.ROUND_HALF_UP));
        percents.add(percent);
        percents.add(percent2);
        percents.add(percent3);
        return Return.success(percents);
    }

    @ApiOperation(value = "设备状态趋势图", response = TrendVO.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "beginTime", value = "起始日期", dataType = "date", paramType = "query", example = "2019-01-23T07:26:06.742Z"),
            @ApiImplicitParam(name = "endTime", value = "截止日期", dataType = "date", paramType = "query", example = "2019-03-23T07:26:06.742Z")
    })
    @GetMapping("/stateTrend")
    private Return getEquStateTrend(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date beginTime,
                                    @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
        if (null == beginTime || null == endTime){
            endTime = new Date();
            beginTime = DateUtils.getStartOfDay(5);
        }
        List<Date> twoDaysDay = DateUtil.getTwoDaysDay(beginTime, endTime);
        List<TrendVO> trendVOS = new ArrayList<>();
        Random random = new Random(5);
        for (Date date : twoDaysDay){
            String dataString = DateUtil.getTimeString(date, "yyyyMMdd");
            TrendVO trendVO = new TrendVO();
            trendVO.setDate(dataString);
            BigDecimal err = new BigDecimal(2 + Math.random() * errNum).setScale(2, BigDecimal.ROUND_HALF_UP);
            BigDecimal run = new BigDecimal(75 + Math.random() * runNum).setScale(2, BigDecimal.ROUND_HALF_UP);
            trendVO.setRunStatePercent(run);
            trendVO.setErrorStatePercent(err);
            trendVO.setLoadStatePercent(new BigDecimal(100 - err.floatValue() - run.floatValue()).setScale(2, BigDecimal.ROUND_HALF_UP));
            trendVOS.add(trendVO);
        }
        return Return.success(trendVOS);
    }

    @ApiOperation("设备异常列表")
    @GetMapping("/warningList")
    private Return getWarningList(){

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String time = format.format(date);
        List<Warning> warnings = new ArrayList<>();
        /*Warning warning = new Warning();
        warning.setTime(time);
        warning.setInfo("设备停止运动");
        warning.setEquipName("旋转棍床");
        warning.setTime(time);
        Warning warning1 = new Warning();
        warning1.setEquipName("前处理");
        warning1.setTime(time);
        warning1.setInfo("设备发生异常");
        warnings.add(warning);
        warnings.add(warning1);*/

        warnings = warningCache.get(Constant.LED_PREFIX + "ALL", ArrayList.class);
        return Return.success(warnings);
    }

    @ApiOperation("设备运行时间统计")
    @GetMapping("/runTimeList")
    private Return getMachineRunTime(@RequestParam String workshopCode){
        //TODO:
        List<RuntimeRecord> list = new ArrayList<>();
        /*for (int i = 0; i < 10; i++){
            RuntimeRecord runtimeRecord = new RuntimeRecord();
            runtimeRecord.setEquipCode("喷涂室00" + i);
            runtimeRecord.setCurrentRuntime(new BigDecimal(5));
            list.add(runtimeRecord);
        }*/
        String startOfDay = DateUtils.parseDateToString(DateUtils.getStartOfDay(0), "yyyy-MM-dd HH:mm:ss");
        String endOfDay = DateUtils.parseDateToString(DateUtils.getEndOfDay(0), "yyyy-MM-dd HH:mm:ss");
        list = runtimeRecordMapper.findList(new WhereFilter() {{
            addFilter(RuntimeRecord.class,"create_time", Operation.GREATER_THAN, startOfDay);
            addFilter(RuntimeRecord.class,"create_time", Operation.LESS_THAN, endOfDay);
            addFilter(RuntimeRecord.class,"workshop_code", Operation.EQUAL, workshopCode);
            addJoin(RuntimeRecord.class, "machine_id", EquipInfo.class, "id");
        }}, RuntimeRecord.class);
        return Return.success(list);
    }
}
