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

import com.alibaba.fastjson.JSON;
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.controller.BaseController;
import com.tofly.pojo.plan.dto.PlanchildDto;
import com.tofly.pojo.plan.vo.PlanchildVo;

import com.tofly.pojo.plan.Planchild;

import com.tofly.gpsboot.service.paln.task.PlanchildService;

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.Optional;

/**
 * 巡检子计划信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/planchild")
@Api(tags = "巡检子计划信息")
public class PlanchildController extends BaseController<Planchild, PlanchildVo> {

    @Autowired
    private PlanchildService service;

    @Autowired
    public PlanchildController(PlanchildService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<PlanchildVo>> getPlanchildPage(Page page, @ModelAttribute PlanchildDto parDto) {
        return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Planchild.class));
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    public ResultRespone<Object> savePlanchild(@Valid @RequestBody PlanchildDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.save(JSON.parseObject(JSON.toJSONString(parDto), Planchild.class)));

    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    public ResultRespone<Object> updateByIdPlanchild(@Valid @RequestBody PlanchildDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Planchild.class)));

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone<Planchild> getByIdPlanchild(@PathVariable Long id) {
        return super.getById(id);
    }


    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone<Object> removeByIdListPlanchild(String ids) {
        return super.removeByIdList(ids);
    }


    @GetMapping("/getPlanChildState")
    @ApiOperation(value = "获取当前用户子计划状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "主计划id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "巡检人id", required = true, dataType = "Long")
    })
    public ResultRespone<String> getPlanChildState(Long planId,Long userId) {
        return ResultRespone.success(service.getPlanChildState(planId, userId));
    }
}
