package com.xhpc.charging.station.controller;

import com.xhpc.charging.station.service.IXhpcTimingChargingModelService;
import com.xhpc.common.api.dto.XhpcTimingChargingModelDto;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import com.xhpc.common.core.domain.R;

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

/**
 * @author yuyang
 * @date 2022/2/22 13:53
 */
@RestController
@RequestMapping("/timingChargingModel")
public class XhpcTimingChargingModelController extends BaseController {


    @Autowired
    private IXhpcTimingChargingModelService xhpcTimingChargingModelService;

    /**
     * 定时计费模型列表
     * @param reason 调整原因
     * @param status   状态（0未执行 1已执行 2执行错误）
     * @param chargingStationId  场站id
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request,String tenantId,Long operatorId, String reason, Integer status, String startTime, String endTime, Long chargingStationId) {
        startPage();
        List<Map<String, Object>> list = xhpcTimingChargingModelService.list(request,tenantId,operatorId,reason, status, startTime,endTime, chargingStationId);
        return getDataTable(list);
    }

    /**
     * 添加定时计费模型
     * @param xhpcTimingChargingModelDto
     * @return
     */
    @Log(title = "添加定时计费模型", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addXhpcTimingChargingModel")
    public R addXhpcTimingChargingModel(HttpServletRequest request, @RequestBody XhpcTimingChargingModelDto xhpcTimingChargingModelDto) {
        return xhpcTimingChargingModelService.addXhpcTimingChargingModel(request,xhpcTimingChargingModelDto);
    }


    /**
     * 删除
     */
    @Log(title = "删除", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{timingChargingModelId}")
    public R updateXhpcTimingChargingModel(@PathVariable Long timingChargingModelId) {
        return xhpcTimingChargingModelService.updateXhpcTimingChargingModel(timingChargingModelId);
    }

    /**
     * 详情
     *
     * @return
     */
    @GetMapping(value = "/getXhpcTimingChargingModelMessage")
    public R getXhpcTimingChargingModelMessage(Long timingChargingModelId) {
        return xhpcTimingChargingModelService.getXhpcTimingChargingModelMessage(timingChargingModelId);
    }


    /**
     * 编辑定时计费模型
     * @param xhpcTimingChargingModelDto
     * @return
     */
    @Log(title = "编辑定时计费模型", businessType = BusinessType.INSERT)
    @PostMapping(value = "/updateXhpcTimingChargingModel")
    public R updateXhpcTimingChargingModel(HttpServletRequest request, @RequestBody XhpcTimingChargingModelDto xhpcTimingChargingModelDto) {

        if(xhpcTimingChargingModelDto.getTimingChargingModelId()==null){
            return R.fail("定时计费模型id比传");
        }
        return xhpcTimingChargingModelService.addXhpcTimingChargingModel(request,xhpcTimingChargingModelDto);
    }

    /**
     * 每30秒执行一次
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void getXhpcTimingChargingModelCron(){
        xhpcTimingChargingModelService.getXhpcTimingChargingModelCron();
    }


}
