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

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.plan.dto.PlanyearDto;
import com.tofly.pojo.plan.vo.PlanyearVo;
import com.tofly.gpsboot.exception.BusinessException;

import com.tofly.pojo.plan.Planyear;
import com.tofly.gpsboot.service.paln.year.PlanyearService;


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 Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/planyear")
@Api(tags = "巡检年度计划记录信息")
public class PlanyearController extends BaseController<Planyear, PlanyearVo> {

    @Autowired
    private PlanyearService service;

    @Autowired
    public PlanyearController(PlanyearService planyearService) {
        init(planyearService);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询(获取当前部门数据,不跨部门)")
    public ResultRespone<Page<PlanyearVo>> getPlanMonthPage(Page page, @ModelAttribute PlanyearDto parDto) {
        return ResultRespone.success(service.getInfoPage(page, parDto));
    }


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

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

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

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检年度计划记录信息")
    public ResultRespone<Object> savePlanyear(@Valid @RequestBody PlanyearDto 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> updateByIdPlanyear(@Valid @RequestBody PlanyearDto 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<Planyear> getByIdPlanyear(@PathVariable Long id) {
        ResultRespone<Planyear> rest = new ResultRespone<>();
        try {
            return super.getById(id);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @GetMapping("/getPlanYearDetail/{id}")
    @ApiOperation(value = "通过ID查询年计划详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询年计划详情")
    public ResultRespone<PlanyearVo> getPlanYearDetail(@PathVariable Long id) {
        ResultRespone<PlanyearVo> rest = new ResultRespone<>();
        try {
            PlanyearVo 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> removeByIdListPlanyear(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;
    }


}
