package org.springblade.gateway.common_gateway.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.flow.activity.entity.ActivityEnum;
import org.springblade.flow.activity.entity.ProcessActivity;
import org.springblade.flow.activity.service.impl.AuditActivityService;
import org.springblade.flow.shop.service.impl.AuditShopService;
import org.springblade.ldt.activity.entity.Activity;
import org.springblade.ldt.activity.entity.JoinRecord;
import org.springblade.ldt.activity.service.IActivityService;
import org.springblade.ldt.activity.service.IJoinRecordService;
import org.springblade.ldt.activity.vo.ActivityVO;
import org.springblade.ldt.activity.wrapper.ActivityWrapper;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.vo.ShopVO;
import org.springblade.ldt.shop.wrapper.ShopWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cy-computer
 */
@Api(tags = "活动模块")
@RestController
@RequestMapping("/common/activity")
@AllArgsConstructor
public class AppActivityController {
	private AuditActivityService auditActivityService;
	private IActivityService activityService;
	private IJoinRecordService joinRecordService;


	@PostMapping("publish")
	public R publish(@RequestBody Activity activity) {
		ProcessActivity processActivity = ProcessActivity.builder().activity(activity).build();
		return R.status(auditActivityService.startProcess(processActivity));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入shop")
	public R<IPage<ActivityVO>> list(@RequestParam Map<String, Object> activity, Query query) {
		IPage<Activity> pages = activityService.page(Condition.getPage(query), Condition.getQueryWrapper(activity, Activity.class).lambda().orderByDesc(Activity::getCreateTime));
		return R.data(ActivityWrapper.build().pageVO(pages));
	}


	/**
	 * 获取可参加活动列表
	 */
	@GetMapping("getCanJoinActivity")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "getCanJoinActivity", notes = "传入shop")
	public R<IPage<ActivityVO>> getCanJoinActivity(Activity activity, Query query) {
		Assert.notNull(activity.getLaunchType(), "launchType不能为空");
		Assert.notNull(activity.getLaunchId(), "launchId不能为空");

		//获取已参加的活动id
		List<Long> joinListId = joinRecordService.list(Condition.getQueryWrapper(new JoinRecord()).lambda()
			.eq(JoinRecord::getJoinType, activity.getLaunchType())
			.eq(JoinRecord::getJoinId, activity.getLaunchId())).stream().map(JoinRecord::getActivityId).collect(Collectors.toList());
		//筛选已参加的活动
		IPage<Activity> pages = activityService.page(Condition.getPage(query), Condition.getQueryWrapper(new Activity()).lambda()
			.eq(Activity::getAuditStatus, ActivityEnum.PASS.name())
			.orderByDesc(Activity::getCreateTime)
			.notIn(CollUtil.isNotEmpty(joinListId),Activity::getId, joinListId));
		return R.data(ActivityWrapper.build().pageVO(pages));
	}

	@GetMapping("/getLocation")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "", notes = "传入rule")
	public R getLocationData(String latitude,String longitude) {
		String request_url = AppConstant.MAP.URL.getValue() +latitude+","+longitude + "&key=" + AppConstant.MAP.KEY.getValue();
		String s = HttpUtil.get(request_url);
		JSONObject jsonObject = JSONUtil.parseObj(s);
		return R.data(jsonObject);
	}


}
