/*
 *      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.PointBills;
import org.springblade.ldt.bills.vo.PointBillsVO;
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 PointBillsWrapper extends BaseEntityWrapper<PointBills, PointBillsVO>  {
	private final ILoginUserService loginUserService;

	private final IShopService shopService;

	private final IMallService mallService;

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

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

	@Override
	public PointBillsVO entityVO(PointBills pointBills) {
		PointBillsVO pointBillsVO = Objects.requireNonNull(BeanUtil.copy(pointBills, PointBillsVO.class));

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

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

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

		return pointBillsVO;
	}

}
