/*
 * 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.PlanpointDto;
import com.tofly.pojo.plan.vo.PlanpointVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Planpoint;
import com.tofly.gpsboot.service.daily.PlanpointService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import javax.validation.Valid;
import java.util.Optional;

/**
 * 巡检点信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/planpoint")
@Api(tags = "巡检点信息")
public class PlanpointController extends BaseController<Planpoint, PlanpointVo> {

    @Autowired
    private PlanpointService service;

    @Autowired
    public PlanpointController(PlanpointService planpointService) {
        init(planpointService);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡检点信息")
    public ResultRespone<Page<PlanpointVo>> getPlanpointPage(Page page, @ModelAttribute PlanpointDto parDto) {
        ResultRespone<Page<PlanpointVo>> rest = new ResultRespone<>();
        try {
            Page<PlanpointVo> planPointPage = service.getPlanPointPage(page, parDto);
            rest.setResult(planPointPage);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检点信息")
    public ResultRespone<Object> savePlanpoint(@Valid @RequestBody PlanpointDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                return super.save(JSON.parseObject(JSON.toJSONString(parDto), Planpoint.class));
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;

    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改巡检点信息")
    public ResultRespone<Object> updateByIdPlanpoint(@Valid @RequestBody PlanpointDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                return super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Planpoint.class));
            } 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<Planpoint> getByIdPlanpoint(@PathVariable Long id) {
        ResultRespone<Planpoint> rest = new ResultRespone<>();
        try {
            return super.getById(id);
        } 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> removeByIdListPlanpoint(String idList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            return super.removeByIdList(idList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


}
