/*
 *      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.user.wrapper;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
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.entity.Member;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.ldt.user.vo.MemberVO;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-09-23
 */
@AllArgsConstructor
public class MemberWrapper extends BaseEntityWrapper<Member, MemberVO> {
	private final IShopService shopService;

	private final IMallService mallService;

	private final ILoginUserService loginUserService;

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

	@Override
	public MemberVO entityVO(Member member) {
		MemberVO memberVO = Objects.requireNonNull(BeanUtil.copy(member, MemberVO.class));

		//User createUser = UserCache.getUser(member.getCreateUser());
		//User updateUser = UserCache.getUser(member.getUpdateUser());
		//memberVO.setCreateUserName(createUser.getName());
		//memberVO.setUpdateUserName(updateUser.getName());
		getMemberInfo(memberVO);
		return memberVO;
	}

	/**
	 * 获取会员详细信息
	 *
	 * @return
	 */
	public MemberVO getMemberInfo(MemberVO memberVO) {
		//查询用户名
		if (ObjectUtils.isNotEmpty(memberVO.getUserId())) {
			LoginUser loginUser = loginUserService.getById(memberVO.getUserId());
			if (loginUser != null) {
				memberVO.setUserName(ObjectUtils.isNotEmpty(loginUser.getNickName()) ? loginUser.getNickName() : memberVO.getUserId().toString());
			}
		}
		//查询商店信息
		if (ObjectUtils.isNotEmpty(memberVO.getShopId())) {
			Shop shop = shopService.getById(memberVO.getShopId());
			if (shop != null) {
				memberVO.setShopName(ObjectUtils.isNotEmpty(shop.getName()) ? shop.getName() : memberVO.getShopId().toString());
			}
		}
		//查询商场信息
		if (ObjectUtils.isNotEmpty(memberVO.getMallId())) {
			Mall mall = mallService.getById(memberVO.getMallId());
			if (mall != null) {
				memberVO.setMallName(ObjectUtils.isNotEmpty(mall.getMallName()) ? mall.getMallName() : memberVO.getMallId().toString());
			}
		}
		return memberVO;
	}

}
