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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.emergency.EmergencyDisService;
import com.tofly.gpsboot.service.emergency.EmergencyService;
import com.tofly.pojo.emergency.Emergency;
import com.tofly.pojo.emergency.EmergencyDis;
import com.tofly.pojo.plan.PlanMt;
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 org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 应急事件处理日志记录表
 *
 * @author admin
 * @date Tue Feb 22 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/emergencydis")
@Api(tags="应急事件处理日志记录表接口")
public class EmergencyDisController {

    private final EmergencyDisService emergencyDisService;
    private final EmergencyService emergencyService;
    /**
    * 分页查询
    * @param page 分页对象
    * @param emergencyDis 应急事件处理日志记录表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getEmergencyDisPage(Page page, @ModelAttribute EmergencyDis emergencyDis) {
      return ResultRespone.success(emergencyDisService.page(page, Wrappers.query(emergencyDis)));
    }


    /**
     * 通过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(emergencyDisService.getById(id));
    }

    /**
     * 新增应急事件处理日志记录表
     * @param fileList
     * @param emergencyDis
     * @return
     */
    @ToFlyAppLog(title="新增应急事件处理日志记录表" )
    @ApiOperation(value = "新增应急事件处理日志记录表")
    @PostMapping
    public ResultRespone save(@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList, EmergencyDis emergencyDis) {
        ResultRespone<Boolean> rest = new ResultRespone<>();
        try {
            boolean b = emergencyDisService.saveInfo(fileList, emergencyDis);
            rest.setResult(b);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    /**
     * 修改应急事件处理日志记录表
     * @param fileList
     * @param emergencyDis
     * @return
     */
    @ToFlyAppLog(title="修改应急事件处理日志记录表" )
    @ApiOperation(value = "修改应急事件处理日志记录表")
    @PutMapping
    public ResultRespone updateById(@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList, EmergencyDis emergencyDis) {
        ResultRespone<Boolean> rest = new ResultRespone<>();
        try {
            boolean b = emergencyDisService.updateInfo(fileList, emergencyDis);
            rest.setResult(b);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


    /**
     * 应急事件记录上报审核
     * @param emergencyId
     * @param auditUserId
     * @return
     */
    @ToFlyAppLog(title="应急事件记录上报审核" )
    @ApiOperation(value = "应急事件记录上报审核")
    @GetMapping("/reportInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emergencyId", value = "应急事件主id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "auditUserId", value = "审核人id", required = true, dataType = "Long")
    })
    public ResultRespone<Boolean> reportInfo(Long emergencyId,Long auditUserId) {
        ResultRespone<Boolean> rest = new ResultRespone<>();
        try {
            boolean b = emergencyDisService.reportInfo(emergencyId, auditUserId);
            rest.setResult(b);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }
    /**
     * 通过id批量删除应急事件处理日志记录表
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除应急事件处理日志记录表" )
    @ApiOperation(value = "通过ID批量删除应急事件处理日志记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone<Boolean> removeById(String ids) {
        ResultRespone<Boolean> rest = new ResultRespone<>();
        List<String> stringList = Arrays.asList(ids.split(","));
        // 判断当前删除内容中的应急事件是否为 2已派工状态 若不是则不运行删除
        Set<Long> mainIdList = emergencyDisService.list(new QueryWrapper<EmergencyDis>().lambda().in(EmergencyDis::getId,stringList))
                .stream().map(EmergencyDis::getEmergencyId).collect(Collectors.toSet());
        Set<Emergency> collect = emergencyService.list(new QueryWrapper<Emergency>().lambda().in(Emergency::getId, mainIdList))
                .stream().filter(i -> !"2".equals(i.getState())).collect(Collectors.toSet());
        try {
            if(collect.size()>0){
                throw new BusinessException(Constant.EMERGENCY_DIS_DELETE_ERROR_CODE,Constant.EMERGENCY_DIS_DELETE_ERROR);
            }else {
                rest.setResult(emergencyDisService.removeByIds(stringList));
            }
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }
}
