package org.springblade.gateway.active_gateway.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.common.utils.IPUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.active_gateway.service.AppActiveProductService;
import org.springblade.gateway.active_gateway.util.ActiveProductUtil;
import org.springblade.sing.active.entity.ActiveHelpRecord;
import org.springblade.sing.active.entity.ActiveProductRecord;
import org.springblade.sing.active.entity.ActiveRecord;
import org.springblade.sing.active.service.IActiveProductRecordService;
import org.springblade.sing.active.service.IActiveRecordService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 9:52 2021/11/9
 * @Modified By:
 */
@RestController
@AllArgsConstructor
@RequestMapping("app/active/product")
@Api(value = "助力", tags = "助力接口")
@Slf4j
public class AppActiveProductController extends BladeController {
	private final AppActiveProductService activeService;

	private final IActiveProductRecordService activeProductRecordService;

	private final IActiveRecordService activeRecordService;

	/**
	 * 初始化作品(预热)
	 */
	@PostConstruct
	public void init() {
		//查询所有的活动列表
		for (ActiveRecord activeRecord : activeRecordService.list(
			Wrappers.<ActiveRecord>lambdaQuery().select(ActiveRecord::getId))) {
			//查询该活动的所有作品
			List<ActiveProductRecord> activeProductRecordList = activeProductRecordService
				.list(Wrappers.<ActiveProductRecord>lambdaQuery()
					.select(ActiveProductRecord::getId, ActiveProductRecord::getVoteCount)
					.eq(ActiveProductRecord::getActiveId, activeRecord.getId())
					.eq(ActiveProductRecord::getStatus, 1));
			if (ObjectUtils.isNotEmpty(activeProductRecordList) && activeProductRecordList.size() != 0) {
				Map<Object, Double> map = activeProductRecordList.stream().collect(Collectors.toMap(ele -> ele.getId(), ele -> Double.valueOf(ele.getVoteCount())));
				ActiveProductUtil.addActiveCache(activeRecord.getId().toString(), map);
			}
		}
	}

	/**
	 * 作品助力
	 */
	@ApiLog("作品助力")
//	@PostMapping("/productHelp")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "作品助力", notes = "传入activeHelpRecord")
	public R<String> productHelp(@Valid ActiveHelpRecord activeHelpRecord, HttpServletRequest request) {
		try {
			//获取用户IP地址
			activeHelpRecord.setIp(IPUtils.getRealIp(request));
			activeService.activeHelp(activeHelpRecord);
			return R.success("助力成功!");
		} catch (Exception e) {
			log.error(e.getMessage());
			return R.fail(e.getMessage());
		}
	}
}
