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

import org.springblade.common.cache.ParamCache;
import org.springblade.common.constant.CacheBizConstant;
import org.springblade.common.enums.IntegralSourceEnum;
import org.springblade.modules.platform.entity.UserIntegral;
import org.springblade.modules.platform.service.IUserAppService;
import org.springblade.modules.platform.vo.UserIntegralVO;
import org.springblade.modules.platform.mapper.UserScoresMapper;
import org.springblade.modules.platform.service.IUserIntegralService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.system.entity.UserApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户积分表 服务实现类
 *
 * @author BladeX
 * @since 2022-02-13
 */
@Service
public class UserIntegralServiceImpl extends ServiceImpl<UserScoresMapper, UserIntegral> implements IUserIntegralService {

	@Lazy
	@Autowired
	private IUserAppService userAppService;

	@Override
	public IPage<UserIntegralVO> selectUserScoresPage(IPage<UserIntegralVO> page, UserIntegralVO userScores) {
		return page.setRecords(baseMapper.selectUserScoresPage(page, userScores));
	}

	@Override
	public Double addIntegralRecord(Double integral, Long userId, Double userIntegral, IntegralSourceEnum integralSourceEnum, String remark) {
		UserIntegral record = new UserIntegral();
		record.setCreateTime(new Date());
		record.setIntegral(integral);
		record.setRemark(remark);
		record.setUserId(userId);
		record.setType(1);// 收入
		record.setUserIntegral(userIntegral);
		record.setLastIntegral(integral + userIntegral);
		record.setDataType(1);// 用户积分
		record.setSource(integralSourceEnum.getValue().toString());
		this.save(record);
		return record.getLastIntegral();
	}

	@Override
	public Double getInviteIntegral() {
		String valueDefault = ParamCache.getValueDefault(CacheBizConstant.INVITE_INTEGRAL, "100");
		return Double.parseDouble(valueDefault);
	}

	@Override
	public void addIntegral(Double integral, Long userId, IntegralSourceEnum integralSourceEnum, String remark) {
		UserApp userApp = userAppService.getByUserId(userId);
		Assert.notNull(userApp, "用户信息不存在");
		Double lastIntegral = this.addIntegralRecord(integral, userId, userApp.getIntegral(), integralSourceEnum, remark);
		userApp.setIntegral(lastIntegral);
		userAppService.updateById(userApp);
	}

	@Override
	public void reductionIntegral(Double integral, Long userId, String remark) {
		UserApp userApp = userAppService.getByUserId(userId);
		Assert.notNull(userApp, "用户信息不存在");

		UserIntegral record = new UserIntegral();
		record.setCreateTime(new Date());
		record.setIntegral(integral);
		record.setRemark(remark);
		record.setUserId(userId);
		record.setType(2);// 支出
		record.setUserIntegral(userApp.getIntegral());
		record.setLastIntegral(userApp.getIntegral() - integral);
		record.setDataType(1);// 用户积分
		this.save(record);

		userApp.setIntegral(record.getLastIntegral());
		userAppService.updateById(userApp);
	}

}
