/*
 *      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.springblade.common.enums.AppConstant;
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.ObjectUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.ldt.bills.entity.WithdrawRec;
import org.springblade.ldt.bills.vo.WithdrawRecVO;
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 WithdrawRecWrapper extends BaseEntityWrapper<WithdrawRec, WithdrawRecVO>  {
	private final ILoginUserService loginUserService;

	private final IShopService shopService;

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

	public static WithdrawRecWrapper build(ILoginUserService loginUserService, IShopService shopService) {
		return new WithdrawRecWrapper(loginUserService,shopService);
	}

	@Override
	public WithdrawRecVO entityVO(WithdrawRec withdrawRec) {
		WithdrawRecVO withdrawRecVO = Objects.requireNonNull(BeanUtil.copy(withdrawRec, WithdrawRecVO.class));

		String name = "";
		if (withdrawRecVO.getOwnerType().equals(AppConstant.USER_TYPE.渠道代理.name())) {
			LoginUser user = (LoginUser) tenantIgnoreUtil.getById(loginUserService,withdrawRecVO.getOwnerId());
			if (ObjectUtil.isNotEmpty(user)) {
				name = user.getNickName();
			}
		} else if (withdrawRecVO.getOwnerType().equals(AppConstant.USER_TYPE.商户.name())) {
			Shop shop = (Shop) tenantIgnoreUtil.getById(shopService,withdrawRecVO.getOwnerId());
			if (ObjectUtil.isNotEmpty(shop)) {
				name = shop.getName();
			}
		}
		withdrawRecVO.setOwnerName(name);
		//User createUser = UserCache.getUser(withdrawRec.getCreateUser());
		//User updateUser = UserCache.getUser(withdrawRec.getUpdateUser());
		//withdrawRecVO.setCreateUserName(createUser.getName());
		//withdrawRecVO.setUpdateUserName(updateUser.getName());

		return withdrawRecVO;
	}

}
