package com.sy.coinage.workshop.controller;

import cn.hutool.core.date.DateUtil;
import com.sy.coinage.config.CustomConfig;
import com.sy.coinage.core.util.DateUtils;
import com.sy.coinage.core.util.ExcelExportUtil;
import com.sy.coinage.log.model.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.*;
import com.sy.coinage.core.vo.*;

import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.IHumidityTemperatureService;
import com.sy.coinage.workshop.vo.*;
import com.sy.coinage.workshop.req.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping(value = "/humidityTemperature")
public class HumidityTemperatureController extends BaseController {

    @Autowired
    private IHumidityTemperatureService humidityTemperatureService;

    @ApiOperation(value = "分页列表")
    @PostMapping("/page")
    @ActiveFastJsonProfile(groups = "list", clazz = HumidityTemperature.class)
    public IPage<HumidityTemperature> page(@RequestBody @ApiParam(value = "列表查询请求bean") HumidityTemperatureListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<HumidityTemperature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq((req.getWareId() != null && req.getWareId() > 0), HumidityTemperature::getWareId, req.getWareId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(HumidityTemperature::getCreated, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(HumidityTemperature::getCreated, DateUtils.getDayEndTime(req.getEndDate()));
        }
        Page<HumidityTemperature> page = new Page<>(req.getPage(), req.getRows());
        IPage<HumidityTemperature> data = this.humidityTemperatureService.page(page, queryWrapper);
        return data;
    }

    private static String[] codes = {"temperature", "humidity", "created", "createTime", "wareCn"};
    private static String[] names = {"温度", "湿度", "创建日期", "汇报时间", "位置"};

    @Autowired
    private CustomConfig customConfig;

    @ApiOperation(value = "导出")
    @PostMapping("/exp")
    public FileVO exp(@RequestBody HumidityTemperatureListReq req) {
        FileVO data = new FileVO();
        LambdaQueryWrapper<HumidityTemperature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq((req.getWareId() != null && req.getWareId() > 0), HumidityTemperature::getWareId, req.getWareId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(HumidityTemperature::getCreated, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(HumidityTemperature::getCreated, DateUtils.getDayEndTime(req.getEndDate()));
        }
        List<HumidityTemperature> list = this.humidityTemperatureService.list(queryWrapper);
        ExcelExportUtil<HumidityTemperature> excelExport = new ExcelExportUtil<>();
        String fileName = DateUtils.toString(new Date()) + "温湿度记录.xlsx";
        data.setName(fileName);
        String url = excelExport.exportData(names, codes, list, fileName, customConfig.getAttaPath(), customConfig.getAttaUrl());
        data.setUrl(url);
        return data;
    }

}
