/*
 *      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.modules.guosen.hotDetail.wrapper;

import org.springblade.common.enums.SystemEnum;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.guosen.helpRecord.entity.HelpRecord;
import org.springblade.modules.guosen.helpRecord.service.IHelpRecordService;
import org.springblade.modules.guosen.helpRecord.vo.HelpRecordVO;
import org.springblade.modules.guosen.hotDetail.entity.HotDetail;
import org.springblade.modules.guosen.hotDetail.vo.HotDetailVO;
import org.springblade.modules.guosen.loginUser.entity.LoginUser;
import org.springblade.modules.guosen.loginUser.service.ILoginUserService;
import org.springblade.modules.guosen.loginUser.vo.LoginWebVO;

import java.util.Objects;
import java.util.Optional;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-07-12
 */
public class HotDetailWrapper extends BaseEntityWrapper<HotDetail, HotDetailVO>  {

	private ILoginUserService loginUserService;
	private IHelpRecordService helpRecordService;

	public HotDetailWrapper(ILoginUserService loginUserService, IHelpRecordService helpRecordService) {
		this.loginUserService = loginUserService;
		this.helpRecordService = helpRecordService;
	}

	public HotDetailWrapper(ILoginUserService loginUserService) {
		this.loginUserService = loginUserService;
	}

	public static HotDetailWrapper build(ILoginUserService loginUserService, IHelpRecordService helpRecordService) {
		return new HotDetailWrapper(loginUserService,helpRecordService);
 	}

	public static HotDetailWrapper build(ILoginUserService loginUserService) {
		return new HotDetailWrapper(loginUserService);
	}

	@Override
	public HotDetailVO entityVO(HotDetail hotDetail) {
		HotDetailVO hotDetailVO = Objects.requireNonNull(BeanUtil.copy(hotDetail, HotDetailVO.class));

		Long userId = hotDetailVO.getUserId();
		if (helpRecordService != null) {
			//小程序获取热力明细，得返回sender的信息
			if (hotDetailVO.getType().equals(SystemEnum.HOT_DETAIL.getValue())) {
				//助力,获取助力人信息
				Long helpRecordsId = hotDetailVO.getRelationId();
				HelpRecord helpRecord = helpRecordService.getById(helpRecordsId);
				userId = Optional.ofNullable(helpRecord).map(HelpRecord::getSenderId).get();
			}
		}
		LoginUser loginUser = loginUserService.getById(userId);
		LoginWebVO loginWebVO = new LoginWebVO();
		if (loginUser != null) {
			BeanUtil.copyProperties(loginUser, loginWebVO);
		}
		hotDetailVO.setLoginWebVO(loginWebVO);

		return hotDetailVO;
	}

}
