package com.kym.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.kym.common.R;
import com.kym.common.annotation.SysLog;
import com.kym.entity.admin.Station;
import com.kym.service.admin.StationService;
import com.kym.service.utils.KymCache;
import org.springframework.context.annotation.Lazy;
import org.springframework.format.annotation.DateTimeFormat;
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 static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNotNull;

/**
 * <p>
 * 充电站信息 前端控制器
 * </p>
 *
 * @author skyline
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/station")
public class StationController {

    private final StationService stationService;

    private final KymCache kymCache;

    public StationController(StationService stationService, @Lazy KymCache kymCache) {
        this.stationService = stationService;
        this.kymCache = kymCache;
    }

    @SysLog("充电站数据统计")
    @GetMapping("/stationStatusDetail")
    R stationStatusDetail(@RequestParam("stationId") String stationId,
                          @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") String startTime,
                          @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") String endTime) {
        // 请求en+接口
        var response = stationService.stationStats(stationId, startTime, endTime);
        return R.success(response);
    }

    // TODO: 2023-09-01 充电站列表（站点id,站点名称，站点编号，站点类型，充电桩数量，站点状态）

    @GetMapping("listStation")
    R listStation(@RequestParam(required = false) String stationName) {
        // 判断数据权限
        var adminStationId = kymCache.getAdminUserStationId(StpUtil.getLoginIdAsLong());
        var res = stationService.lambdaQuery()
                .eq(isNotNull(adminStationId), Station::getStationId, adminStationId)
                .like(isNotNull(stationName), Station::getStationName, stationName)
                .orderByDesc(Station::getStationId).list();
        return R.success(res);
    }

    /**
     * 拉取EN+充电站信息数据并更新本地服务器数据
     *
     * @return
     */
    @GetMapping("/pullEnStations")
    R pullEnStationInfos() {
        stationService.pullEnStationInfos();
        return R.success();
    }

}
