package org.springblade.gateway.active_gateway.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.gateway.active_gateway.constant.ActiveProductConstant;
import org.springblade.gateway.active_gateway.service.ProductSortService;
import org.springblade.gateway.active_gateway.vo.ProductSortVO;
import org.springblade.sing.active.entity.ActiveProductRecord;
import org.springblade.sing.active.service.IActiveProductRecordService;
import org.springblade.sing.active.vo.ActiveProductRecordVO;
import org.springblade.sing.user.entity.LoginUser;
import org.springblade.sing.user.service.ILoginUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class ProductSortServiceImpl implements ProductSortService {

	@Autowired
	private IActiveProductRecordService activeProductRecordService;

	@Autowired
	private ILoginUserService loginUserService;

	@Autowired
	private BladeRedis bladeRedis;

	@Autowired
	private StringRedisTemplate redisTemplate;

	@Override
	public List<ActiveProductRecordVO> sortList(ProductSortVO productSortVO) throws IllegalAccessException {

		String key = ActiveProductConstant.PRODUCT_SORT_CACHE_KEY;

		if (productSortVO.getActiveId() != null){
			//活动曝光量
			bladeRedis.getValueOps().increment(ActiveProductConstant.ACTIVE_VIEW_NUM.concat(":").concat(productSortVO.getActiveId().toString()));

			//获取redis的productId
			Set<Object> uids = bladeRedis.getZSetOps().reverseRange(key.concat(":").concat(productSortVO.getActiveId().toString()), productSortVO.getStart() , productSortVO.getEnd());

			List<Long> list = new ArrayList<>();

			if (uids!=null){
				for (Object uid : uids) {
					//存入list
					list.add(Long.parseLong(uid.toString()));
				}
			}

			List<ActiveProductRecordVO> activeProductRecords = new ArrayList<>();

			for (Long ids : list) {
				ActiveProductRecordVO activeProductRecordVO = new ActiveProductRecordVO();

				//根据ProductId获取mysql作品信息
				ActiveProductRecord productRecord = activeProductRecordService.getById(ids);

				if (productRecord != null){
					BeanUtils.copyProperties(productRecord,activeProductRecordVO);
					//查询用户信息
					LoginUser loginUser = loginUserService.getOne(new LambdaQueryWrapper<LoginUser>().eq(LoginUser::getPhone,productRecord.getPhone()));

					if (loginUser != null){

						activeProductRecordVO.setUsername(DesensitizedUtil.mobilePhone(loginUser.getNickName()));

						activeProductRecordVO.setUserImg(loginUser.getAvatar());
					}

					activeProductRecords.add(activeProductRecordVO);
				}
			}
			return activeProductRecords;
		}else {
			throw new IllegalAccessException("非法操作");
		}
	}

	@Override
	public IPage sortListQuery(ProductSortVO productSortVO, Query query) throws IllegalAccessException {
		//将sql的页码转换为redis zSet的页码
		long current = query.getCurrent() == null ? 1 : query.getCurrent();
		long size = query.getSize()==null ? 10 : query.getSize();
		long start = (current - 1) * size;
		long end = current * size - 1;
		productSortVO.setStart(start);
		productSortVO.setEnd(end);
		List<ActiveProductRecordVO> productRecordVOS = this.sortList(productSortVO);
		//page对象
		String key = ActiveProductConstant.PRODUCT_SORT_CACHE_KEY;
		Long total = bladeRedis.getZSetOps().zCard(key.concat(":").concat(productSortVO.getActiveId().toString()));
		IPage page = new Page();
		page.setRecords(productRecordVOS);
		page.setCurrent(current);
		page.setSize(size);
		page.setTotal(total);
		return page;
	}
}
