package com.gyee.alarm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gyee.alarm.model.vo.AjaxResult;
import com.gyee.alarm.model.vo.AjaxStatus;
import com.gyee.alarm.model.vo.InspectOrderDto;
import com.gyee.alarm.service.InspectOrderService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("/inspectorder")
@CrossOrigin
@Api(value = "检修计划")
public class InspectOrderController {

    @Resource
    private InspectOrderService inspectOrderService;

    @ApiOperation(value = "分页查询隐患巡检单")
    @GetMapping("/order/page")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "pagenum", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pagesize", value = "分页大小", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "step", value = "节点 1-待审核，2-已审核，3-未计划，4-已计划，5-未巡检，6-已巡检", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "windturbineId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "string", paramType = "query")

    })
    public AjaxResult getInspectOrderPage(Integer pageNum,Integer pageSize,Optional<Integer> step,String windturbineId,String startTime,String endTime )

    {

        AjaxResult wrapper = null;

        Date dateStart = DateUtils.parseDate(startTime);
        Date dateEnd = DateUtils.parseDate(endTime);

        IPage<InspectOrderDto> inspectOrderDtoPage = inspectOrderService.getInspectOrderDtoPage(pageNum, pageSize, windturbineId, step, dateStart, dateEnd);

        if (StringUtils.notEmp(inspectOrderDtoPage)) {

            return AjaxResult.successData(AjaxStatus.success.code, inspectOrderDtoPage);
        } else {
            return AjaxResult.successData(AjaxStatus.error.code, "error");
        }
    }

    @ApiOperation(value = "分页查询隐患报告（待审核报告）")
    @GetMapping("/port/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "节场站id", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "windturbineId", value = "风机编号", required = true, dataType = "string", paramType = "query")

    })
    public AjaxResult getNoVerifiedPort( String stationId, String windturbineId) {
        List<InspectOrderDto> inspectOrderDtoPage = inspectOrderService.getInspectOrderDtoPage(stationId, windturbineId);
        if (StringUtils.notEmp(inspectOrderDtoPage)) {

            return AjaxResult.successData(AjaxStatus.success.code, inspectOrderDtoPage);
        } else {
            return AjaxResult.successData(AjaxStatus.error.code, "error");
        }
    }


    @ApiOperation(value = "审核诊断报告")
    @PostMapping("/update")
    public AjaxResult saveInspectOrder(HttpServletRequest request, @RequestBody InspectOrderDto inspectOrderDto) {

        String token = request.getHeader("token");
        String userId = request.getHeader("userId");


        AjaxResult AjaxResult = null;
        //如果id为空，则代表报告评审，否则代表更新流程
        AjaxResult = inspectOrderService.updateInspectOrder(userId, inspectOrderDto);
        return AjaxResult;
    }


    @ApiOperation(value = "第三方系统获取巡检单")
    @GetMapping("/order/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "string", paramType = "query")

    })
    public AjaxResult getInspectOrderList(long startTime, long endTime  ) {

        List<InspectOrderDto> orderDtoList = inspectOrderService.getOrderDtoList(new Date(startTime), new Date(endTime));
        if (StringUtils.notEmp(orderDtoList)) {

            return AjaxResult.successData(AjaxStatus.success.code, orderDtoList);
        } else {
            return AjaxResult.successData(AjaxStatus.error.code, "error");
        }
    }
}
