package com.yonge.cooleshow.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.HolidaysFestivals;
import com.yonge.cooleshow.biz.dal.service.HolidaysFestivalsService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.log.model.AuditLogAnnotation;

@RequestMapping("holidaysFestivals")
@Api(tags = "节假日")
@RestController
public class HolidaysFestivalsController extends BaseController {

	@Autowired
	private HolidaysFestivalsService holidaysFestivalsService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation("按年查询节假日")
	@ApiImplicitParam(name = "year", value = "年份", required = false, dataType = "Integer", paramType = "path")
	@GetMapping(value = "/query/{year}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public HttpResponseResult<HolidaysFestivals> query(@PathVariable("year") Integer year) {
		
		HolidaysFestivals originHolidaysFestivals = holidaysFestivalsService.queryByYear(year);

		return succeed(originHolidaysFestivals);
	}

	@ApiOperation("新增节假日")
	@ApiImplicitParam(name = "HolidaysFestivals", value = "节假日对象", required = false, dataType = "HolidaysFestivals", paramType = "body")
	@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@AuditLogAnnotation(operateName = "新增节假日", interfaceURL = "holidaysFestivals/add")
	@PreAuthorize("@pcs.hasPermissions('holidaysFestivals/add')")
	public Object add(@RequestBody HolidaysFestivals holidaysFestivals) {
		SysUser user = sysUserFeignService.queryUserInfo();

		if (user == null || user.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		if (holidaysFestivals.getYear() == null || holidaysFestivals.getYear() <= 2020) {
			return failed("参数错误");
		}

		// 检查是否已经新增
		HolidaysFestivals originHolidaysFestivals = holidaysFestivalsService.queryByYear(holidaysFestivals.getYear());

		if (originHolidaysFestivals != null) {
			return failed("该年份已提交，请勿重复提交");
		}

		Date date = new Date();
		holidaysFestivals.setCreateTime(date).setCreateBy(user.getId()).setUpdateTime(date).setUpdateBy(user.getId());
		holidaysFestivalsService.insert(holidaysFestivals);
		return succeed();
	}

	@ApiOperation("更新节假日")
	@ApiImplicitParam(name = "HolidaysFestivals", value = "节假日对象", required = false, dataType = "HolidaysFestivals", paramType = "body")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@AuditLogAnnotation(operateName = "更新节假日", interfaceURL = "holidaysFestivals/update")
	@PreAuthorize("@pcs.hasPermissions('holidaysFestivals/update')")
	public Object update(@RequestBody HolidaysFestivals holidaysFestivals) {
		SysUser user = sysUserFeignService.queryUserInfo();

		if (user == null || user.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		Date date = new Date();
		holidaysFestivals.setUpdateTime(date).setUpdateBy(user.getId());

		holidaysFestivalsService.update(holidaysFestivals);
		return succeed();
	}
}
