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


import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
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.jldls.entity.CxcSssjLssjJldls;
import org.gyk.modules.lssj.jldls.mapper.CxcSssjLssjJldlsMapper;
import org.gyk.modules.lssj.jldls.service.ICxcSssjLssjJldlsService;
import org.gyk.modules.sssj.controller.GykController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 计量点历史数据
 * @Author: jeecg-boot
 * @Date: 2021-03-30
 * @Version: V1.0
 */
@Api(tags = "计量点历史数据")
@RestController
@RequestMapping("/jldls/cxcSssjLssjJldls")
@Slf4j
public class CxcSssjLssjJldlsController extends GykController {
    @Autowired
    private ICxcSssjLssjJldlsService cxcSssjLssjJldlsService;
    @Autowired
    private CxcSssjLssjJldlsMapper cxcSssjLssjJldlsMapper;

//	/**
//	 *   添加
//	 *
//	 * @param cxcSssjLssjJldls
//	 * @return
//	 */
//	@AutoLog(value = "计量点历史数据-添加")
//	@ApiOperation(value="计量点历史数据-添加", notes="计量点历史数据-添加")
//	@PostMapping(value = "/add")
//	@DS("slave")
//	public Result<?> add(@RequestBody CxcSssjLssjJldls cxcSssjLssjJldls) {
//		cxcSssjLssjJldlsService.save(cxcSssjLssjJldls);
//		return Result.ok("添加成功！");
//	}
//
//	/**
//	 *  编辑
//	 *
//	 * @param cxcSssjLssjJldls
//	 * @return
//	 */
//	@AutoLog(value = "计量点历史数据-编辑")
//	@ApiOperation(value="计量点历史数据-编辑", notes="计量点历史数据-编辑")
//	@PutMapping(value = "/edit")
//	@DS("slave")
//	public Result<?> edit(@RequestBody CxcSssjLssjJldls cxcSssjLssjJldls) {
//		cxcSssjLssjJldlsService.updateById(cxcSssjLssjJldls);
//		return Result.ok("编辑成功!");
//	}
//
//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "计量点历史数据-通过id删除")
//	@ApiOperation(value="计量点历史数据-通过id删除", notes="计量点历史数据-通过id删除")
//	@DeleteMapping(value = "/delete")
//	@DS("slave")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		cxcSssjLssjJldlsService.removeById(id);
//		return Result.ok("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "计量点历史数据-批量删除")
//	@ApiOperation(value="计量点历史数据-批量删除", notes="计量点历史数据-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	@DS("slave")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.cxcSssjLssjJldlsService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.ok("批量删除成功!");
//	}

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "计量点历史数据-通过id查询")
    @ApiOperation(value = "计量点历史数据-通过id查询", notes = "计量点历史数据-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("slave")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CxcSssjLssjJldls cxcSssjLssjJldls = cxcSssjLssjJldlsService.getById(id);
        if (cxcSssjLssjJldls == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(cxcSssjLssjJldls);
    }


    @AutoLog(value = "计量点历史数据(5分钟)-查询")
    @ApiOperation(value = "计量点历史数据(5分钟)-查询", notes = "计量点历史数据(5分钟)-分页列表查询")
    @GetMapping(value = "/getLssjByJldId")
    @DS("slave")
    public Result<String> getLssjByJldId(@RequestParam(name = "jldId", required = false) String jldId,
                                         @RequestParam(name = "startTime") String startTime,
                                         @RequestParam(name = "endTime") String endTime) {
        Result<String> result = new Result<>();

        if (startTime != null && startTime != "" && jldId !=null && jldId !="") {
            LambdaQueryWrapper<CxcSssjLssjJldls> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CxcSssjLssjJldls::getJldId, jldId);
            queryWrapper.ge(CxcSssjLssjJldls::getCreateTime, startTime);
            queryWrapper.le(CxcSssjLssjJldls::getCreateTime, endTime);
            List<CxcSssjLssjJldls> list = cxcSssjLssjJldlsService.list(queryWrapper);
            //对时间排序
            list.sort(Comparator.comparing(CxcSssjLssjJldls::getCreateTime));

            result.setSuccess(true);
            result.setResult(JSONArray.toJSONString(list));
        }
        return result;
    }

    //获取计量点历史数据   2023-08-28 Niujl
    @RequestMapping("/listLssjByJldName")
    public IPage listByZcAndJzh(@RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize") int pageSize, @RequestParam(value = "jldname") String jldname,  @RequestParam(name = "startTime", required = false) String startTime,
                                @RequestParam(name = "endTime", required = false) String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        log.info("name:"+jldname+" time:"+startTime);
//        long stime = System.currentTimeMillis();

        LambdaQueryWrapper<CxcSssjLssjJldls> lmq = Wrappers.lambdaQuery();
        lmq.between(CxcSssjLssjJldls::getCreateTime,startTime,endTime);
        lmq.eq(CxcSssjLssjJldls::getJldName,jldname);
        lmq.orderByAsc(CxcSssjLssjJldls::getCreateTime);

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

//        long etime = System.currentTimeMillis();
//        log.info("执行时长:"+(etime-stime));

        return list;
    }
}
