/*
 *      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.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.ldt.bills.entity.PlatformBills;
import org.springblade.ldt.bills.vo.PlatformBillsVO;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.mall.service.impl.MallServiceImpl;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.shop.service.impl.ShopServiceImpl;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.ldt.user.service.impl.LoginUserServiceImpl;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-09-23
 */
@AllArgsConstructor
public class PlatformBillsWrapper extends BaseEntityWrapper<PlatformBills, PlatformBillsVO>  {
	private final ILoginUserService loginUserService;

	private final IShopService shopService;

	private final IMallService mallService;

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

	@Override
	public PlatformBillsVO entityVO(PlatformBills platformBills) {
		PlatformBillsVO platformBillsVO = Objects.requireNonNull(BeanUtil.copy(platformBills, PlatformBillsVO.class));

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

			Shop shop = shopService.getById(platformBillsVO.getReceiver());
			platformBillsVO.setReceiverName(shop!=null?shop.getName(): platformBillsVO.getReceiver().toString());
		}else if(StringUtils.equals(OrderType.MALL_RECHARGE.name(), platformBillsVO.getType())){
			Mall mall = mallService.getById(platformBillsVO.getPayer());
			platformBillsVO.setPayerName(mall!=null?mall.getMallName(): platformBillsVO.getPayer().toString());
			platformBillsVO.setReceiverName("[平台]");
		}else if(StringUtils.equals(OrderType.AGENT_CHARGE.name(), platformBillsVO.getType())){
			LoginUser loginUser = loginUserService.getById(platformBillsVO.getPayer());
			platformBillsVO.setPayerName(loginUser!=null?loginUser.getNickName(): platformBillsVO.getPayer().toString());
			platformBillsVO.setReceiverName("[平台]");
		}else{
			platformBillsVO.setPayerName(ObjectUtils.isNotEmpty(platformBillsVO.getPayer())?platformBillsVO.getPayer().toString():null);
			platformBillsVO.setReceiverName(ObjectUtils.isNotEmpty(platformBillsVO.getReceiver())?platformBillsVO.getReceiver().toString():null);
		}

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

		return platformBillsVO;
	}

}
