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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
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.SpringUtil;
import org.springblade.ldt.activity.entity.Activity;
import org.springblade.ldt.activity.vo.ActivityVO;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IAuditService;
import org.springblade.ldt.shop.service.IShopService;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-09-23
 */
public class ActivityWrapper extends BaseEntityWrapper<Activity, ActivityVO>  {

	private static IShopService shopService = SpringUtil.getBean(IShopService.class);

	private static IMallService mallService = SpringUtil.getBean(IMallService.class);

	private static IAuditService auditService = SpringUtil.getBean(IAuditService.class);

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

	public static ActivityWrapper build() {
		return new ActivityWrapper();
 	}

	@Override
	public ActivityVO entityVO(Activity activity) {
		ActivityVO activityVO = Objects.requireNonNull(BeanUtil.copy(activity, ActivityVO.class));

		if(ObjectUtils.isNotEmpty(activityVO)){
			//获取审核数据
			Audit audit = (Audit) tenantIgnoreUtil.getOne(auditService, Wrappers.<Audit>lambdaQuery().eq(Audit::getEntityId, activityVO.getLaunchId()));
			if(ObjectUtils.isNotEmpty(audit)){
				activityVO.setEntity(audit.getEntity());
				activityVO.setIdCard(audit.getIdCard());
				activityVO.setIdCardFront(audit.getIdCardFront());
				activityVO.setIdCardContrary(audit.getIdCardContrary());
				activityVO.setBusinessLicense(audit.getBusinessLicense());
			}
			//查询商户信息
			if(StringUtils.equals(activityVO.getLaunchType(), AppConstant.PLATFORM.SHOP.getName())){
				Shop shop = (Shop) tenantIgnoreUtil.getById(shopService, activityVO.getLaunchId());
				if(ObjectUtils.isNotEmpty(shop)){
					activityVO.setShopName(shop.getName());
					activityVO.setLegalPerson(shop.getPersonName());
					activityVO.setAddress(shop.getAddress());
				}
			}else if(StringUtils.equals(activityVO.getLaunchType(), AppConstant.PLATFORM.MALL.getName())){ //查询商场信息
				Mall mall = (Mall) tenantIgnoreUtil.getById(mallService, activityVO.getLaunchId());
				if(ObjectUtils.isNotEmpty(mall)){
					activityVO.setShopName(mall.getMallName());
					activityVO.setLegalPerson(mall.getPersonName());
					activityVO.setAddress(mall.getAddress());
				}
			}
		}
		//User createUser = UserCache.getUser(activity.getCreateUser());
		//User updateUser = UserCache.getUser(activity.getUpdateUser());
		//activityVO.setCreateUserName(createUser.getName());
		//activityVO.setUpdateUserName(updateUser.getName());


		return activityVO;
	}

}
