/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtymis.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.xrtymis.custom.em.NoiseStateEnum;
import com.tofly.xrtymis.custom.service.NoiseLoggerInfoService;
import com.tofly.xrtymis.em.Constant;
import com.tofly.xrtymis.entity.LeakInfo;
import com.tofly.xrtymis.entity.NoiseLoggerInfo;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ModelAttribute;

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

/**
 * 噪音上传记录表
 *
 * @author admin
 * @date Thu Mar 24 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/noiseupload")
@Api(tags="噪音上传记录表接口")
public class NoiseUploadController {

    private final NoiseLoggerInfoService noiseUploadService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param noiseUpload 噪音上传记录表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getNoiseUploadPage(Page page, @ModelAttribute NoiseLoggerInfo noiseUpload) {
      return ResultRespone.success(noiseUploadService.page(page, new QueryWrapper<NoiseLoggerInfo>().lambda()
              .ge(null!=noiseUpload.getStartTime(),NoiseLoggerInfo::getLastValueDate,noiseUpload.getStartTime())
              .le(null!=noiseUpload.getEndTime(),NoiseLoggerInfo::getLastValueDate,noiseUpload.getEndTime())
              .eq(null!=noiseUpload.getDisposeState(),NoiseLoggerInfo::getDisposeState,noiseUpload.getDisposeState())
              .eq(null!=noiseUpload.getMonitoringState(),NoiseLoggerInfo::getMonitoringState,noiseUpload.getMonitoringState())
      .like(StringUtils.isNotBlank(noiseUpload.getLikeStr()),NoiseLoggerInfo::getMobileNumber,noiseUpload.getLikeStr())));
    }


    @ApiOperation(value = "听取次数修改")
    @PutMapping("updateListenNum")
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone updateListenNum(@RequestParam(value = "id") Long id) {
        NoiseLoggerInfo byId = noiseUploadService.getById(id);
        long l = byId.getListenNum() + 1L;
        return ResultRespone.success(noiseUploadService.update(new UpdateWrapper<NoiseLoggerInfo>().lambda().eq(NoiseLoggerInfo::getId,id)
                .set(NoiseLoggerInfo::getListenNum,l)));
    }

    @GetMapping("/ignore")
    @ApiOperation(value = "噪音一键忽略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noises", value = "噪音数据id,多个以,分隔", required = true, dataType = "String")
    })
    public ResultRespone getNoiseUploadPage(String noises) {
        List<Long> collect = Arrays.stream(noises.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        return ResultRespone.success(noiseUploadService.update(new UpdateWrapper<NoiseLoggerInfo>().lambda()
                .in(NoiseLoggerInfo::getId,collect)
                .set(NoiseLoggerInfo::getDisposeState, NoiseStateEnum.HL.getCode())));
    }

    @GetMapping("/getLeakDeviceArchivePage" )
    @ApiOperation(value = "获取噪声监测列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitoringState", value = "监测状态 0正常,1异常", required = false, dataType = "String"),
            @ApiImplicitParam(name = "likeStr", value = "模糊查询(设备名称,设备编号)", required = false, dataType = "String")
    })
    public ResultRespone getLeakDeviceArchivePage(Page page, String monitoringState,String likeStr) {
        return ResultRespone.success(noiseUploadService.getLeakDeviceArchivePage(page, monitoringState, likeStr));
    }
}
