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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.service.emergency.EmergencyTypeService;
import com.tofly.pojo.emergency.EmergencyType;
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 java.util.Arrays;
import java.util.List;

/**
 * 事件类型配置表
 *
 * @author admin
 * @date Tue Feb 22 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/emergencytype")
@Api(tags = "事件类型配置表接口")
public class EmergencyTypeController {

    private final EmergencyTypeService emergencyTypeService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param emergencyType 事件类型配置表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getEmergencyTypePage(Page page, @ModelAttribute EmergencyType emergencyType) {

        return ResultRespone.success(emergencyTypeService.page(page, Wrappers.query(emergencyType)));
    }

    public boolean checkIsExit(String name, Long id) {
        int count = emergencyTypeService.count(Wrappers.<EmergencyType>lambdaQuery()
                .eq(EmergencyType::getName, name)
                .ne(EmergencyType::getId, id)
        );
        return count > 0 ? false : true;
    }

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

    /**
     * 新增事件类型配置表
     *
     * @param emergencyType 事件类型配置表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增事件类型配置表")
    @ApiOperation(value = "新增事件类型配置表")
    @PostMapping
    public ResultRespone save(@RequestBody EmergencyType emergencyType) {
        if (!checkIsExit(emergencyType.getName(), 0L)) {
            return ResultRespone.failed("类型名称：" + emergencyType.getName() + "已存在，不能重复添加");
        } else {
            return ResultRespone.success(emergencyTypeService.save(emergencyType));
        }
    }

    /**
     * 修改事件类型配置表
     *
     * @param emergencyType 事件类型配置表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改事件类型配置表")
    @ApiOperation(value = "修改事件类型配置表")
    @PutMapping
    public ResultRespone updateById(@RequestBody EmergencyType emergencyType) {
        if (!checkIsExit(emergencyType.getName(), emergencyType.getId())) {
            return ResultRespone.failed("类型名称：" + emergencyType.getName() + "已存在，不能修改");
        } else {
            return ResultRespone.success(emergencyTypeService.updateById(emergencyType));
        }
    }


    /**
     * 通过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 removeById(String ids) {
        List<String> stringList = Arrays.asList(ids.split(Constant.CSV));
        return ResultRespone.success(emergencyTypeService.update(new UpdateWrapper<EmergencyType>().lambda()
                .in(EmergencyType::getId, stringList)
                .set(EmergencyType::getFlag, "0")));
    }
}
