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

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.ldt.feedback.entity.Feedback;
import org.springblade.ldt.feedback.vo.FeedbackVO;
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 FeedbackWrapper extends BaseEntityWrapper<Feedback, FeedbackVO> {

	private final ILoginUserService userService;
	private final IShopService shopService;
	private final IMallService mallService;

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

	@Override
	public FeedbackVO entityVO(Feedback feedback) {
		FeedbackVO feedbackVO = Objects.requireNonNull(BeanUtil.copy(feedback, FeedbackVO.class));

		//User createUser = UserCache.getUser(feedback.getCreateUser());
		//User updateUser = UserCache.getUser(feedback.getUpdateUser());
		//feedbackVO.setCreateUserName(createUser.getName());
		//feedbackVO.setUpdateUserName(updateUser.getName());
		setFeedbackUserName(feedbackVO);
		return feedbackVO;
	}

	/**
	 * 设置用户反馈的业务名
	 *
	 * @param detail
	 */
	private void setFeedbackUserName(FeedbackVO detail) {
		String name = "";
		if ("1".equals(detail.getType())) {
			LoginUser loginUser = userService.getById(detail.getUserId());
			name = ObjectUtil.isNotEmpty(loginUser) ? loginUser.getNickName() : detail.getUserId().toString();
		}
		if ("2".equals(detail.getType())) {
			Shop shop = shopService.getById(detail.getUserId());
			name = ObjectUtil.isNotEmpty(shop) ? shop.getName() : detail.getUserId().toString();
		}
		if ("3".equals(detail.getType())) {
			Mall mall = mallService.getById(detail.getUserId());
			name = ObjectUtil.isNotEmpty(mall) ? mall.getMallName() : detail.getUserId().toString();
		}
		detail.setUserName(name);
	}

}
