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


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.pojo.plan.dto.PlanstopDto;
import com.tofly.pojo.plan.vo.PlanstopVo;
import com.tofly.gpsboot.exception.BusinessException;

import com.tofly.gpsboot.service.paln.task.PlanstopService;
import com.tofly.pojo.plan.Planstop;
import io.swagger.annotations.Api;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * 巡检计划暂停
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/planStop")
@Api(tags = "巡检计划暂停")
public class PlanStopController {

    @Autowired
    private PlanstopService service;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡检计划暂停")
    public ResultRespone<Page<PlanstopVo>> getPlanstopPage(Page page, @ModelAttribute PlanstopDto parDto) {
        ResultRespone<Page<PlanstopVo>> rest = new ResultRespone<>();
        try {
            Page page1 = service.page(page, new QueryWrapper<Planstop>().lambda().setEntity(JSON.parseObject(JSON.toJSONString(parDto), Planstop.class)));
            rest.setResult(page1);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检计划暂停")
    public ResultRespone<Object> savePlanstop(@Valid @RequestBody PlanstopDto parDto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            rest.setResult(service.saveInfo(parDto));
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改巡检计划暂停")
    public ResultRespone<Object> updateByIdPlanstop(@Valid @RequestBody PlanstopDto parDto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回

        try {
            rest.setResult(service.updateById(JSON.parseObject(JSON.toJSONString(parDto), Planstop.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<Planstop> getByIdPlanstop(@PathVariable Long id) {
        ResultRespone<Planstop> rest = new ResultRespone<>();
        try {
            rest.setResult(service.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> removeByIdListPlanstop(String idList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            if (!StringUtils.isEmpty(idList)) {
                List<Long> ids = Arrays.stream(idList.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                        .collect(Collectors.toList());
                rest.setResult(service.removeByIds(ids));
            }
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


}
