package com.jiayue.ipp.idp.controller.an;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.ipp.common.data.entity.an.ChannelStatus;
import com.jiayue.ipp.idp.service.an.ChannelStatusService;
import com.jiayue.ipp.idp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 通道状态表
 *
 * @author csc code generator
 * @date 2022-12-28 14:49:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/channelstatus")
@Api(value = "channelstatus", tags = "通道状态表管理")
public class ChannelStatusController {

    private final ChannelStatusService channelStatusService;

    /**
     * 根据上报时间 场站编号 通道状态 通道类型 查询
     *
     * @param stationCode       场站编号
     * @param startTime         上报时间
     * @param channelStatusCode 通道状态
     * @param channelTypeCode   通道类型
     * @return 查询条件
     */
    @ApiOperation(value = "根据场站编号分页查询", notes = "分页查询")
    @PostMapping("/findByStationCodeAndStartTimeBetweenAndChannelStatusAndChannelType")
    ////@PreAuthorize("@pms.hasPermission('da_weatherstationinfo_get')")
    public R findByStationCodeAndStartTimeBetweenAndChannelStatusAndChannelType(@RequestParam Long currentPage,
                                                                                @RequestParam Long pageSize,
                                                                                @RequestParam String stationCode,
                                                                                @RequestParam Long startTime,
                                                                                @RequestParam String channelStatusCode,
                                                                                @RequestParam String channelTypeCode) {
        Page<ChannelStatus> page = new Page<>(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(channelStatusService.page(page, channelStatusService.findByStationCodeAndStartTimeBetweenAndChannelStatusAndChannelType(stationCode, startTime, startTime + 60 * 60 * 24 * 1000 - 1000L, channelStatusCode, channelTypeCode)));
    }

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param channelStatus 通道状态表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    ////@PreAuthorize("@pms.hasPermission('da_channelstatus_get')")
    public R getChannelStatusPage(Page page, ChannelStatus channelStatus) {
        return R.ok(channelStatusService.page(page, Wrappers.query(channelStatus)));
    }

    /**
     * 通过id查询通道状态表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    ////@PreAuthorize("@pms.hasPermission('da_channelstatus_get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(channelStatusService.getById(id));
    }

    /**
     * 新增通道状态表
     *
     * @param channelStatus 通道状态表
     * @return R
     */
    @ApiOperation(value = "新增通道状态表", notes = "新增通道状态表")
    //    @SysLog("新增通道状态表")
    @PostMapping
    ////@PreAuthorize("@pms.hasPermission('da_channelstatus_add')")
    public R save(@RequestBody ChannelStatus channelStatus) {
        return R.ok(channelStatusService.save(channelStatus));
    }

    /**
     * 修改通道状态表
     *
     * @param channelStatus 通道状态表
     * @return R
     */
    @ApiOperation(value = "修改通道状态表", notes = "修改通道状态表")
    //    @SysLog("修改通道状态表")
    @PutMapping
    // //@PreAuthorize("@pms.hasPermission('da_channelstatus_edit')")
    public R updateById(@RequestBody ChannelStatus channelStatus) {
        return R.ok(channelStatusService.updateById(channelStatus));
    }

    /**
     * 通过id删除通道状态表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除通道状态表", notes = "通过id删除通道状态表")
    //    @SysLog("通过id删除通道状态表")
    @DeleteMapping("/{id}")
    // //@PreAuthorize("@pms.hasPermission('da_channelstatus_del')")
    public R removeById(@PathVariable String id) {
        return R.ok(channelStatusService.removeById(id));
    }


    /**
     * 查询异常通道信息
     *
     * @param channelStatus 通道状态表
     * @return
     */
    @ApiOperation(value = "查询异常通道信息", notes = "查询异常通道信息")
    @GetMapping("/getChannelExStatus")
    public R getChannelExStatus(ChannelStatus channelStatus) {
        return R.ok(channelStatusService.list(Wrappers.query(channelStatus).lambda().eq(ChannelStatus::getStatus,"1")));
    }
}
