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

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.PlanmouthDto;
import com.tofly.pojo.plan.vo.PlanmouthVo;
import com.tofly.pojo.plan.vo.PlanyeardetailVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Planmouth;

import com.tofly.gpsboot.service.paln.month.PlanmouthService;

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("/planmouth")
//@Api(tags = "巡检月度计划记录信息")
public class PlanmouthController extends BaseController<Planmouth, PlanmouthVo> {
    @Autowired
    private PlanmouthService service;

    @Autowired
    public PlanmouthController(PlanmouthService planmouthService) {
        init(planmouthService);
    }


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


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

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

    @GetMapping("/getAllPage")
    @ApiOperation(value = "分页查询(获取当前部门数据+跨部门数据)")
    public ResultRespone<Page<PlanmouthVo>> getAllPage(Page page, @ModelAttribute PlanmouthDto parDto) {
        return ResultRespone.success(service.getAllList(page, parDto));
    }
    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检月度计划记录信息")
    public ResultRespone<Object> savaMonthPlanInfo(@Valid @RequestBody PlanmouthDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                Long aLong = service.savaMonthPlanInfo(parDto);
                rest.setResult(aLong);
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;

    }

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

    @GetMapping("/getPlanMonthDetail/{id}")
    @ApiOperation(value = "通过ID查询月计划详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询月计划详情")
    public ResultRespone<PlanmouthVo> getPlanMonthDetail(@PathVariable Long id) {
        ResultRespone<PlanmouthVo> rest = new ResultRespone<>();
        try {
            PlanmouthVo info = service.getMonthPlanInfo(id);
            rest.setResult(info);
        } 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.deleteMonthPlanInfo(longList);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @GetMapping("/getPlanYearDetail")
    @ApiOperation(value = "通过日期,ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "yearMonth", value = "YYYY-MM格式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "monthId", value = "月计划id", required = false, dataType = "String")
    })
    @ToFlyAppLog(title = "通过日期,ID查询巡检月度计划记录信息")
    public ResultRespone<PlanyeardetailVo> getPlanYearDetail(String yearMonth,String monthId) {
        ResultRespone<PlanyeardetailVo> rest = new ResultRespone<>();
        try {
          rest.setResult(service.getPlanYearDetail(yearMonth,monthId));
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

}
