package org.springblade.party.feign;

import cn.hutool.core.lang.Assert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;
import org.springblade.core.mp.support.Condition;
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 org.springblade.party.entity.ActivityEnroll;
import org.springblade.party.vo.ActivityEnrollVO;
import org.springblade.party.wrapper.ActivityEnrollWrapper;
import org.springblade.party.service.IActivityEnrollService;

import java.util.List;
/**
 *  client
 *
 * @author lianghanqiang
 * @since 2021-04-19
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/activityenroll")

public class ActivityEnrollClient implements IActivityEnrollClient {

    private final IActivityEnrollService activityEnrollService;
	private IActivityService activityService;
	private IPartyMemberService memberService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public ActivityEnrollVO detail(ActivityEnroll activityEnroll) {
        ActivityEnroll detail = activityEnrollService.getOne(Condition.getQueryWrapper(activityEnroll));
        return ActivityEnrollWrapper.build(memberService, activityService).entityVO(detail);
        }

    /**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<ActivityEnrollVO> list(ActivityEnroll activityEnroll) {
        return ActivityEnrollWrapper.build(memberService, activityService).listVO(activityEnrollService.list(Condition.getQueryWrapper(activityEnroll)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    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);
		}
	}

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody ActivityEnroll activityEnroll) {
        return activityEnrollService.updateById(activityEnroll);
        }

    /**
     * 新增或修改
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody ActivityEnroll activityEnroll) {
        return activityEnrollService.saveOrUpdate(activityEnroll);
        }


    /**
     * 删除
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return activityEnrollService.deleteLogic(Func.toLongList(ids));
        }

	/**
	 * 取消报名
	 */
	@PostMapping("/cancel")
	@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);
	}

}
