package com.yuchen.saas.park.service.controller.fire;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuchen.saas.park.api.event.InspectTaskApplicationEvent;
import com.yuchen.saas.park.api.request.*;
import com.yuchen.saas.park.api.response.FireInspectPlanListResponse;
import com.yuchen.saas.park.api.response.FireInspectPlanResponse;
import com.yuchen.saas.park.service.service.FireInspectPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ClassName FireInspectPlanController
 * @Description 消防巡查计划控制器
 * @Author 肖祥
 * @Date 2022/10/20 15:30
 * @Version 1.0。0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/fire/inspect/plan")
@Api(value = "消防巡查计划控制器接口", tags = "消防巡查计划控制器接口")
public class FireInspectPlanController {

    @Resource
    private FireInspectPlanService fireInspectPlanService;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;


    /**
     * 消防巡查计划新增
     */
    @ApiOperation(value = "消防巡查计划新增")
    @PostMapping("/add")
    public R<Long> add(@Valid @RequestBody FireInspectPlanAddRequest fireInspectPlanAddRequest) {
       Long planId =  fireInspectPlanService.add(fireInspectPlanAddRequest).getData();
        if(planId > 0){
            applicationEventPublisher.publishEvent(new InspectTaskApplicationEvent(planId));
        }
        return R.data(planId);
    }

    /**
     * 消防巡查计划列表
     */
    @GetMapping("/list/page")
    @ApiOperation(value = "消防巡查计划列表分页", notes = "消防巡查计划列表分页")
    public R<IPage<FireInspectPlanListResponse>> list(@Valid FireInspectPlanQueryRequest fireInspectPlanQueryRequest, Query query) {
        return fireInspectPlanService.list(fireInspectPlanQueryRequest,query);
    }

    /**
     * 消防巡查计划结束
     */
    @ApiOperation(value = "消防巡查计划结束")
    @PostMapping("/over")
    public R<Boolean> over(@Valid @RequestBody FireInspectPlanOverRequest fireInspectPlanOverRequest) {
        return  fireInspectPlanService.over(fireInspectPlanOverRequest);
    }

    /**
     * 消防巡查计划删除
     */
    @ApiOperation(value = "消防巡查计划删除")
    @PostMapping("/delete")
    public R<Boolean> delete(@Valid @RequestBody FireInspectPlanDeleteRequest fireInspectPlanDeleteRequest) {
        return  fireInspectPlanService.delete(fireInspectPlanDeleteRequest);
    }

    /**
     * 消防巡查计划更新
     */
    @ApiOperation(value = "消防巡查计划更新")
    @PostMapping("/update")
    public R<Boolean> update(@Valid @RequestBody FireInspectPlanUpdateRequest fireInspectPlanUpdateRequest) {
        return  fireInspectPlanService.update(fireInspectPlanUpdateRequest);
    }

    /**
     * 消防巡查计划详情
     */
    @ApiOperation(value = "消防巡查计划详情")
    @GetMapping("/detail")
    public R<FireInspectPlanResponse> detail(@RequestParam(name = "id",required = false) Long id) {
        return  fireInspectPlanService.detail(id);
    }
}
