package com.cyksj.service.cps.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.EnvCommonService;
import com.cyksj.common.constant.Constant;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.UserMapper;
import com.cyksj.mapper.manage.cps.UserDistributePopularizeMapper;
import com.cyksj.model.entity.User;
import com.cyksj.model.entity.UserDistributePopularize;
import com.cyksj.model.views.UserDistributePopularizeView;
import com.cyksj.service.cps.CpsPopularizeService;
import com.cyksj.service.user.UserCommonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/*
 *项目名: netflix
 *文件名: CpsPopularizeServiceImpl
 *创建者: JavaZou
 *创建时间:2023/5/15 16:01
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CpsPopularizeServiceImpl implements CpsPopularizeService {
	private final UserDistributePopularizeMapper popularizeMapper;

	private final UserMapper userMapper;

	private final EnvCommonService envCommonService;

	private final UserCommonService userCommonService;

	@Override
	public void insert(UserDistributePopularize popularize) {
		saveOrUpdate(popularize);
	}

	@Override
	public void update(UserDistributePopularize popularize) {
		Long popularizeId = popularize.getId();
		if (popularizeId == null) throw BusinessRuntimeException.getInstance("链接不存在");
		saveOrUpdate(popularize);
	}

	@Override
	public void deleteById(long cpsUserId, Long id) {

		popularizeMapper.update(null, Wrappers.lambdaUpdate(UserDistributePopularize.class)
				.set(UserDistributePopularize::getDeleted, false)
				.eq(UserDistributePopularize::getUserId, cpsUserId)
				.eq(UserDistributePopularize::getId, id));
	}

	public void saveOrUpdate(UserDistributePopularize popularize) {
		Long popularizeId = popularize.getId();
		String name = popularize.getName();
		String remark = popularize.getRemark();
		Long userId = popularize.getUserId();
		if (name.length() > 128) {
			throw BusinessRuntimeException.getInstance("链接名称过长");
		}
		if (StrUtil.isNotBlank(remark) && remark.length() > 200) {
			throw BusinessRuntimeException.getInstance("备注过长");
		}
		if (popularizeId == null) {
			boolean isSuccess = false;
			String code = null;
			while (!isSuccess) {
				try {
					code = RandomUtil.randomString(6);
					popularize.setCode(code);
					popularizeMapper.insert(popularize);
					isSuccess = true;
				} catch (DuplicateKeyException e) {
					log.info("重复推广链接码code:{},重新获取code", code);
				}
			}
			return;
		}
		Assert.notNull(popularizeMapper.selectById(popularizeId), "链接不存在");
		popularizeMapper.update(null, Wrappers.lambdaUpdate(UserDistributePopularize.class)
				.set(UserDistributePopularize::getName, name)
				.set(UserDistributePopularize::getRemark, remark)
				.eq(UserDistributePopularize::getId, popularizeId)
				.set(UserDistributePopularize::getGoodsId, popularize.getGoodsId())
				.set(UserDistributePopularize::getSkuId, popularize.getSkuId())
				.eq(UserDistributePopularize::getUserId, userId));
	}

	@Override
	public UserDistributePopularizeView getDefaultPopularizeDetail(long cpsUserId, String start, String end) {
		return popularizeMapper.getDefaultPopularizeDetail(cpsUserId, start, end);
	}

	@Override
	public String getServiceCodeUrl(long cpsUserId) {
		User user = userMapper.selectById(cpsUserId);
		String dsCode = user.getShowId();
		if (StrUtil.isEmpty(dsCode)) {
			user.setShowId(userCommonService.getUserShowId(user.getId()));
		}
		String sharedUrl;
		if (envCommonService.isPrdEnv()) {
			sharedUrl = String.format(envCommonService.getDomain() + "serviceCode/?serviceCode=%s&dsCode=%s", Constant.EP_AC_CODE_ID, dsCode);
		} else {
			//测试环境
			sharedUrl = String.format(envCommonService.getDomain() + "test/web/serviceCode?serviceCode=%s&dsCode=%s", Constant.EP_AC_CODE_ID, dsCode);
		}
		return sharedUrl;
	}

	@Override
	public UserDistributePopularizeView getVipPopularizeDetail(Long cpsPopularizeId, String startTime, String endTime) {
		return popularizeMapper.getVipPopularizeDetail(cpsPopularizeId, startTime, endTime);

	}
}
