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.EnergyLevel;
import com.nantian.cloud.jljc.service.EnergyLevelService;
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
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/energyLevel")
@Api(value = "energyLevel",tags ="能耗级别")
public class EnergyLevelController {

	private final EnergyLevelService energyLevelService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param energyLevel
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取能耗级别")
	@ApiOperation(value = "分页获取能耗级别", notes = "分页获取能耗级别")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "energyLevel", value = "能耗级别对象（查询条件：能耗级别level、下限值lowerLimit、上限值upperLimit）",
					required = false, dataType = "json", paramType = "path")
	})
	public R getEnergyLevelPage(Page page, EnergyLevel energyLevel) {
		//return R.ok(energyLevelService.page(page, Wrappers.query(energyLevel)));
		energyLevel.setDelFlag("1"); //筛选未删除项
		return R.ok(energyLevelService.getPageScope(page, energyLevel));
	}


	/**
	 * 信息
	 *
	 * @param energyLevelId
	 * @return R
	 */
	@GetMapping("/{energyLevelId}")
	@SysLog("根据id获取能耗级别")
	@ApiOperation(value = "根据id获取能耗级别", notes = "根据id获取能耗级别")
	@ApiImplicitParam(name = "energyLevelId", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("energyLevelId") Integer energyLevelId) {
		return R.ok(energyLevelService.getById(energyLevelId));
	}

	/**
	 * 保存
	 *
	 * @param energyLevel
	 * @return R
	 */
	@PostMapping
	@SysLog("新增能耗级别")
	@ApiOperation(value = "新增能耗级别", notes = "新增能耗级别")
	@ApiImplicitParam(name = "EnergyLevel", value = "能耗级别对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody EnergyLevel energyLevel) {
		ScUser u= SecurityUtils.getUser();
		energyLevel.setUserid(u.getId());
		energyLevel.setDeptId(u.getDeptId());
		energyLevel.setUpdateTime(new Date());
		energyLevel.setDelFlag("1");
		return R.ok(energyLevelService.save(energyLevel));
	}

	/**
	 * 修改
	 *
	 * @param energyLevel
	 * @return R
	 */
	@PutMapping
	@SysLog("修改能耗级别")
	@ApiOperation(value = "修改能耗级别", notes = "修改能耗级别")
	@ApiImplicitParam(name = "EnergyLevel", value = "能耗级别对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody EnergyLevel energyLevel) {
		ScUser u= SecurityUtils.getUser();
		energyLevel.setUserid(u.getId());
		energyLevel.setUpdateTime(new Date());
		return R.ok(energyLevelService.updateById(energyLevel));
	}

	/**
	 * 删除
	 *
	 * @param energyLevelId
	 * @return R
	 */
	@DeleteMapping("/{energyLevelId}")
	@SysLog("删除能耗级别")
	@ApiOperation(value = "删除能耗级别", notes = "删除能耗级别")
	@ApiImplicitParam(name = "energyLevelId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer energyLevelId) {
		EnergyLevel energyLevel = energyLevelService.getById(energyLevelId);
		if(energyLevel==null){
			return R.failed("能耗级别不存在，删除失败！");
		}
		ScUser u= SecurityUtils.getUser();
		energyLevel.setUserid(u.getId());
		energyLevel.setDeptId(u.getDeptId());
		energyLevel.setUpdateTime(new Date());
		energyLevel.setDelFlag("4");
		return R.ok(energyLevelService.updateById(energyLevel));
	}

}
