package com.tuoheng.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tuoheng.admin.entity.domain.ThMission;
import com.tuoheng.admin.entity.request.MissionQuery;
import com.tuoheng.admin.entity.request.MissionRequest;
import com.tuoheng.admin.entity.request.emergency.QueryEmergencyListRequest;
import com.tuoheng.admin.entity.request.emergency.QueryMissionListRequest;
import com.tuoheng.admin.entity.vo.MissionLiveVO;
import com.tuoheng.admin.entity.vo.MissionVO;
import com.tuoheng.admin.service.IMissionService;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.system.utils.ShiroUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 林场巡检任务
 *
 * @author: qiujinyang
 * @date: 2021/9/1
 */
@RestController
@RequestMapping("/mission")
@ApiOperation(value = "任务管理")
public class MissionController {
    @Autowired
    private IMissionService missionService;

    /**
     * 新建巡检任务
     * @param addThMissionRequest
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增任务", notes = "传入巡检任务对象")
    public JsonResult<Integer> addMission(@RequestBody @Valid MissionRequest addThMissionRequest) {
        //新增巡检任务校验
        checkAddInfo(addThMissionRequest);
        return JsonResult.success(missionService.addMission(addThMissionRequest));
    }

    private void checkAddInfo(MissionRequest request) throws ServiceException {
        //判断任务名称不能重复
        LambdaQueryWrapper<ThMission> lambdaQueryCheckRepartWrapper= new  LambdaQueryWrapper<>();
        lambdaQueryCheckRepartWrapper.eq(ThMission::getName, request.getName());
        lambdaQueryCheckRepartWrapper.eq(ThMission::getTenantId, ShiroUtils.getTenantId());
        List<ThMission> missionList = missionService.getList(lambdaQueryCheckRepartWrapper);
        if(!ObjectUtils.isEmpty(missionList) || missionList.size() >0 ){
            throw new ServiceException(HttpStatus.SC_BAD_REQUEST,"任务名称不能重复！");
        }

        Date executionStartTime = request.getExecutionStartTime();
        long start = executionStartTime.getTime() - 500;
        long end = executionStartTime.getTime() + 500;
        Date startTime=new Date(start);
        Date endTime=new Date(end);

        LambdaQueryWrapper<ThMission> lambdaQueryTimeWrapper= new  LambdaQueryWrapper<>();
        lambdaQueryTimeWrapper.eq(ThMission::getInspectionLine, request.getInspectionLine());
        lambdaQueryTimeWrapper.between(ThMission::getExecutionStartTime, startTime,endTime);
        lambdaQueryTimeWrapper.eq(ThMission::getTenantId, ShiroUtils.getTenantId());
        List<ThMission> missionListTime = missionService.getList(lambdaQueryTimeWrapper);
        if(!ObjectUtils.isEmpty(missionListTime) || missionListTime.size() >0 ){
            throw new ServiceException(HttpStatus.SC_BAD_REQUEST,"同一个航线已存在相同的巡检时间！");
        }

        if(request.getExecutionStartTime().getTime() < new Date().getTime()){
            throw new ServiceException(HttpStatus.SC_BAD_REQUEST,"无法输入该时间！");
        }

        if(request.getName().length()>100){
            throw new ServiceException(HttpStatus.SC_BAD_REQUEST,"任务名称长度不能超过100个字");
        }

        if(StringUtils.isNotEmpty(request.getNote()) && request.getNote().length()>200){
            throw new ServiceException(HttpStatus.SC_BAD_REQUEST,"备注最多输入200个字");
        }
    }

    /**
     * 删除巡检任务
     */
    @DeleteMapping("/{idList}")
    @ApiOperation(value = "删除巡检任务", notes = "传入巡检任务id")
    public JsonResult<Boolean> deleteBatch(@PathVariable("idList") List<Integer> idList) {
        return JsonResult.success(missionService.deleteBatch(idList));
    }


    /**
     * 查询巡检任务列表
     *
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询巡检任务列表", notes = "查询巡检任务分页")
    public JsonResult<IPage<MissionVO>> findList(MissionQuery queryMissionRequest) {
        return JsonResult.success(missionService.findList(queryMissionRequest));
    }


    /**
     * 获取巡检任务详情
     */
    @GetMapping("/{id}")
    @ApiImplicitParam(name="id",value = "任务的Id，missionId")
    public JsonResult findById(@PathVariable("id")Integer id) {
        return JsonResult.success(missionService.findById(id));
    }

    /**
     * 根据机场Id获取正在直播中的任务
     */
    @GetMapping("/live/{airportId}")
    @ApiImplicitParam(name="id",value = "任务的Id，missionId")
    public JsonResult<MissionLiveVO> live(@PathVariable("airportId")Integer id) {
        return JsonResult.success(missionService.live(id));
    }

    /**
     * 根据id获取任务视频回放和直播
     *
     * @param id
     * @return
     */
    @GetMapping("/videoById/{id}")
    public JsonResult videoById(@PathVariable("id") String id) {
        return missionService.getVideoById(id);
    }

    /**
     * 查询巡检任务列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询应急任务列表", notes = "查询任务列表")
    public JsonResult getList(QueryMissionListRequest request) {
        return missionService.getList(request);
    }

    /**
     * 查询巡检任务列表
     */
    @GetMapping("/emergency/list")
    @ApiOperation(value = "查询应急任务列表", notes = "查询应急任务列表")
    public JsonResult getEmergencyList(QueryEmergencyListRequest request) {
        return missionService.getEmergencyList(request);
    }
}
