/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.party.controller;

import cn.hutool.core.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.party.entity.Activity;
import org.springblade.party.service.IActivityService;
import org.springblade.party.service.IPartyMemberService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.party.entity.ActivityEnroll;
import org.springblade.party.vo.ActivityEnrollVO;
import org.springblade.party.wrapper.ActivityEnrollWrapper;
import org.springblade.party.service.IActivityEnrollService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/activityenroll")
@Api(value = "", tags = "接口")
public class ActivityEnrollController extends BladeController {

	private IActivityEnrollService activityEnrollService;
	private IActivityService activityService;
	private IPartyMemberService memberService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activityEnroll")
	public R<ActivityEnrollVO> detail(ActivityEnroll activityEnroll) {
		ActivityEnroll detail = activityEnrollService.getOne(Condition.getQueryWrapper(activityEnroll));
		return R.data(ActivityEnrollWrapper.build(memberService, activityService).entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activityEnroll")
	public R<IPage<ActivityEnrollVO>> list(ActivityEnroll activityEnroll, Query query) {
//		Assert.notNull(activityEnroll.getActivityId(),"活动id不能为空");
		IPage<ActivityEnroll> pages = activityEnrollService.page(Condition.getPage(query), Condition.getQueryWrapper(activityEnroll));
		return R.data(ActivityEnrollWrapper.build(memberService,activityService).pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入activityEnroll")
	public R<IPage<ActivityEnrollVO>> page(ActivityEnrollVO activityEnroll, Query query) {
		IPage<ActivityEnrollVO> pages = activityEnrollService.selectActivityEnrollPage(Condition.getPage(query), activityEnroll);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入activityEnroll")
	public R save(@Valid @RequestBody ActivityEnroll activityEnroll) {
		Assert.notNull(activityEnroll.getActivityId(),"参加活动为空");
		Assert.notNull(activityEnroll.getMemberId(),"报名党员为空");

		synchronized (activityEnroll.getActivityId()){//每次执行报名某个活动只能有一个党员能进入，对活动加锁，防止报名人数超过预设的报名总人数
			Activity activity = activityService.getById(activityEnroll.getActivityId());
			Assert.notNull(activity,"找不到对应的活动");
			Assert.isTrue(activity.getRemainingCount()>0,"活动已没有名额");
			activityEnroll.setEnrollStatus(1);
			if(activityEnrollService.save(activityEnroll)){//添加报名记录成功，修改活动剩余可报名名额
				activity.setRemainingCount(activity.getRemainingCount()-1);
				if(activityService.updateById(activity)){
					return R.status(true);
				}else{
					activityEnrollService.getBaseMapper().deleteById(activityEnroll.getId());
				}
			}
		}


		return R.status(false);
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入activityEnroll")
	public R update(@Valid @RequestBody ActivityEnroll activityEnroll) {
		return R.status(activityEnrollService.updateById(activityEnroll));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入activityEnroll")
	public R submit(@Valid @RequestBody ActivityEnroll activityEnroll) {
		return R.status(activityEnrollService.saveOrUpdate(activityEnroll));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activityEnrollService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 取消报名
	 */
	@PostMapping("/cancel")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "取消报名", notes = "传入activityEnroll")
	@Transactional
	public R cancel(@Valid @RequestBody ActivityEnroll activityEnroll) {
		Assert.notNull(activityEnroll.getActivityId(),"参加活动为空");
		Assert.notNull(activityEnroll.getMemberId(),"报名党员为空");
		Activity activity = this.activityService.getById(activityEnroll.getActivityId());
		Assert.notNull(activity,"找不到对应的活动");
		activityEnroll.setEnrollStatus(2);
		if (activityEnrollService.updateById(activityEnroll)){
			activity.setRemainingCount(activity.getRemainingCount() +1);
			return R.status(this.activityService.updateById(activity));
		}
		return R.status(false);
	}


}
