package com.huimv.env.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.env.admin.common.utils.DataUill;
import com.huimv.env.admin.common.utils.DateUtil;
import com.huimv.env.admin.common.utils.Result;
import com.huimv.env.admin.common.utils.ResultCode;
import com.huimv.env.admin.entity.EnvData;
import com.huimv.env.admin.entity.vo.DataListByPortCodeParam;
import com.huimv.env.admin.service.IEnvDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2024-05-09
 */
@RestController
@CrossOrigin
@RequestMapping("/env-data")
public class EnvDataController {
    @Autowired
    private IEnvDataService envDataService;

    @PostMapping("/listByPortCode")
    public Result listByPortCode(@RequestBody DataListByPortCodeParam dataListByPortCodeParam){
        String startTime = dataListByPortCodeParam.getStartTime();
        String endTime = dataListByPortCodeParam.getEndTime();
        QueryWrapper<EnvData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EnvData::getTerminalPortCode, dataListByPortCodeParam.getPortCode()).orderByAsc(EnvData::getCreateTime);
        if (ObjectUtil.isEmpty(startTime)) {
            queryWrapper.lambda().ge(EnvData::getCreateTime, DataUill.getTimesmorning());
        }else {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            queryWrapper.lambda().between(EnvData::getCreateTime,startTime,endTime);
        }
        return new Result(ResultCode.SUCCESS,envDataService.list(queryWrapper));
    }

}
