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

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springblade.common.utils.SpringContextHolder;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.gateway.active_gateway.constant.ActiveProductConstant;
import org.springblade.sing.active.entity.ActiveProductRecord;
import org.springblade.sing.active.entity.ActiveRecord;
import org.springblade.sing.active.service.IActiveRecordService;
import org.springblade.sing.active.vo.ActiveProductRecordVO;
import java.util.Objects;

/**
 * 活动作品包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-11-10
 */
public class ActiveProductRecordWrapper extends BaseEntityWrapper<ActiveProductRecord, ActiveProductRecordVO>  {

	private BladeRedis bladeRedis;

	private IActiveRecordService activeRecordService = SpringContextHolder.getBean(IActiveRecordService.class);

	public ActiveProductRecordWrapper(BladeRedis bladeRedis) {
		this.bladeRedis = bladeRedis;
	}

	public ActiveProductRecordWrapper() {
	}

	public static ActiveProductRecordWrapper build(BladeRedis bladeRedis) {
		return new ActiveProductRecordWrapper(bladeRedis);
 	}

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

	@Override
	public ActiveProductRecordVO entityVO(ActiveProductRecord activeProductRecord) {
		ActiveProductRecordVO activeProductRecordVO = Objects.requireNonNull(BeanUtil.copy(activeProductRecord, ActiveProductRecordVO.class));

		if (bladeRedis != null) {
			//获取排名
			String key = ActiveProductConstant.PRODUCT_SORT_CACHE_KEY.concat(":").concat(activeProductRecordVO.getActiveId().toString());
			Long revrank = bladeRedis.zRevrank(key, activeProductRecordVO.getId());
			if (revrank != null){
				activeProductRecordVO.setRank(revrank + 1);
			}
		}

		//添加活动数据
		ActiveRecord activeRecord = activeRecordService.getById(activeProductRecordVO.getActiveId());
		if (ObjectUtils.isNotEmpty(activeRecord)) {
			activeProductRecordVO.setActiveName(activeRecord.getTitle());
		}


		//User createUser = UserCache.getUser(activeProductRecord.getCreateUser());
		//User updateUser = UserCache.getUser(activeProductRecord.getUpdateUser());
		//activeProductRecordVO.setCreateUserName(createUser.getName());
		//activeProductRecordVO.setUpdateUserName(updateUser.getName());

		return activeProductRecordVO;
	}

}
