package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.EquipmentAlarmService;
import com.gxwebsoft.apps.entity.EquipmentAlarm;
import com.gxwebsoft.apps.param.EquipmentAlarmParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 故障报警记录控制器
 *
 * @author 科技小王子
 * @since 2022-12-01 23:49:44
 */
@Api(tags = "故障报警记录管理")
@RestController
@RequestMapping("/api/apps/equipment-alarm")
public class EquipmentAlarmController extends BaseController {
    @Resource
    private EquipmentAlarmService equipmentAlarmService;

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:list')")
    @OperationLog
    @ApiOperation("分页查询故障报警记录")
    @GetMapping("/page")
    public ApiResult<PageResult<EquipmentAlarm>> page(EquipmentAlarmParam param) {
        PageParam<EquipmentAlarm, EquipmentAlarmParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(equipmentAlarmService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(equipmentAlarmService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:list')")
    @OperationLog
    @ApiOperation("查询全部故障报警记录")
    @GetMapping()
    public ApiResult<List<EquipmentAlarm>> list(EquipmentAlarmParam param) {
        PageParam<EquipmentAlarm, EquipmentAlarmParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(equipmentAlarmService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(equipmentAlarmService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:list')")
    @OperationLog
    @ApiOperation("根据id查询故障报警记录")
    @GetMapping("/{id}")
    public ApiResult<EquipmentAlarm> get(@PathVariable("id") Integer id) {
        return success(equipmentAlarmService.getById(id));
        // 使用关联查询
        //return success(equipmentAlarmService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:save')")
    @OperationLog
    @ApiOperation("添加故障报警记录")
    @PostMapping()
    public ApiResult<?> save(@RequestBody EquipmentAlarm equipmentAlarm) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          equipmentAlarm.setUserId(loginUser.getUserId());
          equipmentAlarm.setMerchantCode(getMerchantCode());
        }
        if (equipmentAlarmService.save(equipmentAlarm)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:update')")
    @OperationLog
    @ApiOperation("修改故障报警记录")
    @PutMapping()
    public ApiResult<?> update(@RequestBody EquipmentAlarm equipmentAlarm) {
        if (equipmentAlarmService.updateById(equipmentAlarm)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:remove')")
    @OperationLog
    @ApiOperation("删除故障报警记录")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (equipmentAlarmService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:save')")
    @OperationLog
    @ApiOperation("批量添加故障报警记录")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<EquipmentAlarm> list) {
        if (equipmentAlarmService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:update')")
    @OperationLog
    @ApiOperation("批量修改故障报警记录")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<EquipmentAlarm> batchParam) {
        if (batchParam.update(equipmentAlarmService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentAlarm:remove')")
    @OperationLog
    @ApiOperation("批量删除故障报警记录")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (equipmentAlarmService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
