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.CheckRegion;
import com.nantian.cloud.jly.service.CheckRegionService;
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.annotation.Inner;
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("/checkRegion")
@CrossOrigin
@Api(value = "checkRegion",tags = "审查资料范围")
public class CheckRegionController {
	private final CheckRegionService checkRegionService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param checkRegion 请假审批单
	 * @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 getcheckRegionPage(Page page, CheckRegion checkRegion) {
		checkRegion.setDelFlag("1"); //查询未删除数据
		return R.ok(checkRegionService.page(page, Wrappers.query(checkRegion).orderByAsc("check_item")));
	}


	/**
	 * 信息
	 *
	 * @param checkRegionId
	 * @return R
	 */
	@SysLog("审查计划表-查询审查资料范围")
	@ApiOperation(value = "查询审查资料范围",notes = "条件查询审查资料范围")
	@ApiImplicitParam(name ="checkRegionId",value = "审查资料范围",required = false,dataType = "int",paramType = "path")
	@GetMapping("/{checkRegionId}")
	public R getById(@PathVariable("checkRegionId") Integer checkRegionId) {
		return R.ok(checkRegionService.getById(checkRegionId));
	}

	/**
	 * 保存
	 *
	 * @param checkRegion
	 * @return R
	 */
	@SysLog("审查计划表-保存")
	@ApiOperation(value = "保存审查资料范围",notes = "新增审查资料范围")
	@ApiImplicitParam(name ="CheckRegion",value = "审查资料范围",required = true,dataType = "json",paramType = "path")
	@PostMapping
	public R save(@RequestBody CheckRegion checkRegion) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkRegion.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			checkRegion.setDeptId(u.getDeptId());
		}
		checkRegion.setDelFlag("1");
		checkRegion.setUpdateTime(new Date());
		return R.ok(checkRegionService.save(checkRegion));
	}

	/**
	 * 修改
	 *
	 * @param checkRegion
	 * @return R
	 */
	@SysLog("审查计划表-修改")
	@ApiOperation(value = "修改审查资料范围",notes = "修改审查资料范围")
	@ApiImplicitParam(name ="CheckRegion",value = "审查资料范围",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody CheckRegion checkRegion) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkRegion.setUserid(u.getId());
        }
        checkRegion.setUpdateTime(new Date());
		return R.ok(checkRegionService.updateById(checkRegion));
	}

	/**
	 * 删除
	 *
	 * @param checkRegionId
	 * @return R
	 */
	@SysLog("审查计划表-删除")
	@ApiOperation(value = "删除审查资料范围",notes = "删除审查资料范围")
	@ApiImplicitParam(name ="checkRegionId",value = "审查资料范围",required = true,dataType = "int",paramType = "path")
	@DeleteMapping("/{checkRegionId}")
	public R removeById(@PathVariable("checkRegionId") Long checkRegionId) {
        CheckRegion checkRegion = checkRegionService.getById(checkRegionId);
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkRegion.setUserid(u.getId());
        }
        checkRegion.setDelFlag("4");
        checkRegion.setUpdateTime(new Date());
		return R.ok(checkRegionService.updateById(checkRegion));
	}

}
