package com.ylx.web.controller.massage;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.SecurityUtils;
import com.ylx.common.utils.StringUtils;
import com.ylx.massage.domain.Alarm;
import com.ylx.massage.domain.vo.AlarmListVO;
import com.ylx.massage.service.AlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * 告警工单表控制层
 *
 * @author makejava
 * @since 2024-04-28 09:19:04
 */
@RestController
@RequestMapping("alarm")
@Api(tags = {"告警工单"})
public class AlarmController {
    /**
     * 服务对象
     */
    @Resource
    private AlarmService alarmService;

    /**
     * 分页查询告警记录
     *
     * @param page 分页对象
     * @param alarm 筛选条件
     * @return R 查询结果
     */
    @GetMapping("getAlarmList")
    @ApiOperation("分页查询告警记录")
    public R queryByPage(Page<AlarmListVO> page, Alarm alarm) {
        try {
            Page<AlarmListVO> pageSelect = alarmService.selectAlarmPage(page, alarm);
            return R.ok(pageSelect);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过主键ID查询详情
     *
     * @param id 主键
     * @return R<Alarm>
     */
    @GetMapping("{id}")
    public R<AlarmListVO> queryById(@PathVariable("id") String id) {
        try {
            return R.ok(alarmService.queryById(id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 处理告警工单
     * @param alarm
     * @return R
     */
    @PostMapping("/handleAlarm")
    @ApiOperation("已处理")
    @Log(title = "处理告警工单", businessType = BusinessType.UPDATE)
    public R handleAlarm(@RequestBody Alarm alarm) {
        try {
            if (StringUtils.isBlank(alarm.getId())) {
                throw new ServiceException("id不能为空");
            }
            if (StringUtils.isBlank(alarm.getNote())) {
                throw new ServiceException("处理过程及结果不能为空");
            }

            String handleResult = alarm.getNote().trim();
            if (handleResult.length() > 128) {
                throw new ServiceException("处理过程及结果不能超过128个字符");
            }

            Alarm existAlarm = alarmService.getById(alarm.getId());
            if (existAlarm == null) {
                throw new ServiceException("告警工单不存在");
            }
            if (Integer.valueOf(1).equals(existAlarm.getAlarmStatus())) {
                throw new ServiceException("告警工单已处理，请勿重复处理");
            }

            Alarm updateAlarm = new Alarm()
                    .setId(alarm.getId())
                    .setAlarmStatus(1)
                    .setNote(handleResult)
                    .setUpdateBy(SecurityUtils.getUsername())
                    .setUpdateTime(new Date());
            boolean updateResult = alarmService.updateById(updateAlarm);
            if (!updateResult) {
                throw new ServiceException("处理告警工单失败");
            }
            return R.ok();
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

