/*
 *      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.api.service.impl;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.cache.DictCache;
import org.springblade.common.cache.UserCache;
import org.springblade.common.enums.DictBizEnum;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.modules.api.service.IApiService;
import org.springblade.modules.api.vo.CouponQuantityVO;
import org.springblade.modules.mall.service.ICouponUserService;
import org.springblade.modules.platform.service.IUserAddressService;
import org.springblade.modules.platform.service.IUserCardInfoService;
import org.springblade.modules.platform.wrapper.UserAppWrapper;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.service.IAttachService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.UserAppVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 商品规格值表 服务实现类
 *
 * @author BladeX
 * @since 2022-02-13
 */
@Service
public class ApiServiceImpl implements IApiService {

	@Resource
	private ICouponUserService couponUserService;
	@Lazy
	@Resource
	private IUserService userService;

	@Override
	public String userCodeUrl(String code){
		try {
			String href = "https://wine.gzzzyd.com/login.html?code=" + code;
			QrConfig config = new QrConfig(300, 300);
			// 设置边距，既二维码和背景之间的边距
			config.setMargin(3);
			// 生成base64二维码到文件，也可以到流
			return QrCodeUtil.generateAsBase64(href, config, "png");
		}catch (Exception e){
			e.printStackTrace();
		}
		return "";
	}

	@Override
	public UserAppVO getUserAppVo(Long userId) {
		UserApp userApp = new UserApp();
		UserApp query = userApp.selectOne(Wrappers.<UserApp>lambdaQuery().eq(UserApp::getUserId, userId));
		if (query == null){
			return null;
		}
		UserAppVO vo = UserAppWrapper.build().entityVO(query);
		// 查询用户优惠券数量
		CouponQuantityVO quantity = couponUserService.quantity(userId);
		vo.setCouponTotal(quantity.getUnused());

		User user = userService.getById(userId);
		vo.setCode(user.getCode());
		vo.setAvatar(user.getAvatar());
		vo.setRealName(user.getRealName());
		vo.setName(user.getName());
		vo.setSexDisplayName(DictCache.getValue(DictEnum.SEX,user.getSex()));
		vo.setStateDisplayName(DictBizCache.getValue(DictBizEnum.USER_STATE, userApp.getState()));
		vo.setUsername(user.getAccount());
		vo.setBirthday(user.getBirthday());

		return vo;
	}


}
