/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.ldt.agent.wrapper;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.enums.OrderType;
import org.springblade.common.utils.TenantIgnoreUtil;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.ldt.agent.entity.AgentBills;
import org.springblade.ldt.agent.vo.AgentBillsVO;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-09-23
 */
@AllArgsConstructor
public class AgentBillsWrapper extends BaseEntityWrapper<AgentBills, AgentBillsVO> {

	private final ILoginUserService loginUserService;

	private final IShopService shopService;

	private final IMallService mallService;

	private final static TenantIgnoreUtil tenantIgnoreUtil = SpringUtil.getBean(TenantIgnoreUtil.class);

	public static AgentBillsWrapper build(ILoginUserService loginUserService, IShopService shopService, IMallService mallService) {
		return new AgentBillsWrapper(loginUserService, shopService, mallService);
	}

	@Override
	public AgentBillsVO entityVO(AgentBills agentBills) {
		AgentBillsVO agentBillsVO = Objects.requireNonNull(BeanUtil.copy(agentBills, AgentBillsVO.class));

		//User createUser = UserCache.getUser(agentBills.getCreateUser());
		//User updateUser = UserCache.getUser(agentBills.getUpdateUser());
		//agentBillsVO.setCreateUserName(createUser.getName());
		//agentBillsVO.setUpdateUserName(updateUser.getName());
		getPayerAndReceiver(agentBillsVO);
		return agentBillsVO;
	}

	/**
	 * 获取收款方和付款方名称
	 *
	 * @param agentBillsVO
	 */
	private AgentBillsVO getPayerAndReceiver(AgentBillsVO agentBillsVO) {
		//查询代理
		LoginUser agenterUser = loginUserService.getById(agentBillsVO.getAgenter());
		agentBillsVO.setAgenterName(agenterUser != null ? agenterUser.getNickName() :
			ObjectUtils.isNotEmpty(agentBillsVO.getAgenter()) ? agentBillsVO.getAgenter().toString() : null);
		//查询支付方(用户、商家、代理) 和 收款方(商家、平台)
		//用户支付->商家
		if (StringUtils.equals(OrderType.USER_PAY.name(), agentBillsVO.getType()) || StringUtils.equals(OrderType.WECHAT_PAY.name(), agentBillsVO.getType())) {
			LoginUser loginUser = (LoginUser) tenantIgnoreUtil.getById(loginUserService,agentBillsVO.getPayer());
			agentBillsVO.setPayerName(loginUser != null ? loginUser.getNickName() : agentBillsVO.getPayer().toString());
			Shop shop = (Shop) tenantIgnoreUtil.getById(shopService,agentBillsVO.getReceiver());
			agentBillsVO.setReceiverName(shop != null ? shop.getName() : agentBillsVO.getReceiver().toString());
		} else if (StringUtils.equals(OrderType.MALL_CHARGE.name(), agentBillsVO.getType())) {
			Mall mall = (Mall) tenantIgnoreUtil.getById(mallService,agentBillsVO.getPayer());
			agentBillsVO.setPayerName(mall != null ? mall.getMallName() : agentBillsVO.getPayer().toString());
			agentBillsVO.setReceiverName("[平台]");
		} else if (StringUtils.equals(OrderType.AGENT_CHARGE.name(), agentBillsVO.getType())) {
			LoginUser loginUser = (LoginUser) tenantIgnoreUtil.getById(loginUserService,agentBillsVO.getPayer());
			agentBillsVO.setPayerName(loginUser != null ? loginUser.getNickName() : agentBillsVO.getPayer().toString());
			agentBillsVO.setReceiverName("[平台]");
		} else {
			agentBillsVO.setPayerName(ObjectUtils.isNotEmpty(agentBillsVO.getPayer()) ? agentBillsVO.getPayer().toString() : null);
			agentBillsVO.setReceiverName(ObjectUtils.isNotEmpty(agentBillsVO.getReceiver()) ? agentBillsVO.getReceiver().toString() : null);
		}
		return agentBillsVO;
	}

}
