package com.cyksj.web.controller.manage.spotify;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.GroupsRelationMapper;
import com.cyksj.mapper.SpotifyUserUpgradeSubmitRecordMapper;
import com.cyksj.model.entity.SpotifyUserUpgradeSubmitRecord;
import com.cyksj.model.views.SpotifyUserUpgradeOrderView;
import com.cyksj.model.views.SpotifyUserUpgradeSubmitRecordView;
import com.cyksj.service.mail.MailService;
import com.cyksj.service.order.OrderRefundService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * 项目名: yhlxj
 * 文件名: SpotifyUpgradeController
 * 创建者: JavaZou
 * 创建时间:2024/12/3 17:01
 */
@RestController
@RequestMapping("/manage/spotify/upgrade")
@RequiredArgsConstructor
public class SpotifyUpgradeController {

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final SpotifyUserUpgradeSubmitRecordMapper spotifyUserUpgradeSubmitRecordMapper;

	private final OrderRefundService orderRefundService;

	private final GroupsRelationMapper relationMapper;

	private final MailService mailService;

	/**
	 * 提交账号列表
	 */
	@GetMapping("/get/account/submit")
	public Result<SearchResult<SpotifyUserUpgradeSubmitRecordView>> getSubmitAccount() {
		SearchResult<SpotifyUserUpgradeSubmitRecordView> search = beanSearcher.search(SpotifyUserUpgradeSubmitRecordView.class, MapUtils.flatBuilder(request.getParameterMap()).orderBy(SpotifyUserUpgradeSubmitRecordView::getUpdateTime).desc().build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 设置提交状态
	 */
	@PutMapping("/set/account/submit/status")
	public Result<String> setAccountSubmitStatus(@RequestBody SpotifyUserUpgradeSubmitRecord submitRecord) {
		Long id = submitRecord.getId();
		SpotifyUserUpgradeSubmitRecord.Status status = submitRecord.getStatus();
		SpotifyUserUpgradeSubmitRecord upgradeSubmitRecord = spotifyUserUpgradeSubmitRecordMapper.selectById(id);
		if (upgradeSubmitRecord.getStatus() != SpotifyUserUpgradeSubmitRecord.Status.waiting) {
			throw BusinessRuntimeException.getInstance("状态已修改");
		}
		upgradeSubmitRecord.setStatus(status);
		spotifyUserUpgradeSubmitRecordMapper.updateById(upgradeSubmitRecord);
		//成功后 修改车票时间
		if (status == SpotifyUserUpgradeSubmitRecord.Status.success) {
			Optional.ofNullable(relationMapper.selectById(upgradeSubmitRecord.getRelationId())).ifPresent(relation -> {
				if (relation.getStartTime() == null) {
					relation.setStartTime(DateTime.now());
					relation.setExpiryTime(DateUtil.offsetMonth(relation.getStartTime(), 12));
				} else {
					relation.setExpiryTime(DateUtil.offsetMonth(DateTime.now(), 12));
				}
				relationMapper.updateById(relation);
			});
		}
		//发送spotify个人升级状态邮件
		mailService.sendSpotifyUpgradeEmail(submitRecord.getStatus(), submitRecord.getEmail());
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 订单列表
	 */
	@GetMapping("/get/orders")
	public Result<SearchResult<SpotifyUserUpgradeOrderView>> getOrders() {
		SearchResult<SpotifyUserUpgradeOrderView> search = beanSearcher.search(SpotifyUserUpgradeOrderView.class, MapUtils.flatBuilder(request.getParameterMap()).orderBy(SpotifyUserUpgradeOrderView::getId).desc().build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 订单退款
	 */
	@PostMapping("/refund/{orderId}")
	public Result<String> refund(@PathVariable Long orderId) throws Exception {
		orderRefundService.refundSpotifyUpgrade(orderId);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
