package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.DictDev;
import com.nantian.cloud.jly.entity.DictEnergy;
import com.nantian.cloud.jly.service.DictDevService;
import com.nantian.cloud.jly.service.DictEnergyService;
import com.nantian.sc.cloud.common.core.util.R;
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
@Api(value = "dictEnergy",tags = "主要次级用能单位能源消耗量")
@RequestMapping("/dictEnergy")
public class DictEnergyController {
	private final DictEnergyService dictEnergyService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param dictEnergy 主要次级用能单位能源消耗量对象
	 * @return
	 */

	@ApiOperation(value = "查询主要次级用能单位能源消耗量",notes = "条件查询主要次级用能单位能源消耗量")
	@ApiImplicitParams({
			@ApiImplicitParam(name ="DictEnergy",value = "主要次级用能单位能源消耗量",required = false,dataType = "json",paramType = "path"),
			@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path")
	})
	@GetMapping("/page")
	public R getcheckDevPage(Page page, DictEnergy dictEnergy) {
		dictEnergy.setDelFlag("1"); //查询未删除数据
		return R.ok(dictEnergyService.page(page, Wrappers.query(dictEnergy)));
	}


	/**
	 * 信息
	 *
	 * @param dictEnergyId
	 * @return R
	 */
	@ApiOperation(value = "通过id查询主要次级用能单位能源消耗量",notes = "通过id条件查询主要次级用能单位能源消耗量")
	@ApiImplicitParam(name ="dictEnergyId",value = "主要次级用能单位能源消耗量ID",required = true,paramType = "path",dataType = "int")
	@GetMapping("/{dictEnergyId}")
	public R getById(@PathVariable("dictEnergyId") Integer dictEnergyId) {
		return R.ok(dictEnergyService.getById(dictEnergyId));
	}

	/**
	 * 保存
	 *
	 * @param dictEnergy
	 * @return R
	 */
	@ApiOperation(value = "新增主要次级用能单位能源消耗量",notes = "新增主要次级用能单位能源消耗量")
	@ApiImplicitParam(name ="DictEnergy",value = "主要次级用能单位能源消耗量",required = true,dataType = "json",paramType = "path")
	@PostMapping
	public R save(@RequestBody DictEnergy dictEnergy) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			dictEnergy.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			dictEnergy.setDeptId(u.getDeptId());
		}
		dictEnergy.setDelFlag("1");
		dictEnergy.setUpdateTime(new Date());
		return R.ok(dictEnergyService.save(dictEnergy));
	}

	/**
	 * 修改
	 *
	 * @param dictEnergy
	 * @return R
	 */
	@ApiOperation(value = "修改主要次级用能单位能源消耗量",notes = "修改主要次级用能单位能源消耗量")
	@ApiImplicitParam(name ="DictEnergy",value = "主要次级用能单位能源消耗量",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody DictEnergy dictEnergy) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			dictEnergy.setUserid(u.getId());
		}
		dictEnergy.setUpdateTime(new Date());
		return R.ok(dictEnergyService.updateById(dictEnergy));
	}

	/**
	 * 删除
	 *
	 * @param dictEnergyId
	 * @return R
	 */
	@ApiOperation(value = "删除主要次级用能单位能源消耗量",notes = "删除主要次级用能单位能源消耗量")
	@ApiImplicitParam(name ="dictEnergyId",value = "主要次级用能单位能源消耗量id",required = true,dataType = "int",paramType = "path")
	@DeleteMapping("/{dictEnergyId}")
	public R removeById(@PathVariable("dictEnergyId") Long dictEnergyId) {
		DictEnergy dictEnergy = dictEnergyService.getById(dictEnergyId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			dictEnergy.setUserid(u.getId());
		}
		dictEnergy.setDelFlag("4");
		dictEnergy.setUpdateTime(new Date());
		return R.ok(dictEnergyService.updateById(dictEnergy));
	}

}
