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.EliminateDevInfo;
import com.nantian.cloud.jljc.service.EliminateDevInfoService;
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("/eliminateDevInfo")
@Api(value = "eliminateDevInfo",tags ="淘汰设备信息")
public class EliminateDevInfoController {
	private final EliminateDevInfoService eliminateDevInfoService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param eliminateDevInfo
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取淘汰设备信息")
	@ApiOperation(value = "分页获取淘汰设备信息", notes = "分页获取淘汰设备信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "eliminateDevInfo", value = "淘汰设备信息对象（查询条件：设备分类名称devClass、" +
					"设备类型devType、名称devName、型号types）", required = false, dataType = "json", paramType = "path")
	})
	public R getEliminateDevInfoPage(Page page, EliminateDevInfo eliminateDevInfo) {
		//return R.ok(eliminateDevInfoService.page(page, Wrappers.query(eliminateDevInfo)));
		eliminateDevInfo.setDelFlag("1"); //筛选未删除项
		return R.ok(eliminateDevInfoService.getPageScope(page,eliminateDevInfo));
	}


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

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

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

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

}
