package com.kym.admin.controller;

import com.kym.admin.jobs.StationStatJob;
import com.kym.common.R;
import com.kym.entity.admin.StationStatMonth;
import com.kym.entity.admin.queryParams.StatQueryParam;
import com.kym.entity.admin.queryParams.StatementsQueryParam;
import com.kym.service.admin.StationStatMonthService;
import com.kym.service.miniapp.ChargeOrderService;
import org.springframework.web.bind.annotation.*;

/**
 * @author skyline
 * @description 统计数据
 * @date 2023-09-02 09:44
 */
@RestController
@RequestMapping("/stat")
public class StatController {

    private final ChargeOrderService chargeOrderService;
    private final StationStatMonthService stationStatMonthService;
    private final StationStatJob stationStatJob;

    public StatController(ChargeOrderService chargeOrderService, StationStatMonthService stationStatMonthService, StationStatJob stationStatJob) {
        this.chargeOrderService = chargeOrderService;
        this.stationStatMonthService = stationStatMonthService;
        this.stationStatJob = stationStatJob;
    }

    /**
     * 站点充电数据-日/月
     *
     * @param params
     * @return
     */
    @GetMapping("/stationStat")
    R<?> stationStat(@ModelAttribute StatQueryParam params) {
        return R.success(chargeOrderService.stationStat(params));
    }

    /**
     * 站点实时数据
     *
     * @return
     */
    @GetMapping("/stationTodayStat")
    R<?> stationTodayStat(String stationId) {
        return R.success(chargeOrderService.stationTodayStat(stationId));
    }

    /**
     * 站点详细数据统计
     *
     * @param params
     * @return
     */
    @GetMapping("/stationStatDetail")
    R<?> stationStatDetail(@ModelAttribute StatQueryParam params) {
        return R.success(chargeOrderService.stationStatDetail(params));
    }

    /**
     * 站点月统计数据列表
     *
     * @param params
     * @return
     */
    @GetMapping("/listStatMonth")
    R<?> listStatMonth(@ModelAttribute StatementsQueryParam params) {
        return R.success(stationStatMonthService.listStatMonth(params));
    }

    /**
     * 单条站点月统计数据
     *
     * @param statMonthId
     * @return
     */
    @GetMapping("/getStationStatMonth/{statMonthId}")
    R<?> getStationStatMonth(@PathVariable("statMonthId") String statMonthId) {
        return R.success(stationStatMonthService.getById(statMonthId));
    }

    /**
     * 修改站点月统计数据（填写电费账单数据）
     *
     * @param stationStatMonth
     * @return
     */
    @PostMapping("/modifyStationStatMonth")
    R<?> modifyStationStatMonth(@RequestBody StationStatMonth stationStatMonth) {
        stationStatMonthService.modifyStationStatMonth(stationStatMonth);
        return R.success();
    }

    /**
     * 重新同步指定月份的站点月统计数据
     * @param statMonth 格式: "yyyy-MM"
     */
    @PostMapping("/resyncStationStatMonth")
    R<?> resyncStationStatMonth(String statMonth) {
        try {
            int count = stationStatJob.resyncMonth(statMonth);
            return R.success("重新同步完成，共更新 " + count + " 条记录");
        } catch (IllegalArgumentException e) {
            return R.failed(400, e.getMessage());
        }
    }

}
