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.ReportDate;
import com.nantian.cloud.jljc.service.ReportDateService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
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.List;

@RestController
@AllArgsConstructor
@RequestMapping("/reportDate")
@Api(value = "reportDate", tags = "企业报告截止日期")
@CrossOrigin
public class ReportDateController {

	private final ReportDateService reportDateService;

	/**
	 * 查询
	 *
	 *
	 * @param reportDate
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取企业报告截止日期")
	@ApiOperation(value = "分页获取企业报告截止日期", notes = "分页获取企业报告截止日期")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "reportDate", value = "企业报告截止日期对象", required = false, dataType = "json", paramType = "path")
	})
	public R getReportDate(Page page, ReportDate reportDate) {
		return R.ok(reportDateService.page(page, Wrappers.query(reportDate)));
	}

	/**
	 * 修改
	 *
	 * @param reportDate
	 * @return R
	 */
	@RequestMapping("/updates")
	@SysLog("修改企业报告截止日期")
	@ApiOperation(value = "修改企业报告截止日期", notes = "修改企业报告截止日期")
	@ApiImplicitParam(name = "reportDate", value = "企业报告截止日期对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody ReportDate reportDate) {
		//System.out.println("size is"+reportDate);
			reportDateService.updateById(reportDate);
		return R.ok(true);
	}


	/**
	 * 保存
	 *
	 * @param reportDate
	 * @return R
	 */
	@PostMapping
	@SysLog("新增企业报告截止日期")
	@ApiOperation(value = "新增企业报告截止日期", notes = "新增企业报告截止日期")
	@ApiImplicitParam(name = "reportDate", value = "企业报告截止日期对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody ReportDate reportDate) {
		ReportDate q = new ReportDate();
		q.setDelFlag("1");
		q.setYear(reportDate.getYear());
		List<ReportDate> list = reportDateService.list(Wrappers.query(q));
		if(list.size()>0){
			return R.failed(reportDate.getYear()+"年度已设置上报截止日期！");
		}
		return R.ok(reportDateService.save(reportDate));
	}


	/**
	 * 删除
	 *
	 * @param reportDateId
	 * @return R
	 */
	@DeleteMapping("/{reportDateId}")
	@SysLog("删除企业报告截止日期")
	@ApiOperation(value = "删除企业报告截止日期", notes = "删除企业报告截止日期")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer reportDateId) {
		return R.ok(reportDateService.removeById(reportDateId));
	}
}
