package com.hw.ems.record.controller;

import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.ems.record.domain.RecordDnbInstant;
import com.hw.ems.record.domain.vo.RecordDnbInstantDataVo;
import com.hw.ems.record.service.IRecordDnbInstantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 电实时数据Controller
 *
 * @author YinQ
 * @date 2023-04-07
 */
@Controller
@RequestMapping("/record/dnbInstant")
public class RecordDnbInstantController extends BaseController {
    private String prefix = "record/dnbInstant";

    @Autowired
    private IRecordDnbInstantService recordDnbInstantService;

    @RequiresPermissions("record:dnbInstant:view")
    @GetMapping()
    public String dnbInstant()
    {
        return prefix + "/dnbInstant";
    }

    /**
     * 获取电实时数据 定时任务用
     * */
    @PostMapping("/getDnbInstant")
    public void getDnbInstant(){
        recordDnbInstantService.getDnbInstantByJob();
    }
    /**
     * 点钞数据
     * @return
     */
    @GetMapping("/dnbCountingData")
    public String dnbCountingData()
    {
        return prefix + "/dnbCountingData";
    }

    @GetMapping("/dnbHistoryData")
    public String dnbHistoryData()
    {
        return prefix + "/dnbHistoryData";
    }

    /**
     * 手动抄表
     * @return
     */
    @GetMapping("/dnbMeterReading")
    public String dnbMeterReading()
    {
        return prefix + "/dnbMeterReading";
    }

    /**
     * 查询电实时数据列表
     */
    @RequiresPermissions("record:dnbInstant:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordDnbInstant recordDnbInstant)
    {
        startPage();
        RecordDnbInstantDataVo recordDnbInstantDataVo = recordDnbInstantService.selectRecordDnbInstantList(recordDnbInstant);
        List<RecordDnbInstant> records = recordDnbInstantDataVo.getRecordDnbInstants();
        int count = recordDnbInstantDataVo.getCount();
        return getDataTable(records,count);
    }

    /**
     * 获取最新实时电数据
     */
    @PostMapping("/selectRealTimeList")
    @ResponseBody
    public TableDataInfo selectRealTimeParentList(RecordDnbInstant baseDnbInstant)
    {
        List<RecordDnbInstant> list = recordDnbInstantService.selectRealTimeParentList(baseDnbInstant);
        return getDataTable(list);
    }

    /**
     * 实时数据
     * @param recordDnbInstant
     * @return
     */
    @GetMapping("/instantList")
    @ResponseBody
    public TableDataInfo instantList(RecordDnbInstant recordDnbInstant)
    {
//        startPage();
        List<RecordDnbInstant> list = recordDnbInstantService.instantList(recordDnbInstant);
        return getDataTable(list);
    }

    /**
     * 导出电实时数据列表
     */
    @RequiresPermissions("record:dnbInstant:export")
    @Log(title = "电实时数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response,RecordDnbInstant recordDnbInstant)
    {
        RecordDnbInstantDataVo recordDnbInstantDataVo = recordDnbInstantService.selectRecordDnbInstantList(recordDnbInstant);
        List<RecordDnbInstant> list = recordDnbInstantDataVo.getRecordDnbInstants();
        ExcelUtil<RecordDnbInstant> util = new ExcelUtil<>(RecordDnbInstant.class);
        util.exportExcel(response,list, "电实时数据数据");
    }

    /**
     * 新增电实时数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电实时数据
     */
    @RequiresPermissions("record:dnbInstant:add")
    @Log(title = "电实时数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody RecordDnbInstant recordDnbInstant)
    {
        return toAjax(recordDnbInstantService.insertRecordDnbInstant(recordDnbInstant));
    }

    /**
     * 修改电实时数据
     */
    @RequiresPermissions("record:dnbInstant:edit")
    @GetMapping("/edit/{objid}")
    public String edit(@PathVariable("objid") Long objid, ModelMap mmap)
    {
        RecordDnbInstant recordDnbInstant = recordDnbInstantService.selectRecordDnbInstantByObjid(objid);
        mmap.put("recordDnbInstant", recordDnbInstant);
        return prefix + "/edit";
    }

    /**
     * 修改保存电实时数据
     */
    @RequiresPermissions("record:dnbInstant:edit")
    @Log(title = "电实时数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordDnbInstant recordDnbInstant)
    {
        return toAjax(recordDnbInstantService.updateRecordDnbInstant(recordDnbInstant));
    }

    /**
     * 删除电实时数据
     */
    @RequiresPermissions("record:dnbInstant:remove")
    @Log(title = "电实时数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordDnbInstantService.deleteRecordDnbInstantByObjids(ids));
    }

    /**
     * 表具实时状态
     * @param dnbInstant
     * @return
     */
    @PostMapping( "/tablewareDetailsInfo")
    @ResponseBody
    public RecordDnbInstant tablewareDetailsInfo(RecordDnbInstant dnbInstant){
        RecordDnbInstant recordDnbInstants = recordDnbInstantService.tablewareDetailsInfo(dnbInstant);
        return recordDnbInstants;
    }

    /**
     * 实时电压电流曲线
     * @param baseDnbInstant
     * @return
     */
    @PostMapping("/voltageCurrentCurve")
    @ResponseBody
    public AjaxResult voltageCurrentCurve(RecordDnbInstant baseDnbInstant) {
        return AjaxResult.success(recordDnbInstantService.voltageCurrentCurve(baseDnbInstant));
    }

}
