/*
 *      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.constant.CacheBizConstant;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.modules.api.service.IApiService;
import org.springblade.modules.finance.service.IOrderConsignService;
import org.springblade.modules.finance.service.IOrderGrantService;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.platform.entity.UserAddress;
import org.springblade.modules.platform.entity.UserCardInfo;
import org.springblade.modules.platform.service.IUserAddressService;
import org.springblade.modules.platform.service.IUserCardInfoService;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.service.IAttachService;
import org.springblade.modules.shopping.entity.GoodsSpecs;
import org.springblade.modules.shopping.entity.SpecItems;
import org.springblade.modules.shopping.service.IConsignService;
import org.springblade.modules.shopping.service.IGoodsSpecsService;
import org.springblade.modules.shopping.service.ISpecItemsService;
import org.springblade.modules.shopping.vo.SpecItemsVO;
import org.springblade.modules.shopping.wrapper.SpecItemsWrapper;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.vo.UserAppVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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



	@Autowired
	private OssBuilder ossBuilder;

	@Autowired
	private IAttachService attachService;

	@Autowired
	private ISpecItemsService specItemsService;

	@Autowired
	private IGoodsSpecsService goodsSpecsService;

	@Autowired
	private IUserAddressService userAddressService;

	@Autowired
	private IUserCardInfoService userCardInfoService;

	@Autowired
	private IOrderService orderService;

	@Autowired
	private IOrderGrantService orderGrantService;

	@Autowired
	private IOrderConsignService orderConsignService;

	@Autowired
	private IConsignService consignService;

	@Autowired
	private BladeRedis bladeRedis;

	@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 = UserWrapper.build().entityVo(query);
		//团队人数
		Integer teamNum = userApp.selectCount(Wrappers.<UserApp>lambdaQuery().like(UserApp::getParentPath, userId)
			.ne(UserApp::getParentId,userId));
		vo.setTeamNum (teamNum);
		int count = userAddressService.count(Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUserId,userId));
		vo.setIsAddress(1);
		if (count > 0){
			vo.setIsAddress(2);
		}
		vo.setIsOauth(1);
		UserCardInfo userCardInfo = userCardInfoService.getOne(Wrappers.<UserCardInfo>lambdaQuery().eq(UserCardInfo::getUserId,userId));
		if (userCardInfo != null){
			vo.setIsOauth(2);
			vo.setIdCard(userCardInfo.getIdCard());
			vo.setRealName(userCardInfo.getTrueName());
			vo.setNegativeUrl(userCardInfo.getNegativeUrl());
			vo.setPositiveUrl(userCardInfo.getPositiveUrl());
		}
		return vo;
	}

	@Override
	public List<SpecItemsVO> specItemsList(Long goodsId) {
		List<SpecItems> specItemsList = specItemsService.list(Wrappers.<SpecItems>lambdaQuery()
			.eq(SpecItems::getGoodsId, goodsId));
		if (specItemsList.size() == 0){
			return new ArrayList<>();
		}
		List<SpecItemsVO> list = SpecItemsWrapper.build().listVO(specItemsList);
		list.forEach(specItems -> {
			specItems.setMarketPrice(0.0);
			specItems.setGoodsPrice(0.0);
			specItems.setTicketNum(0.0);
			specItems.setGoodsStock(0);
			GoodsSpecs specs = goodsSpecsService.getOne(Wrappers.<GoodsSpecs>lambdaQuery()
				.eq(GoodsSpecs::getGoodsId, goodsId)
				.eq(GoodsSpecs::getSpecIds, specItems.getSpecId().toString()));
			if (specs != null){
				specItems.setMarketPrice(specs.getMarketPrice());
				specItems.setGoodsPrice(specs.getSpecPrice());
				specItems.setTicketNum(specs.getTicketNum());
				specItems.setGoodsStock(specs.getSpecStock());
				specItems.setGoodsSpecIds(specs.getId().toString());
			}else{
				list.remove(specItems);
			}
		});
		return list;
	}

}
