package com.ho.business.controller;



import com.ho.business.entity.IndustrialElecPrice;
import com.ho.business.service.IndustrialElecPriceService;
import com.ho.business.vo.req.industrialElec.IndustrialElecPriceReqVo;
import com.ho.common.tools.annotation.HzPermission;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.PermissionConstant;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
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.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RequestMapping(ContextConstant.BUSINESS + "industrialElecPrice")
@RestController
@Slf4j
@Api(tags = "业务模块-工商业代理电价")
public class IndustrialElecPriceController {

    @Autowired
    private IndustrialElecPriceService industrialElecPriceService;

    @PostMapping("page")
    @ApiOperation(value = "查询所有代理电价-分页")
    public DataResult<PageResult<IndustrialElecPrice>> page(@RequestBody IndustrialElecPriceReqVo reqVo) {
        return industrialElecPriceService.getElecPriceList(reqVo);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增代理电价")
    @HzPermission(PermissionConstant.BUSINESS_INDUSTRIALPRICE_ADD)
    public DataResult add(@RequestBody @Valid IndustrialElecPrice industrialElecPrice) {
        industrialElecPrice.setCreateTime(new Date());
        industrialElecPriceService.add(industrialElecPrice);
        return DataResult.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "删除代理电价")
    @HzPermission(PermissionConstant.BUSINESS_INDUSTRIALPRICE_DEL)
    public DataResult deleteById(@RequestBody IndustrialElecPriceReqVo industrialElecPriceReqVo) {
        industrialElecPriceService.deleteById(industrialElecPriceReqVo.getId());
        return DataResult.success();
    }

    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id显示详情")
    public DataResult findById(@PathVariable("id")Long id) {
        IndustrialElecPrice industrialElecPrice = industrialElecPriceService.findById(id);
        return DataResult.success(industrialElecPrice);
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新")
    @HzPermission(PermissionConstant.BUSINESS_INDUSTRIALPRICE_EDIT)
    public DataResult updateById(@RequestBody @Valid IndustrialElecPrice industrialElecPrice) {
        industrialElecPrice.setUpdateTime(new Date());
        industrialElecPriceService.updateById(industrialElecPrice);
        return DataResult.success();
    }

    @GetMapping("/getTouLineById/{id}")
    @ApiOperation(value = "根据id查询分时电价折线图")
    public DataResult getTouLineById(@PathVariable("id")Long id) {
        List<Map<String,Object>> lineData = industrialElecPriceService.getTouLineById(id);
        return DataResult.success(lineData);
    }

    @GetMapping("/getTouLineHistoryById/{id}")
    @ApiOperation(value = "根据id查询分时电价历史数据图表/表格")
    public DataResult getTouLineHistoryById(@PathVariable("id")Long id) {
        Map<String,Object> lineData = industrialElecPriceService.getTouLineHistoryById(id);
        return DataResult.success(lineData);
    }

    @GetMapping("/getMaxPriceDiffById/{id}")
    @ApiOperation(value = "根据id查询峰谷电差价折线图/表格")
    public DataResult getMaxPriceDiffById(@PathVariable("id")Long id) {
        Map<String,Object> lineData = industrialElecPriceService.getMaxPriceDiffById(id);
        return DataResult.success(lineData);
    }

    @PostMapping("/collectPrice")
    @ApiOperation(value = "通过图片识别采集数据")
    @HzPermission(PermissionConstant.BUSINESS_INDUSTRIALPRICE_COLLECT)
    public DataResult collectPrice(@RequestPart("file") MultipartFile file, @RequestParam("regionId") Integer regionId, @RequestParam("executionTime") String executionTime) {
        DataResult dataResult = industrialElecPriceService.collectPrice(file,regionId,executionTime);
        return dataResult;
    }
}
