package com.xhpc.activity.controller;


import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.activity.service.PileLogService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 桩运行日志
 */
@RestController
@RequestMapping("/log/pile")
public class PileLogController extends BaseController {

    @Resource
    PileLogService pileLogService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPilePage(
            HttpServletRequest request,
            @RequestParam(required = false) String stationName,
            @RequestParam(required = false) String pileName,
            @RequestParam(required = false) String pileSerialNumber,
            @RequestParam(required = false) Integer number,
            @RequestParam(required = false) Long operatorId,
            @RequestParam(required = false) Long chargingStationId,
            @RequestParam(required = false) Long chargingPileId) {
        startPage();

        Map<String, Object> params = new HashMap<>();
        params.put("stationName", stationName);
        params.put("pileName", pileName);
        params.put("operatorId", operatorId);
        params.put("chargingStationId", chargingStationId);
        params.put("chargingPileId", chargingPileId);
        params.put("number", number);
        params.put("pileSerialNumber", pileSerialNumber);

        return getDataTable(pileLogService.getPilePage(params));
    }


    @GetMapping("/detail")
    public TableDataInfo getPileDetailPage(@RequestParam("pileId") String pileId,
                                           @RequestParam(required = false) String startTime,
                                           @RequestParam(required = false) String endTime) {

        startPage();
        return getDataTable(pileLogService.getPileRunLogPage(pileId, startTime, endTime));
    }


    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam("pileId") String pileId,
                       @RequestParam(required = false) String tenantId,
                       @RequestParam(required = false) Integer number,
                       @RequestParam(required = false) String startTime,
                       @RequestParam(required = false) String endTime,
                       @RequestParam(required = false) String serialNumber) throws Exception {

        Map<String, Object> params = new HashMap<>();
        params.put("operatorId", SecurityUtils.getUserId());
        params.put("number", number);
        params.put("type", StationDeviceEnum.PILE.getCode());
        params.put("serialNumber", serialNumber);
        params.put("tenantId", tenantId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("pileId", pileId);
        pileLogService.export(response, params);
    }

}
