/*
 * Author: Zev
 * 生成controller
 */
package com.tofly.gpsboot.controller.plan.daily;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.PlanDto;
import com.tofly.pojo.plan.vo.PlanDetailVo;
import com.tofly.pojo.plan.vo.PlanVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Plan;

import com.tofly.gpsboot.service.paln.task.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 巡检计划详细信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/plan")
@Api(tags = "巡检计划详细信息")
public class PlanController {

    @Autowired
    private PlanService service;


    @GetMapping("/getReportPlanYear")
    @ApiOperation(value = "本年度巡检计划统计")
    @ToFlyAppLog(title = "本年度巡检计划统计")
    public ResultRespone getReportPlanYear() {

        return ResultRespone.success(service.getReportPlanYear());
    }


    @GetMapping("/getReportPlanMonth")
    @ApiOperation(value = "本月巡检计划统计")
    @ToFlyAppLog(title = "本月巡检计划统计")
    public ResultRespone getReportPlanMonth() {

        return ResultRespone.success(service.getReportPlanMonth());
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡检计划详细信息")
    public ResultRespone<Page<Plan>> getPlanPage(Page page, @ModelAttribute PlanDto parDto) {
        ResultRespone<Page<Plan>> rest = new ResultRespone<>();
        try {
            Page<Plan> planPage = service.getPlanPage(page, parDto);
            rest.setResult(planPage);
            return rest;
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }
    @GetMapping("/getPlanTimeIntervalPage")
    @ApiOperation(value = "分页查询巡检计划满足时间范围内详细信息")
    @ToFlyAppLog(title = "分页查询巡检计划满足时间范围内详细信息")
    public ResultRespone<Page<Plan>> getPlanTimeIntervalPage(Page page, @ModelAttribute PlanDto parDto) {
        ResultRespone<Page<Plan>> rest = new ResultRespone<>();
        try {
            Page<Plan> voPage = service.getPlanTimeIntervalPage(page, parDto);
            rest.setResult(voPage);
            return rest;
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检计划详细信息")
    public ResultRespone<Object> savePlan(@Valid @RequestBody PlanDto parDto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回

        try {
            service.savaPlanInfo(parDto);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }

        return rest;

    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改巡检计划详细信息")
    public ResultRespone<Object> updateByIdPlan(@Valid @RequestBody PlanDto parDto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        try {
            service.updatePlanInfo(parDto);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }

        return rest;

    }

    @ApiOperation(value = "修改计划状态")
    @GetMapping("/updatePlanState")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "planId", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "planState", value = "planState 3 正在执行 5 已开始未执行", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "Long"),
    })
    @ToFlyAppLog(title = "修改计划状态")
    public ResultRespone<Boolean> updatePlanState(Long planId, String planState, Long userId) {
        ResultRespone<Boolean> rest = new ResultRespone<>();
        try {
            service.updatePlanState(planId, planState, userId);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }

        return rest;

    }


    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询巡检计划详细信息")
    public ResultRespone<Plan> getByIdPlan(@PathVariable Long id) {
        ResultRespone<Plan> rest = new ResultRespone<>();
        try {
            Plan planInfo = service.getPlanInfo(id);
            rest.setResult(planInfo);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过ID批量删除巡检计划详细信息")
    public ResultRespone<Object> removeByIdListPlan(String idList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<Long> longList = Arrays.stream(idList.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            service.deletePlanInfo(longList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @GetMapping("/getPlanDetail")
    @ApiOperation(value = "通过ID查询计划详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "计划id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "flag", value = "是否展示部门下所有数据 是则填1 个人不填为空", required = false, dataType = "String")
    })
    @ToFlyAppLog(title = "通过ID查询计划详情")
    public ResultRespone<List<PlanDetailVo>> getPlanDetail(Long id, String flag) {
        ResultRespone<List<PlanDetailVo>> rest = new ResultRespone<>();
        try {
            List<PlanDetailVo> planDetail = service.getPlanDetail(id, flag);
            rest.setResult(planDetail);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @GetMapping("/planUrgent")
    @ApiOperation(value = "巡检计划加急督办处理")
    @ToFlyAppLog(title = "巡检计划加急督办处理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "巡检计划计划id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "planState", value = "计划状态", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userIds", value = "处理人id,多个以,分隔", required = true, dataType = "String"),
    })
    public ResultRespone<Boolean> planUrgent(Long planId,String planState, String userIds) {
        ResultRespone<Boolean> rest = new ResultRespone<>();
        try {
            Boolean flag= service.planUrgent(planId,planState,userIds);
            rest.setResult(flag);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

}
