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.EnergyCheck;
import com.nantian.cloud.jljc.service.EnergyCheckService;
import com.nantian.cloud.jljc.service.EnergyCheckService;
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("/energyCheck")
@Api(value = "energyCheck",tags ="节能考核")
public class EnergyCheckController {

	private final EnergyCheckService energyCheckService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param energyCheck 节能考核
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取节能考核")
	@ApiOperation(value = "分页获取节能考核", notes = "分页获取节能考核")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "energyCheck", value = "节能考核", required = false, dataType = "json", paramType = "path")
	})
	public R getEnergyCheckPage(Page page, EnergyCheck energyCheck) {
		//return R.ok(energyCheckService.page(page, Wrappers.query(energyCheck)));
		energyCheck.setDelFlag("1"); //筛选未删除项
		return R.ok(energyCheckService.getPageScope(page, Wrappers.query(energyCheck)));
	}

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



	/**
	 * 保存
	 *
	 * @param energyCheck
	 * @return R
	 */
	@PostMapping
	@SysLog("新增节能考核信息")
	@ApiOperation(value = "新增节能考核信息", notes = "新增节能考核信息")
	@ApiImplicitParam(name = "EnergyCheck", value = "节能考核信息对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody EnergyCheck energyCheck) {
		ScUser u= SecurityUtils.getUser();
		energyCheck.setUserid(u.getId());
		energyCheck.setDeptId(u.getDeptId());
		energyCheck.setUpdateTime(new Date());
		energyCheck.setDelFlag("1");
		return R.ok(energyCheckService.save(energyCheck));
	}

	/**
	 * 修改
	 *
	 * @param energyCheck
	 * @return R
	 */
	@PutMapping
	@SysLog("修改节能考核信息")
	@ApiOperation(value = "修改节能考核信息", notes = "修改节能考核信息")
	@ApiImplicitParam(name = "EnergyCheck", value = "能源节能考核对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody EnergyCheck energyCheck) {
		ScUser u= SecurityUtils.getUser();
		energyCheck.setUserid(u.getId());
		energyCheck.setUpdateTime(new Date());
		return R.ok(energyCheckService.updateById(energyCheck));
	}

	/**
	 * 删除
	 *
	 * @param energyCheckId
	 * @return R
	 */
	@DeleteMapping("/{energyCheckId}")
	@SysLog("删除节能考核信息")
	@ApiOperation(value = "删除节能考核信息", notes = "删除节能考核信息")
	@ApiImplicitParam(name = "energyCheckId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer energyCheckId) {
		EnergyCheck energyCheck = energyCheckService.getById(energyCheckId);
		ScUser u= SecurityUtils.getUser();
		energyCheck.setUserid(u.getId());
		energyCheck.setUpdateTime(new Date());
		energyCheck.setDelFlag("4");
		return R.ok(energyCheckService.updateById(energyCheck));
	}

}
