package org.gyk.modules.lssj.bjls.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gyk.common.annotation.AutoLog;
import org.gyk.common.api.Result;
import org.gyk.modules.lssj.bjls.entity.CxcSssjLssjBjlsjl;
import org.gyk.modules.lssj.bjls.mapper.CxcSssjLssjBjlsjlMapper;
import org.gyk.modules.lssj.bjls.service.ICxcSssjLssjBjlsjlService;
import org.gyk.modules.sssj.controller.GykController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;

/**
* @Description: 报警历史记录
* @Author: jeecg-boot
* @Date:   2021-03-17
* @Version: V1.0
*/
@Api(tags="报警历史记录")
@RestController
@RequestMapping("/bjls/cxcSssjLssjBjlsjl")
@Slf4j
public class CxcSssjLssjBjlsjlController extends GykController{
   @Autowired
   private ICxcSssjLssjBjlsjlService cxcSssjLssjBjlsjlService;
   @Autowired
   private CxcSssjLssjBjlsjlMapper cxcSssjLssjBjlsjlMapper;

   /**
    *   添加
    *
    * @param cxcSssjLssjBjlsjl
    * @return
    */
   @AutoLog(value = "报警历史记录-添加")
   @ApiOperation(value="报警历史记录-添加", notes="报警历史记录-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody CxcSssjLssjBjlsjl cxcSssjLssjBjlsjl) {
       cxcSssjLssjBjlsjlService.save(cxcSssjLssjBjlsjl);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param cxcSssjLssjBjlsjl
    * @return
    */
   @AutoLog(value = "报警历史记录-编辑")
   @ApiOperation(value="报警历史记录-编辑", notes="报警历史记录-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody CxcSssjLssjBjlsjl cxcSssjLssjBjlsjl) {
       cxcSssjLssjBjlsjlService.updateById(cxcSssjLssjBjlsjl);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "报警历史记录-通过id删除")
   @ApiOperation(value="报警历史记录-通过id删除", notes="报警历史记录-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       cxcSssjLssjBjlsjlService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "报警历史记录-批量删除")
   @ApiOperation(value="报警历史记录-批量删除", notes="报警历史记录-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.cxcSssjLssjBjlsjlService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "报警历史记录-通过id查询")
   @ApiOperation(value="报警历史记录-通过id查询", notes="报警历史记录-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       CxcSssjLssjBjlsjl cxcSssjLssjBjlsjl = cxcSssjLssjBjlsjlService.getById(id);
       if(cxcSssjLssjBjlsjl==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(cxcSssjLssjBjlsjl);
   }

    //获取计量点报警历史数据   2023-08-28 Niujl
    @RequestMapping("/listBjByZc")
    public IPage listByZcAndJzh(@RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize") int pageSize, @RequestParam(value = "zhanc") String zhanc, @RequestParam(name = "startTime", required = false) String startTime,
                                @RequestParam(name = "endTime", required = false) String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        LambdaQueryWrapper<CxcSssjLssjBjlsjl> lmq = Wrappers.lambdaQuery();
        lmq.between(CxcSssjLssjBjlsjl::getCreateTime,startTime,endTime);
        lmq.eq(CxcSssjLssjBjlsjl::getBz,zhanc);
        lmq.orderByAsc(CxcSssjLssjBjlsjl::getCreateTime);

        IPage<CxcSssjLssjBjlsjl> iPage = new Page<>(pageNo,pageSize);
        IPage<CxcSssjLssjBjlsjl> list = cxcSssjLssjBjlsjlMapper.selectPage(iPage,lmq);

        return list;
    }

}
