/*
 *      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.bills.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.bills.entity.BalanceBills;
import org.springblade.ldt.bills.vo.BalanceBillsVO;
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 BalanceBillsWrapper extends BaseEntityWrapper<BalanceBills, BalanceBillsVO>  {
	private final ILoginUserService loginUserService;

	private final IShopService shopService;

	private final IMallService mallService;

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

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

	@Override
	public BalanceBillsVO entityVO(BalanceBills balanceBills) {
		BalanceBillsVO balanceBillsVO = Objects.requireNonNull(BeanUtil.copy(balanceBills, BalanceBillsVO.class));

		//查询支付方(用户、商家、代理) 和 收款方(商家、平台)
		//用户支付->商家
		if(StringUtils.equals(OrderType.PAY_CONSUMER.name(), balanceBillsVO.getType())){
			LoginUser loginUser = (LoginUser) tenantIgnoreUtil.getById(loginUserService,balanceBillsVO.getPayId());
			balanceBillsVO.setPayerName(loginUser!=null?loginUser.getNickName(): balanceBillsVO.getPayId().toString());

			Shop shop = (Shop) tenantIgnoreUtil.getById(shopService,balanceBillsVO.getReceiveId());
			balanceBillsVO.setReceiverName(shop!=null?shop.getName(): balanceBillsVO.getReceiveId().toString());
		}else if(StringUtils.equals(OrderType.SHOP_SEND.name(), balanceBillsVO.getType())){
			Shop shop = (Shop) tenantIgnoreUtil.getById(shopService,balanceBillsVO.getPayId());
			balanceBillsVO.setPayerName(shop!=null?shop.getName(): balanceBillsVO.getPayId().toString());
			balanceBillsVO.setReceiverName("[平台]");
		}else if(StringUtils.equals(OrderType.MALL_SEND.name(), balanceBillsVO.getType())){
			Mall mall = (Mall) tenantIgnoreUtil.getById(mallService,balanceBillsVO.getPayId());
			balanceBillsVO.setPayerName(mall!=null?mall.getMallName(): balanceBillsVO.getPayId().toString());
			balanceBillsVO.setReceiverName("[平台]");
		}else{
			balanceBillsVO.setPayerName(ObjectUtils.isNotEmpty(balanceBillsVO.getPayId())?balanceBillsVO.getPayId().toString():null);
			balanceBillsVO.setReceiverName(ObjectUtils.isNotEmpty(balanceBillsVO.getReceiveId())?balanceBillsVO.getReceiveId().toString():null);
		}

		//User createUser = UserCache.getUser(balanceBills.getCreateUser());
		//User updateUser = UserCache.getUser(balanceBills.getUpdateUser());
		//balanceBillsVO.setCreateUserName(createUser.getName());
		//balanceBillsVO.setUpdateUserName(updateUser.getName());

		return balanceBillsVO;
	}

}
