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.CheckOnSite;
import com.nantian.cloud.jly.service.CheckOnSiteService;
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
 */
@RestController
@AllArgsConstructor
@Api(value = "checkOnSite",tags = "现场审核")
@RequestMapping("/checkOnSite")
public class CheckOnSiteController {
	private final CheckOnSiteService checkOnSiteService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param checkOnSite 请假审批单
	 * @return
	 */
	@SysLog("现场审核-保存/修改")
	@ApiOperation(value = "查询现场审核",notes = "条件查询现场审核")
	@ApiImplicitParams({
		@ApiImplicitParam(name ="CheckOnSite",value = "现场审核",required = false,dataType = "json",paramType = "path"),
		@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path")
	})
	@GetMapping("/page")
	public R getcheckOnSitePage(Page page, CheckOnSite checkOnSite) {
		checkOnSite.setDelFlag("1"); //查询未删除数据
		return R.ok(checkOnSiteService.page(page, Wrappers.query(checkOnSite)));
	}


	/**
	 * 信息
	 *
	 * @param checkOnSiteId
	 * @return R
	 */
	@SysLog("现场审核-查询")
	@ApiOperation(value = "通过id查询现场审核",notes = "通过id查询现场审核")
	@ApiImplicitParam(name ="checkOnSiteId",value = "现场审核id",required = true,dataType = "int",paramType = "path")
	@GetMapping("/{checkOnSiteId}")
	public R getById(@PathVariable("checkOnSiteId") Integer checkOnSiteId) {
		return R.ok(checkOnSiteService.getById(checkOnSiteId));
	}

	/**
	 * 保存
	 *
	 * @param checkOnSite
	 * @return R
	 */
	@SysLog("现场审核-保存")
	@ApiOperation(value = "保存现场审核信息",notes = "新增现场审核")
	@ApiImplicitParam(name ="CheckItems",value = "现场审核",required = true,dataType = "json",paramType = "path")
	@PostMapping
	public R save(@RequestBody CheckOnSite checkOnSite) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkOnSite.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			checkOnSite.setDeptId(u.getDeptId());
		}
		checkOnSite.setDelFlag("1");
		checkOnSite.setUpdateTime(new Date());
		return R.ok(checkOnSiteService.save(checkOnSite));
	}

	/**
	 * 修改
	 *
	 * @param checkOnSite
	 * @return R
	 */
	@SysLog("现场审核-修改")
	@ApiOperation(value = "修改现场审核",notes = "修改现场审核")
	@ApiImplicitParam(name ="CheckItems",value = "现场审核",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody CheckOnSite checkOnSite) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkOnSite.setUserid(u.getId());
		}
		checkOnSite.setUpdateTime(new Date());
		return R.ok(checkOnSiteService.updateById(checkOnSite));
	}

	/**
	 * 删除
	 *
	 * @param checkOnSiteId
	 * @return R
	 */
	@SysLog("现场审核-删除")
	@ApiOperation(value = "删除现场审核信息",notes = "删除现场审核信息")
	@ApiImplicitParam(name ="checkOnSiteId",value = "现场审核",required = true,dataType = "int",paramType = "path")
	@DeleteMapping("/{checkOnSiteId}")
	public R removeById(@PathVariable("checkOnSiteId") Long checkOnSiteId) {
		CheckOnSite checkOnSite = checkOnSiteService.getById(checkOnSiteId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkOnSite.setUserid(u.getId());
		}
		checkOnSite.setDelFlag("4");
		checkOnSite.setUpdateTime(new Date());
		return R.ok(checkOnSiteService.updateById(checkOnSite));
	}

}
