/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.maintain;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.exception.BusinessException;
import com.tofly.gpsboot.service.maintain.RepairplanyearService;
import com.tofly.pojo.maintain.Repairplanyear;
import lombok.AllArgsConstructor;
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 java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 抢维修年度计划记录信息表
 *
 * @author admin
 * @date Tue Nov 02 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/repairplanyear")
@Api(tags = "抢维修年度计划记录信息表接口")
public class RepairplanyearController {

    private final RepairplanyearService repairplanyearService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param repairplanyear 抢维修年度计划记录信息表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询(获取当前部门数据,不跨部门)")
    public ResultRespone getRepairplanyearPage(Page page, @ModelAttribute Repairplanyear repairplanyear) {
        return ResultRespone.success(repairplanyearService.getInfoPage(page, repairplanyear));
    }


    @GetMapping("/getAuditPage")
    @ApiOperation(value = "分页查询(只获取审核信息)")
    public ResultRespone getAuditPage(Page page, @ModelAttribute Repairplanyear repairplanyear) {
        return ResultRespone.success(repairplanyearService.getAuditList(page, repairplanyear));
    }

    @GetMapping("/getNeedAuditList")
    @ApiOperation(value = "分页查询(获取当前只需要审核的数据)")
    public ResultRespone getNeedAuditList(Page page, @ModelAttribute Repairplanyear repairplanyear) {
        return ResultRespone.success(repairplanyearService.getNeedAuditList(page, repairplanyear));
    }

    @GetMapping("/getAllPage")
    @ApiOperation(value = "分页查询(获取当前部门数据+跨部门数据)")
    public ResultRespone getAllPage(Page page, @ModelAttribute Repairplanyear repairplanyear) {
        return ResultRespone.success(repairplanyearService.getAllList(page, repairplanyear));
    }



    /**
     * 通过id查询抢维修年度计划记录信息表
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id") Long id) {
        return ResultRespone.success(repairplanyearService.getById(id));
    }

    /**
     * 新增抢维修年度计划记录信息表
     *
     * @param repairplanyear 抢维修年度计划记录信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增抢维修年度计划记录信息表")
    @ApiOperation(value = "新增抢维修年度计划记录信息表")
    @PostMapping
    public ResultRespone save(@RequestBody Repairplanyear repairplanyear) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        try {
            Repairplanyear yearPlanInfo = repairplanyearService.savaYearPlanInfo(repairplanyear);
            rest.setResult(yearPlanInfo);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }

        return rest;
    }

    /**
     * 修改抢维修年度计划记录信息表
     *
     * @param repairplanyear 抢维修年度计划记录信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改抢维修年度计划记录信息表")
    @ApiOperation(value = "修改抢维修年度计划记录信息表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Repairplanyear repairplanyear) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回

        try {
            repairplanyearService.updateYearPlanInfo(repairplanyear);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }

        return rest;
    }

    /**
     * 通过id批量删除抢维修年度计划记录信息表
     *
     * @param idList idList
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id批量删除抢维修年度计划记录信息表")
    @ApiOperation(value = "通过ID批量删除抢维修年度计划记录信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(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());
            repairplanyearService.deleteYearPlanInfo(longList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }
}
