package com.tzy.controller.group;

import com.tzy.annotation.ApiLimitRule;
import com.tzy.annotation.ApiLog;
import com.tzy.annotation.RequireRoles;
import com.tzy.app.dto.user.PrizeQuery;
import com.tzy.app.dto.user.SendPrizeParam;
import com.tzy.common.config.handle.ApiVersion;
import com.tzy.common.constant.MsgConstants;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.dto.UserInfo;
import com.tzy.common.enums.BusinessType;
import com.tzy.common.utils.UserType;
import com.tzy.common.utils.UserUtils;
import com.tzy.framework.web.domain.LimitRule;
import com.tzy.sportcard.api.domain.GroupInfo;
import com.tzy.sportcard.api.service.GroupApiService;
import com.tzy.sportcard.group.service.impl.GroupActService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author by po'yi
 * @Classname GroupActController
 * @Description
 * @Date 2024/6/28 13:24
 */
@Api(value = "拼团活动接口")
@RestController
@RequestMapping("/api/{version}/group/act")
@Slf4j
public class GroupActController {
	@Resource
	private GroupActService groupActService;
	@Resource
	private GroupApiService groupService;

	@ApiLog(title = "拼团商家赠礼活动", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
	@PostMapping(value = "/gifts")
	@ApiVersion(2.0)
	@ApiLimitRule(seconds = 1, limitCount = 2, type = LimitRule.USER)
	//@RequireRoles(value = {UserType.USER_ROLE_ADMIN, UserType.USER_ROLE_SHIPPING})
	public OutDTO groupMerchantGifts(@RequestBody InDto inDto) {
		Integer infoId = inDto.getIntegerParam("groupInfoId");
		if (infoId == null) {
			return OutDTO.error500(MsgConstants.PARAM_EMPTY);
		}
		return OutDTO.ok().put("giftActData", groupActService.getMerchantActGift(infoId.longValue()));
	}

	@ApiLog(title = "拼团赠礼用户", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
	@PostMapping(value = "/gift/user")
	@ApiVersion(2.0)
	@ApiLimitRule(seconds = 1, limitCount = 2, type = LimitRule.USER)
	@RequireRoles(value = {UserType.USER_ROLE_SHIPPING})
	public OutDTO groupMerchantGiftUsers(@RequestBody InDto inDto) {
		PrizeQuery query = inDto.buildParam(new PrizeQuery());
		if (query.getRecordId() == null || query.getGroupInfoId() == null) {
			return OutDTO.error500(MsgConstants.PARAM_EMPTY);
		}
		return OutDTO.ok().put("sendGiftCount",groupActService.getGroupSendGiftCount(query))
				.put("groupUsers",groupActService.getGroupMerchantGiftUsers(query));
	}

	@ApiLog(title = "商家赠礼", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
	@PostMapping(value = "/gift/send")
	@ApiVersion(2.0)
	@ApiLimitRule(seconds = 1, limitCount = 1, type = LimitRule.USER)
	@RequireRoles(value = {UserType.USER_ROLE_SHIPPING})
	public OutDTO sendGroupMerchantGift(@RequestBody InDto inDto) {
		UserInfo user = UserUtils.getSimpleUserInfo(true);
		SendPrizeParam param = inDto.buildParam(new SendPrizeParam());
		if (param.getRecordId() == null || param.getGroupInfoId() == null || StringUtils.isEmpty(param.getUserIds())) {
			return OutDTO.error500(MsgConstants.PARAM_EMPTY);
		}
		GroupInfo group = groupService.getGroupInfoById(param.getGroupInfoId());
		if (group == null || group.getMerchantId() != user.getMerchantId().intValue()) {
			return OutDTO.error500(MsgConstants.PARAM_EMPTY);
		}
		groupActService.sendGroupMerchantGift(param);
		return OutDTO.ok();
	}

	@ApiLog(title = "用户确认收货", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
	@PostMapping(value = "/gift/received")
	@ApiVersion(2.0)
	@ApiLimitRule(seconds = 1, limitCount = 1, type = LimitRule.USER)
	public OutDTO confirmReceived(@RequestBody InDto inDto) {
		String ids = inDto.getString("ids");
		Integer userId = inDto.getUserId();
		if (StringUtils.isEmpty(ids) || userId == null) {
			return OutDTO.error500(MsgConstants.PARAM_EMPTY);
		}
		boolean received = inDto.getBooleanParam("received");
		List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
		groupActService.confirmReceived(userId, idList,received);
		return OutDTO.ok();
	}

	@ApiLog(title = "编辑商家赠礼活动信息", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
	@PostMapping(value = "/edit/name")
	@ApiVersion(2.0)
	@ApiLimitRule(seconds = 1, limitCount = 2, type = LimitRule.USER)
	@RequireRoles(value = {UserType.USER_ROLE_ADMIN})
	public OutDTO editActDesc(@RequestBody InDto inDto) {
		Integer id = inDto.getIntegerParam("id");
		String actName = inDto.getString("actName");
		if (id == null || StringUtils.isEmpty(actName)) {
			return OutDTO.error500(MsgConstants.PARAM_EMPTY);
		}
		groupActService.editActDesc(id, actName);
		return OutDTO.ok();
	}



}
