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.ConservationDevInfo;
import com.nantian.cloud.jljc.service.ConservationDevInfoService;
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("/conservationDevInfo")
@Api(value = "conservationDevInfo",tags = "节能机电设备信息")
public class ConservationDevInfoController {
	private final ConservationDevInfoService conservationDevInfoService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param conservationDevInfo
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取节能机电设备信息")
	@ApiOperation(value = "分页获取节能机电设备信息", notes = "分页获取节能机电设备信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "choozeEnterprise", value = "节能机电设备信息（查询条件：设备名称devName、设备型号devType）", required = false, dataType = "json", paramType = "path")
	})
	public R getConservationDevInfoPage(Page page, ConservationDevInfo conservationDevInfo) {
		//return R.ok(conservationDevInfoService.page(page, Wrappers.query(conservationDevInfo)));
		conservationDevInfo.setDelFlag("1"); //筛选未删除项
		return R.ok(conservationDevInfoService.getPageScope(page, conservationDevInfo));
	}


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

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

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

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

}
