/*
 *      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 cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.utils.BeanPropertyUtil;
import org.springblade.common.utils.SpringContextHolder;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.gateway.active_gateway.constant.ActiveProductConstant;
import org.springblade.sing.active.entity.ActiveHelpRecord;
import org.springblade.sing.active.entity.ActiveProductRecord;
import org.springblade.sing.active.entity.ActiveRecord;
import org.springblade.sing.active.mapper.ActiveHelpMapper;
import org.springblade.sing.active.mapper.ActiveProductRecordMapper;
import org.springblade.sing.active.service.IActiveHelpRecordService;
import org.springblade.sing.active.service.IActiveHelpService;
import org.springblade.sing.active.service.IActiveProductRecordService;
import org.springblade.sing.active.vo.ActiveRecordVO;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;
import java.util.Objects;

/**
 * 活动记录包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-11-09
 */
public class ActiveRecordWrapper extends BaseEntityWrapper<ActiveRecord, ActiveRecordVO> {

	private static final IActiveProductRecordService activeProductRecordService = SpringContextHolder.getBean(IActiveProductRecordService.class);

	private static final IActiveHelpRecordService activeHelpRecordService = SpringContextHolder.getBean(IActiveHelpRecordService.class);

	private static final ActiveHelpMapper activeHelpMapper = SpringContextHolder.getBean(ActiveHelpMapper.class);

	private static final ActiveProductRecordMapper activeProductRecordMapper = SpringContextHolder.getBean(ActiveProductRecordMapper.class);

	private static final StringRedisTemplate redisTemplate = SpringContextHolder.getBean(StringRedisTemplate.class);

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

	@Override
	public ActiveRecordVO entityVO(ActiveRecord activeRecord) {
		ActiveRecordVO activeRecordVO = Objects.requireNonNull(BeanUtil.copy(activeRecord, ActiveRecordVO.class));
		//查询活动票数
		Map<String, Object> map = activeProductRecordService.getMap(Wrappers.<ActiveProductRecord>query().select(
				new StringBuilder("ifnull(sum(")
					.append(BeanPropertyUtil.getFieldNameToUnder(ActiveHelpRecord::getVoteCount))
					.append("),0) as count")
					.toString())
			.lambda()
			.eq(ActiveProductRecord::getActiveId, activeRecordVO.getId()));
		activeRecordVO.setActiveVoteCount(MapUtil.getLong(map, "count"));
		//查询投票的用户数
		activeRecordVO.setUserCount(
			Long.valueOf(
				activeHelpRecordService.count(
					Wrappers.<ActiveHelpRecord>lambdaQuery()
						.eq(ActiveHelpRecord::getActiveId,activeRecordVO.getId()))));


		String num = redisTemplate.opsForValue().get(ActiveProductConstant.ACTIVE_VIEW_NUM.concat(":").concat(activeRecordVO.getId().toString()));

		//查询曝光量
		if (num != null){
			activeRecordVO.setViewCount(Long.valueOf(num));
		}

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

		//查询转发数
		Long indexMax = activeHelpMapper.selectIndexMax(activeRecord.getId());
		activeRecordVO.setTransmitCount(indexMax);

		//查询作品数
		Long productNum = activeProductRecordMapper.selectProductByActiveId(activeRecordVO.getId());
		activeRecordVO.setProductNum(productNum);

		return activeRecordVO;
	}

}
