package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dto.HotSpotTemperatureResultDto;
import com.huatek.busi.service.HotSpotTemperatureResultService;
import com.huatek.busi.vo.HotSpotTemperatureResultVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 *  <p>
 *     热点温度计算结果表 前端控制器
 * </p>
 *
 * @author dong
 * @since 2023-07-19
 */
@Api("hotSpotTemperatureResult信息API")
@RestController
@RequestMapping(value = "/api/hotSpotTemperatureResult")
public class HotSpotTemperatureResultController {

    @Reference(check = false)
    private HotSpotTemperatureResultService hotSpotTemperatureResultService;


    @ApiOperation(value = "热点温度计算结果表分页")
    @GetMapping(value = "/page")
    public Map<String, Object> page(
                                    @RequestParam ("siteId") String siteId,
                                    @RequestParam(value = "mainTreeId") String mainTreeId,
                                    @RequestParam(value = "startTime", required = false) String startTime,
                                    @RequestParam(value = "endTime", required = false) String endTime,
                                    @RequestParam(value = "page", required = false) Integer page,
                                    @RequestParam(value = "type", required = false) String type ,
                                    @RequestParam(value = "limit", required = false) Integer limit) {

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = null;
        LocalDate end = null;


        if (StringUtils.isNotBlank(startTime)) {
            start = LocalDate.parse(startTime, df);
        }

        if (StringUtils.isNotBlank(endTime)) {
            end = LocalDate.parse(endTime, df).plusDays(1);
        }

        PageInfo<HotSpotTemperatureResultVo> pageInfo = hotSpotTemperatureResultService.page(siteId,mainTreeId, start, end, page, limit, type);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }


    @ApiOperation(value = "新增热点温度计算")
    @PostMapping(value = "/calculate", produces = { "application/json;charset=utf-8" })
    public Map<String, Object>  add(@RequestBody HotSpotTemperatureResultDto dto) {
        boolean flag = hotSpotTemperatureResultService.calculate(dto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, flag ? "计算成功" :"计算失败" , "");
    }

    @ApiOperation(value = "结果追溯")
    @GetMapping(value = "/getResult/{id}", produces = { "application/json;charset=utf-8" })
    public Map<String, Object>  add(@PathVariable String id) {
        HotSpotTemperatureResultVo vo = hotSpotTemperatureResultService.selectById(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "" , vo);
    }

}
