package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.*;
import com.nantian.cloud.jljc.service.*;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 企业能耗准确性分析详细信息表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/epcEnergyAccuracyAnalysisInfo")
@Api(value = "epcEnergyAccuracyAnalysisInfo", tags = "企业能耗准确性分析详细信息表")
@CrossOrigin
public class EpcEnergyAccuracyAnalysisInfoInfoController {
    private final EpcEnergyAccuracyAnalysisInfoService epcEnergyAccuracyAnalysisInfoService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @SysLog("分页获取企业能耗准确性分析详细信息表")
    @ApiOperation(value = "分页获取企业能耗准确性分析详细信息表", notes = "分页获取企业能耗准确性分析详细信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "epcEnergyAccuracyAnalysisInfo", value = "企业能耗准确性分析详细信息表对象", required = false, dataType = "json", paramType = "path")
    })
    public R page(Page page, EpcEnergyAccuracyAnalysisInfo epcEnergyAccuracyAnalysisInfo) {
        epcEnergyAccuracyAnalysisInfo.setDelFlag("1"); //筛选未删除项
        return R.ok(epcEnergyAccuracyAnalysisInfoService.page(page, Wrappers.query(epcEnergyAccuracyAnalysisInfo)));
    }


    /**
     * 根据id获取企业能耗准确性分析详细信息表
     */
    @GetMapping("/getById")
    @SysLog("根据id获取企业能耗准确性分析详细信息表")
    @ApiOperation(value = "根据id获取企业能耗准确性分析详细信息表", notes = "根据id获取企业能耗准确性分析详细信息表")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Integer id) {
        return R.ok(epcEnergyAccuracyAnalysisInfoService.getById(id));
    }

    /**
     * 保存企业能耗准确性分析详细信息表
     */
    @PostMapping("save")
    @SysLog("保存企业能耗准确性分析详细信息表")
    @ApiOperation(value = "保存企业能耗准确性分析详细信息表", notes = "保存企业能耗准确性分析详细信息表")
    @ApiImplicitParam(name = "EpcEnergyAccuracyAnalysisInfo", value = "企业能耗准确性分析详细信息表对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody EpcEnergyAccuracyAnalysisInfo epcEnergyAccuracyAnalysisInfo) {
        ScUser u = SecurityUtils.getUser();
        epcEnergyAccuracyAnalysisInfo.setUserid(u.getId());
        epcEnergyAccuracyAnalysisInfo.setDeptId(u.getDeptId());
        epcEnergyAccuracyAnalysisInfo.setUpdateTime(new Date());
        epcEnergyAccuracyAnalysisInfo.setDelFlag("1");
        if("0".equals(epcEnergyAccuracyAnalysisInfo.getChangeRatio())){

            if(epcEnergyAccuracyAnalysisInfo.getLimits()==null || "".equals(epcEnergyAccuracyAnalysisInfo.getLimits())){
                if (Float.valueOf(epcEnergyAccuracyAnalysisInfo.getValue())>0) {
                    epcEnergyAccuracyAnalysisInfo.setChangeRatio("+∞");
                }else if (Float.valueOf(epcEnergyAccuracyAnalysisInfo.getValue())<0) {
                    epcEnergyAccuracyAnalysisInfo.setChangeRatio("-∞");
                }else{
                    epcEnergyAccuracyAnalysisInfo.setChangeRatio("");
                }
            }else{
                if (epcEnergyAccuracyAnalysisInfo.getValue()==null || "".equals(epcEnergyAccuracyAnalysisInfo.getValue())) {
                    epcEnergyAccuracyAnalysisInfo.setChangeRatio("");
                }
            }
        }
        return R.ok(epcEnergyAccuracyAnalysisInfoService.save(epcEnergyAccuracyAnalysisInfo));
    }

}
