package com.iformall.controller.market;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.*;
import com.iformall.domain.po.base.TenantEntity;
import com.iformall.enums.*;
import com.iformall.exception.MallinkException;
import com.iformall.mapper.*;
import com.iformall.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据初始化
 */
@Slf4j
@RestController
@RequestMapping("markdo")
public class MarketDoController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private WxRefundOrderService wxRefundOrderService;

	@Autowired
	private WxAppinfoService wxAppinfoService;

	@ApiOperation(value = "退券退款", notes = "{\"couponOrderId\":\"string\"}")
	@PostMapping("/refund")
	@SystemControllerLog(description = "券包-退券退款")
	public ResultData create(@RequestBody Map<String, String> paramMap) {
		logger.debug("[" + getIpAddr() + "] WxCouponOrderController::create");
		//Assert.notNull(wxRefundOrder.getName(), "角色名不能为空");
		//Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
		logger.info(paramMap.toString());
		String tenantId = paramMap.get("tenantId");
		String couponOrderIdStr = paramMap.get("couponOrderId");
		if (StringUtils.isBlank(tenantId)) {
			logger.error("tenantId不能为空: " + paramMap.toString());
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		}
		if (StringUtils.isBlank(couponOrderIdStr)) {
			logger.error("couponOrderId不能为空: " + paramMap.toString());
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		}
		Long couponOrderId = 0L;
		try {
			couponOrderId = Long.valueOf(couponOrderIdStr);
		} catch (NumberFormatException e) {
			logger.error("couponOrderId参数不正确: " + paramMap.toString());
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		}

		TenantEntity tenantEntity = new TenantEntity() {{
			setTenantId(tenantId);
		}};

		WxAppinfo appinfoQ = new WxAppinfo();
		appinfoQ.updateTenantInfo(tenantEntity);
		appinfoQ.setType(EnumAppType.B.getCode());
		PageInfo<WxAppinfo> appinfoPageInfo = wxAppinfoService.listAsPage(appinfoQ, 1, 1);
		if (appinfoPageInfo.getList().size() > 0) {
			WxAppinfo appinfo = appinfoPageInfo.getList().get(0);
			if (appinfo != null) {
				try {
					wxRefundOrderService.createRefundOrder(appinfo, couponOrderId, EnumPayType.PAY_ADMIN_REFUND, null);
					return new ResultData();
				} catch (MallinkException e) {
					logger.error(e.getMessage());
					return new ResultData(e.getErrorCode(), e.getMessage());
				} catch (Exception e) {
					logger.error(e.getMessage());
					return new ResultData(ErrorCode.REFUND_ORDER_ERROR);
				}
			}
		}
		return new ResultData(ErrorCode.REFUND_ORDER_ERROR.getCode(), "AppInfo获取失败");
	}
}
