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

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.maintain.Repairplanyear;
import com.tofly.pojo.maintain.dto.RepairplanDto;
import com.tofly.pojo.maintain.vo.RepairplanVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.maintain.RepairplanService;

import com.tofly.pojo.maintain.Repairplan;


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 Tue Apr 06 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/repairplan")
@Api(tags="抢维修月度计划记录信息表接口")
public class RepairplanController extends BaseController<Repairplan, RepairplanVo> {
    @Autowired
    private RepairplanService service;
    @Autowired
    public RepairplanController(RepairplanService service) {
        init(service);
    }



    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询抢维修月度计划记录信息")
    public ResultRespone<Page<RepairplanVo>> getRepairplanPage(Page page, @ModelAttribute RepairplanDto parDto) {
        ResultRespone<Page<RepairplanVo>> rest = new ResultRespone<>();
        try {
            Page<RepairplanVo> repairPlanPage = service.getRepairPlanPage(page, parDto);
            rest.setResult(repairPlanPage);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

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

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

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

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增抢维修月度计划记录信息")
    public ResultRespone<Object> saveRepairplan(@Valid @RequestBody RepairplanDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                rest.setResult(service.savaYearPlanInfo(parDto));
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改抢维修月度计划记录信息")
    public ResultRespone<Object> updateByIdRepairplan(@Valid @RequestBody RepairplanDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                service.updateYearPlanInfo(parDto);
            } 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<Repairplan> getByIdRepairplan(@PathVariable Long id) {
        ResultRespone<Repairplan> rest = new ResultRespone<>();
        try {
            return super.getById(id);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @GetMapping("/getPlanRepairYearDetail/{id}")
    @ApiOperation(value = "通过ID查询抢维修月度计划记录详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询抢维修月度计划记录详情")
    public ResultRespone<RepairplanVo> getPlanRepairYearDetail(@PathVariable Long id) {
        ResultRespone<RepairplanVo> rest = new ResultRespone<>();
        try {
            RepairplanVo yearPlanInfo = service.getYearPlanInfo(id);
            rest.setResult(yearPlanInfo);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "单个或批量删除,多个请用,隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过ID批量删除抢维修月度计划记录")
    public ResultRespone<Object> removeByIdListRepairplan(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.deleteYearPlanInfo(longList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


}
