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.CheckAbnormalReport;
import com.nantian.cloud.jly.service.CheckAbnormalReportService;
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;
import java.util.List;


/**
 *不符合报告记录
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@Api(value = "checkAbnormalReport",tags = "不符合报告记录")
@RequestMapping("/checkAbnormalReport")
public class CheckAbnormalReportController {
	private final CheckAbnormalReportService checkAbnormalReportService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param checkAbnormalReport 请假审批单
	 * @return
	 */
	@SysLog("查询不符合报告记录")
	@ApiOperation(value = "查询不符合报告记录",notes = "条件查询不符合报告记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="CheckAbnormalReport",value = "不符合报告记录",required = false,dataType = "json",paramType = "path"),
            @ApiImplicitParam(name ="Page",value = "不符合报告记录",required = false,dataType = "json",paramType = "path")
    })
	@GetMapping("/page")
	public R getcheckAbnormalReportPage(Page page, CheckAbnormalReport checkAbnormalReport) {
		checkAbnormalReport.setDelFlag("1"); //查询未删除数据
		return R.ok(checkAbnormalReportService.getPageScope(page, checkAbnormalReport));
	}

	/**
	 * 企业查询不符合报告
	 *
	 */
	@SysLog("企业查询不符合报告")
	@GetMapping("/getByEpc")
	public R getByEpc(CheckAbnormalReport checkAbnormalReport) {
		checkAbnormalReport.setDelFlag("1"); //查询未删除数据
		return R.ok(checkAbnormalReportService.getByEpc(checkAbnormalReport));
	}


	/**
	 * 信息
	 *
	 * @param checkAbnormalReportId
	 * @return R
	 */
	@SysLog("通过ID查询不符合报告记录")
	@ApiOperation(value = "通过ID查询不符合报告记录",notes = "通过ID条件查询不符合报告记录")
	@ApiImplicitParam(name ="checkAbnormalReportId",value = "不符合报告记录ID",required = false,dataType = "int",paramType = "path")
	@GetMapping("/{checkAbnormalReportId}")
	public R getById(@PathVariable("checkAbnormalReportId") Integer checkAbnormalReportId) {
		return R.ok(checkAbnormalReportService.getById(checkAbnormalReportId));
	}

	/**
	 * 保存
	 *
	 * @param checkAbnormalReport
	 * @return R
	 */
	@SysLog("新增不符合报告记录")
	@ApiOperation(value = "新增不符合报告记录",notes = "新增不符合报告记录")
	@ApiImplicitParam(name ="CheckAbnormalReport",value = "不符合报告记录",required = true,paramType = "path",dataType = "json")
	@PostMapping
	public R save(@RequestBody CheckAbnormalReport checkAbnormalReport) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkAbnormalReport.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			checkAbnormalReport.setDeptId(u.getDeptId());
		}
		checkAbnormalReport.setDelFlag("1");
		checkAbnormalReport.setUpdateTime(new Date());
		return R.ok(checkAbnormalReportService.save(checkAbnormalReport));
	}

	/**
	 * 批量保存
	 *
	 * @param checkAbnormalReport
	 * @return R
	 */
	@SysLog("新增不符合报告记录")
	@ApiOperation(value = "新增不符合报告记录",notes = "新增不符合报告记录")
	@ApiImplicitParam(name ="CheckAbnormalReport",value = "不符合报告记录",required = true,paramType = "path",dataType = "json")
	@PostMapping("/saveAll")
	public R saveAll(@RequestBody List<CheckAbnormalReport> checkAbnormalReport) {
		ScUser u= SecurityUtils.getUser();
		checkAbnormalReport.forEach(r->{
			if(u.getId()!=null){
				r.setUserid(u.getId());
			}
			if (u.getDeptId() != null) {
				r.setDeptId(u.getDeptId());
			}
			r.setDelFlag("1");
			r.setUpdateTime(new Date());
		});
		return R.ok(checkAbnormalReportService.saveOrUpdateBatch(checkAbnormalReport));
	}

	/**
	 * 修改
	 *
	 * @param checkAbnormalReport
	 * @return R
	 */
	@SysLog("修改不符合报告记录")
	@ApiOperation(value = "修改不符合报告记录",notes = "修改不符合报告记录")
	@ApiImplicitParam(name ="CheckAbnormalReport",value = "不符合报告记录",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody CheckAbnormalReport checkAbnormalReport) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkAbnormalReport.setUserid(u.getId());
		}
		checkAbnormalReport.setUpdateTime(new Date());
		return R.ok(checkAbnormalReportService.updateById(checkAbnormalReport));
	}

	/**
	 * 删除
	 *
	 * @param checkAbnormalReportId
	 * @return R
	 */
	@SysLog("删除不符合报告记录")
	@ApiOperation(value = "删除不符合报告记录",notes = "删除不符合报告记录")
	@ApiImplicitParam(name ="checkAbnormalReportId",value = "不符合报告记录ID",required = true,dataType = "int",paramType = "path")
	@DeleteMapping("/{checkAbnormalReportId}")
	public R removeById(@PathVariable("checkAbnormalReportId") Long checkAbnormalReportId) {
		CheckAbnormalReport checkAbnormalReport = checkAbnormalReportService.getById(checkAbnormalReportId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			checkAbnormalReport.setUserid(u.getId());
		}
		checkAbnormalReport.setDelFlag("4");
		checkAbnormalReport.setUpdateTime(new Date());
		return R.ok(checkAbnormalReportService.updateById(checkAbnormalReport));
	}

}
