package com.ho.business.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ho.business.entity.ElecPrice;
import com.ho.business.service.ElecPriceService;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName ElecPriceController.java
 * @Description 电价管理
 * @createTime 2022年09月09日 11:09:00
 */

@RequestMapping(ContextConstant.BUSINESS + "price")
@RestController
@Slf4j
@Api(tags = "业务模块-电价管理")
public class ElecPriceController {

    @Autowired
    ElecPriceService elecPriceService;

    @Autowired
    RedisService redisService;

    //查询所有
    @GetMapping("/all")
    @ApiOperation(value = "查询所有电价接口", hidden = true)
    public DataResult<List<ElecPrice>> selectAll() {
        DataResult<List<ElecPrice>> result = DataResult.success();
        List<ElecPrice> elecPrices = elecPriceService.selectAll();
        result.setData(elecPrices);
        return result;
    }

    //根据用户查询所属集团电价
    @GetMapping("groupElecPrice")
    @ApiOperation(value = "集团最新电价列表")
    public DataResult<List<ElecPrice>> groupElecPrice(HttpServletRequest request) {

        String token = request.getHeader(CommonConstant.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        Integer groupId = simpleUser.getGroupId();
        //电站id不传,只查集团电价
        List<ElecPrice> elecPrices = elecPriceService.selectLatestPrices(groupId, null, null, null);
        return DataResult.success(elecPrices);
    }

    //根据电站id查询
    @GetMapping("/{id}")
    @ApiOperation(value = "根据电站id查询电价信息")
    //  @LogAnnotation(title = "监控管理", action = "根据电站id查询电价信息")
    public DataResult<List<ElecPrice>> selectById(@PathVariable @Valid Integer id) {
        DataResult<List<ElecPrice>> result = DataResult.success();
        SimpleDateFormat sdf = new SimpleDateFormat(CommonConstant.DATE);
        List<ElecPrice> elecPrices = null;
        String newTime = elecPriceService.selectStartTime(id);
        if (newTime != null) {
            DateTime parse = DateUtil.parse(newTime);
            DateTime beginOfDayTime = DateUtil.beginOfDay(parse);
            String beginTime = sdf.format(beginOfDayTime);
            //根据时间戳查询当天电价
            log.info("beginTime:" + beginTime);
            elecPrices = elecPriceService.selectByStationIdAndTime(id, beginTime);
        }
        result.setData(elecPrices);
        return result;
    }


}
