package com.ylx.web.controller.massage;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.StringUtils;
import com.ylx.massage.domain.YlxAlarm;
import com.ylx.massage.service.YlxAlarmService;
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;

/**
 * 报警记录表(YlxAlarm)表控制层
 *
 * @author makejava
 * @since 2024-04-28 09:19:04
 */
@RestController
@RequestMapping("ylxAlarm")
@Api(tags = {"报警记录"})
public class YlxAlarmController {
    /**
     * 服务对象
     */
    @Resource
    private YlxAlarmService ylxAlarmService;

    /**
     * 分页查询
     *
     * @param ylxAlarm    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping("getAlarmList")
    @ApiOperation("Pc报警列表")
    public R queryByPage(Page<YlxAlarm> page, YlxAlarm ylxAlarm) {
        LambdaQueryWrapper<YlxAlarm> alarmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        alarmLambdaQueryWrapper.eq(StringUtils.isNotBlank(ylxAlarm.getJsId()), YlxAlarm::getJsId, ylxAlarm.getJsId()).
                eq(StringUtils.isNotBlank(ylxAlarm.getOpenId()), YlxAlarm::getOpenId, ylxAlarm.getOpenId()).
                eq(null != ylxAlarm.getAlarmStatus(), YlxAlarm::getAlarmStatus, ylxAlarm.getAlarmStatus()).
                like(StringUtils.isNotBlank(ylxAlarm.getJsName()), YlxAlarm::getJsName, ylxAlarm.getJsName()).
                like(StringUtils.isNotBlank(ylxAlarm.getJsPhone()), YlxAlarm::getJsPhone, ylxAlarm.getJsPhone()).
                like(StringUtils.isNotBlank(ylxAlarm.getUserPhone()), YlxAlarm::getUserPhone, ylxAlarm.getUserPhone()).
                like(StringUtils.isNotBlank(ylxAlarm.getUserName()), YlxAlarm::getUserName, ylxAlarm.getUserName()).orderByDesc(YlxAlarm::getCreateTime);
        // 获取查询返回结果
        Page<YlxAlarm> pageSelect = ylxAlarmService.page(page, alarmLambdaQueryWrapper);
        return R.ok(pageSelect);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<YlxAlarm> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.ylxAlarmService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param ylxAlarm 实体
     * @return 新增结果
     */
    @PostMapping("/addAlarm")
    @ApiOperation("添加报警记录")
    @Log(title = "添加报警记录", businessType = BusinessType.INSERT)
    public R<YlxAlarm> addAlarm(@RequestBody YlxAlarm ylxAlarm) {
        return R.ok(this.ylxAlarmService.addAlarm(ylxAlarm));
    }



    @PostMapping("/handleAlarm")
    @ApiOperation("已处理")
    @Log(title = "处理报警记录", businessType = BusinessType.UPDATE)
    public R handleAlarm(@RequestBody YlxAlarm ylxAlarm) {
        if(StringUtils.isBlank(ylxAlarm.getId())){
            throw new ServiceException("id不能为空");
        }
        ylxAlarm.setAlarmStatus(1);
        return R.ok(ylxAlarmService.updateById(ylxAlarm));
    }


    @PostMapping("/deleteAlarm")
    @ApiOperation("删除")
    @Log(title = "处理报警记录", businessType = BusinessType.UPDATE)
    public R deleteAlarm(@RequestBody YlxAlarm ylxAlarm) {
        return R.ok(ylxAlarmService.removeById(ylxAlarm.getId()));
    }

    /**
     * 编辑数据
     *
     * @param ylxAlarm 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<YlxAlarm> edit(YlxAlarm ylxAlarm) {
        return ResponseEntity.ok(this.ylxAlarmService.update(ylxAlarm));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.ylxAlarmService.deleteById(id));
    }

}

