/*
 *      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.modules.ldt.activityjoinrecord.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.common.constant.SystemConstant;
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.modules.ldt.activity.entity.Activity;
import org.springblade.modules.ldt.activity.service.IActivityService;
import org.springblade.modules.ldt.mall.entity.Mall;
import org.springblade.modules.ldt.mall.service.IMallService;
import org.springblade.modules.ldt.shop.entity.Shop;
import org.springblade.modules.ldt.shop.service.IShopService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.ldt.activityjoinrecord.entity.ActivityJoinRecord;
import org.springblade.modules.ldt.activityjoinrecord.vo.ActivityJoinRecordVO;
import org.springblade.modules.ldt.activityjoinrecord.service.IActivityJoinRecordService;
import org.springblade.core.boot.ctrl.BladeController;

import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-07-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-ldt/activityjoinrecord")
@Api(value = "", tags = "接口")
public class ActivityJoinRecordController extends BladeController {

	private final IActivityJoinRecordService activityJoinRecordService;
	private final IActivityService activityService;
	private final IMallService mallService;
	private final IShopService shopService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activityJoinRecord")
	public R<ActivityJoinRecord> detail(ActivityJoinRecord activityJoinRecord) {
		ActivityJoinRecord detail = activityJoinRecordService.getOne(Condition.getQueryWrapper(activityJoinRecord));
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activityJoinRecord")
	public R<IPage<ActivityJoinRecord>> list(ActivityJoinRecord activityJoinRecord, Query query) {
		IPage<ActivityJoinRecord> pages = activityJoinRecordService.page(Condition.getPage(query), Condition.getQueryWrapper(activityJoinRecord));
		return R.data(pages);
	}

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

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入activityJoinRecord")
	public R save(@Valid @RequestBody ActivityJoinRecord activityJoinRecord) {
		activityJoinRecord.getActivityId();
		return R.status(activityJoinRecordService.save(activityJoinRecord));
	}

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

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入activityJoinRecord")
	public R submit(@Valid @RequestBody ActivityJoinRecord activityJoinRecord) {
		Assert.notNull(activityJoinRecord.getJoinType(),"joinType不能为空");
		Assert.notNull(activityJoinRecord.getJoinId(),"参与者不能为空");
		Assert.notNull(activityJoinRecord.getActivityId(),"活动不能为空");
		Activity activity = activityService.getById(activityJoinRecord.getActivityId());
		Assert.notNull(activity,"活动不存在");

		//设置活动初始折扣
		activityJoinRecord.setInitSupplyPrice(activity.getSupplyPrice());
		//设置参与者追加的折扣
		activityJoinRecord.setAppendSupplyPrice(activityJoinRecord.getAppendSupplyPrice() != null ?
				activityJoinRecord.getAppendSupplyPrice():
				BigDecimal.valueOf(100));
		//设置最终折扣
		activityJoinRecord.setFinalSupplyPrice(
			activityJoinRecord.getInitSupplyPrice().multiply(activityJoinRecord.getAppendSupplyPrice()).divide(BigDecimal.valueOf(100))
		);
		//设置参与者的logo图
		if(activityJoinRecord.getJoinType() == SystemConstant.MALLORSHOP.MALL.getValue()){//商场
			Mall mall = mallService.getById(activityJoinRecord.getJoinId());
			Assert.notNull(mall,"商场不存在");
			activityJoinRecord.setJoinPic(mall.getPic());
			activityJoinRecord.setJoinName(mall.getName());
		}else if(activityJoinRecord.getJoinType() == SystemConstant.MALLORSHOP.SHOP.getValue()){
			Shop shop = shopService.getById(activityJoinRecord.getJoinId());
			Assert.notNull(shop,"商店不存在");
			activityJoinRecord.setJoinPic(shop.getCover());
			activityJoinRecord.setJoinName(shop.getName());
		}else{
			Assert.isTrue(false,"joinType错误");
		}


		return R.status(activityJoinRecordService.saveOrUpdate(activityJoinRecord));
	}


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

	/**
	 * 批量更改
	 * 小程序使用
	 */
	@PostMapping("/updateBatch")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "批量更改", notes = "传入ids")
	public R updateBatch(@RequestBody List<ActivityJoinRecord> activityJoinRecordList) {
		return R.status(activityJoinRecordService.updateBatchById(activityJoinRecordList));
	}
	/**
	 * 退出活动
	 * 小程序使用
	 */
	@PostMapping("/exitBatch")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "退出活动", notes = "传入ids")
	public R exitBatch(@RequestBody List<ActivityJoinRecord> activityJoinRecordList) {
		Assert.notNull(activityJoinRecordList, "缺少必要参数");
		List<Long> ids = new ArrayList<>();
		for (ActivityJoinRecord item : activityJoinRecordList) {
			Assert.notNull(item.getActivityId(), "活动id不能为空");
			Assert.notNull(item.getJoinId(), "joinId不能为空");
			Assert.notNull(item.getJoinType(), "joinType不能为空");
			Long id = activityJoinRecordService.getOne(Condition.getQueryWrapper(new ActivityJoinRecord()).lambda()
				.eq(ActivityJoinRecord::getActivityId, item.getActivityId())
				.eq(ActivityJoinRecord::getJoinId, item.getJoinId()).eq(ActivityJoinRecord::getJoinType, item.getJoinType())).getId();
			ids.add(id);
		}
		return R.status(activityJoinRecordService.removeByIds(ids));
	}


}
