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.DevInfo;
import com.nantian.cloud.jljc.service.DevInfoService;
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.*;
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("/devInfo")
@Api(value = "devInfo",tags = "耗能设备信息")
public class DevInfoController {
	private final DevInfoService devInfoService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param devInfo
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取耗能设备信息")
	@ApiOperation(value = "分页获取耗能设备信息", notes = "分页获取耗能设备信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "devInfo", value = "耗能设备信息对象（查询条件：设备代码devNum、设备名称devName、归类代码tradeCode、归类名称tradeName）", required = false, dataType = "json", paramType = "path")
	})
	public R getDevInfoPage(Page page, DevInfo devInfo) {
		//return R.ok(devInfoService.page(page, Wrappers.query(devInfo)));
		devInfo.setDelFlag("1"); //筛选未删除项
		return R.ok(devInfoService.getPageScope(page, devInfo));
	}


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

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

	/**
	 * 修改
	 *
	 * @param devInfo
	 * @return R
	 */
	@PutMapping
	@SysLog("修改耗能设备信息")
	@ApiOperation(value = "修改耗能设备信息", notes = "修改耗能设备信息")
	@ApiImplicitParam(name = "DevInfo", value = "耗能设备信息对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody DevInfo devInfo) {
		ScUser u= SecurityUtils.getUser();
		devInfo.setUserid(u.getId());
		devInfo.setUpdateTime(new Date());
		return R.ok(devInfoService.updateById(devInfo));
	}

	/**
	 * 删除
	 *
	 * @param devInfoId
	 * @return R
	 */
	@DeleteMapping("/{devInfoId}")
	@SysLog("删除耗能设备信息")
	@ApiOperation(value = "删除耗能设备信息", notes = "删除耗能设备信息")
	@ApiImplicitParam(name = "devInfoId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer devInfoId) {
		DevInfo devInfo = devInfoService.getById(devInfoId);
		if(devInfo==null){
			return R.failed("耗能设备信息不存在，删除失败！");
		}
		ScUser u= SecurityUtils.getUser();
		devInfo.setUserid(u.getId());
		devInfo.setUpdateTime(new Date());
		devInfo.setDelFlag("4");
		return R.ok(devInfoService.updateById(devInfo));
	}

}
