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.CheckReport;
import com.nantian.cloud.jly.service.CheckReportService;
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
@RequestMapping("/checkReport")
@Api(value = "checkReport",tags = "审批意见")
public class CheckReportController {
	private final CheckReportService checkReportService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param checkReport 请假审批单
	 * @return
	 */
	@SysLog("审批意见-分页查询")
	@ApiOperation(value = "查询审批意见",notes = "条件查询审批意见")
	@ApiImplicitParams({
		@ApiImplicitParam(name ="CheckRegion",value = "审批意见",required = false,dataType = "json",paramType = "path"),
		@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path")
	})
	@GetMapping("/page")
	public R getcheckReportPage(Page page, CheckReport checkReport) {
		checkReport.setDelFlag("1"); //查询未删除数据
		return R.ok(checkReportService.page(page, Wrappers.query(checkReport)));
	}


	/**
	 * 信息
	 *
	 * @param checkReportId
	 * @return R
	 */
	@SysLog("审批意见-查询审批意见")
	@ApiOperation(value = "查询审批意见",notes = "条件查询审批意见")
	@ApiImplicitParam(name ="checkReportId",value = "审批意见id",required = true,dataType = "int",paramType = "path")
	@GetMapping("/{checkReportId}")
	public R getById(@PathVariable("checkReportId") Integer checkReportId) {
		return R.ok(checkReportService.getById(checkReportId));
	}

	/**
	 * 保存
	 *
	 * @param checkReport
	 * @return R
	 */
	@SysLog("审批意见-保存")
	@ApiOperation(value = "保存审批意见",notes = "新增审批意见")
	@ApiImplicitParam(name ="CheckReport",value = "审批意见",required = true,dataType = "json",paramType = "path")
	@PostMapping
	public R save(@RequestBody CheckReport checkReport) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkReport.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			checkReport.setDeptId(u.getDeptId());
		}
		checkReport.setDelFlag("1");
		checkReport.setUpdateTime(new Date());
		return R.ok(checkReportService.save(checkReport));
	}

	/**
	 * 修改
	 *
	 * @param checkReport
	 * @return R
	 */
	@SysLog("审批意见-修改")
	@ApiOperation(value = "修改审批意见",notes = "修改审批意见")
	@ApiImplicitParam(name ="CheckReport",value = "审批意见",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody CheckReport checkReport) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkReport.setUserid(u.getId());
		}
		checkReport.setUpdateTime(new Date());
		return R.ok(checkReportService.updateById(checkReport));
	}

	/**
	 * 删除
	 *
	 * @param checkReportId
	 * @return R
	 */
	@SysLog("审批意见-删除")
	@ApiOperation(value = "删除审批意见",notes = "删除审批意见")
	@ApiImplicitParam(name ="checkReportId",value = "审批意见id",required = true,dataType = "int",paramType = "path")
	@DeleteMapping("/{checkReportId}")
	public R removeById(@PathVariable("checkReportId") Long checkReportId) {
		CheckReport checkReport = checkReportService.getById(checkReportId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkReport.setUserid(u.getId());
		}
		checkReport.setDelFlag("4");
		checkReport.setUpdateTime(new Date());
		return R.ok(checkReportService.updateById(checkReport));
	}

}
