package org.gyk.modules.sz.bjpz.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.thrift.TException;
import org.gyk.common.annotation.AutoLog;
import org.gyk.common.api.Result;
import org.gyk.modules.sz.bjpz.entity.CxcSssjSzBjsz;
import org.gyk.modules.sz.bjpz.service.ICxcSssjSzBjszService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)

@RequestMapping("/sz")
@RestController
public class CxcSssjSzBjszController {
    @Autowired
    private ICxcSssjSzBjszService cxcSssjSzBjszService;

    /**
     * 通过计量点ID查询获取某条记录
     * @param bjId
     * @return Result<String>
     */
    @RequestMapping("/GetBjdId")
    public Result<String> GetBjdId(@RequestParam(value = "bjId") String bjId) throws TException, InterruptedException {
        Result <String> result=new Result<>();
        LambdaQueryWrapper<CxcSssjSzBjsz> queryWrapper = new LambdaQueryWrapper<CxcSssjSzBjsz>();
        queryWrapper.eq(CxcSssjSzBjsz::getId, bjId);
        List<CxcSssjSzBjsz> listJldId = cxcSssjSzBjszService.list(queryWrapper);
        result.setSuccess(true);
        result.setResult(JSON.toJSONString(listJldId));
//		 result.setResult(JSONArray.toJSONString(listZhan));
//		 String res = JSON.toJSONString(listZhan);
//        System.out.println("返回结果1:"+result);
        return  result;
    }

    /*** 通过计量点IDs查询获取所有记录
     * @param bjIDs
     * @return Result<String>
     */
    @RequestMapping("/GetBjdIds")
    public Result<String> GetBjdIds(@RequestParam(value = "bjIDs") String bjIDs) throws TException, InterruptedException {
        Result <String> result=new Result<>();
        LambdaQueryWrapper<CxcSssjSzBjsz> queryWrapper = new LambdaQueryWrapper<CxcSssjSzBjsz>();
        Arrays.asList(bjIDs.split(",")).forEach(item -> {
            queryWrapper.eq(CxcSssjSzBjsz::getId, item);
        });
        List<CxcSssjSzBjsz> listJldId = cxcSssjSzBjszService.list(queryWrapper);
        result.setSuccess(true);
        result.setResult(JSON.toJSONString(listJldId));
//		 result.setResult(JSONArray.toJSONString(listZhan));
//		 String res = JSON.toJSONString(listZhan);
//        System.out.println("返回结果1:"+result);
        return  result;
    }


    /**
     *   添加
     *
     * @param cxcSssjSzBjsz
     * @return
     */
    @AutoLog(value = "参数报警信息表-添加")
    @ApiOperation(value="参数报警信息表-添加", notes="参数报警信息表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CxcSssjSzBjsz cxcSssjSzBjsz) {
        cxcSssjSzBjszService.save(cxcSssjSzBjsz);
        return Result.ok("添加成功！");
    }

    /**
     *  编辑
     *
     * @param cxcSssjSzBjsz
     * @return
     */
    @AutoLog(value = "参数报警信息表-编辑")
    @ApiOperation(value="参数报警信息表-编辑", notes="参数报警信息表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CxcSssjSzBjsz cxcSssjSzBjsz) {

        cxcSssjSzBjszService.updateById(cxcSssjSzBjsz);
        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) {
        cxcSssjSzBjszService.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.cxcSssjSzBjszService.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) {
        CxcSssjSzBjsz cxcSssjSzBjsz = cxcSssjSzBjszService.getById(id);
        if(cxcSssjSzBjsz==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(cxcSssjSzBjsz);
    }
}
