package com.cyksj.service.user.impl;

import com.cyksj.common.EnvCommonService;
import com.cyksj.common.constant.Constant;
import com.cyksj.mapper.DistributeOrdersDeductPointsRecordMapper;
import com.cyksj.mapper.UserBalanceSourceRecordMapper;
import com.cyksj.mapper.UserMapper;
import com.cyksj.mapper.shop.YhShopDistributeOrdersDeductPointsRecordMapper;
import com.cyksj.model.entity.DistributeOrdersDeductPointsRecord;
import com.cyksj.model.entity.User;
import com.cyksj.model.entity.UserBalanceSourceRecord;
import com.cyksj.model.entity.YhShopDistributeOrdersDeductPointsRecord;
import com.cyksj.service.sms.SmsService;
import com.cyksj.service.user.UserBenefitsService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/*
 *项目名: netflix
 *文件名: UserBenefitsServiceImpl
 *创建者: JavaZou
 *创建时间:2023/5/18 16:06
 */
@Service
@RequiredArgsConstructor
public class UserBenefitsServiceImpl implements UserBenefitsService {
	private final UserMapper userMapper;

	private final DistributeOrdersDeductPointsRecordMapper distributeOrdersDeductPointsRecordMapper;

	private final UserBalanceSourceRecordMapper userBalanceSourceRecordMapper;

	private final EnvCommonService envCommonService;

	private final SmsService smsService;

	private final YhShopDistributeOrdersDeductPointsRecordMapper yhShopDistributeOrdersDeductPointsRecordMapper;

	@Override
	public void deductDistributePoints(Long orderId, Long userId, BigDecimal deductPoints) {
		if (deductPoints.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal points = user.getPoints();
			Integer success = userMapper.subPointsNegative(userId, points, deductPoints);
			if (success == 1){
				//记录积分已发送后,退款后扣除积分记录
				deductPointsRecord(orderId, userId, deductPoints);
				break;
			}
		}
	}

	@Override
	public void addDistributePoints(Long userId, BigDecimal addPoints) {
		if (addPoints.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal points = user.getPoints();
			Integer success = userMapper.updatePoints(user.getId(), points, addPoints);
			if (success == 1) break;
		}
	}

	@Override
	public void addUserBalance(Long userId, BigDecimal addBalance, UserBalanceSourceRecord.Source source, Long yhsId, Long orderId, String detail, Boolean isRecord) {
		if (addBalance.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal balance = user.getBalance();
			Integer success = userMapper.addUserBalance(user.getId(), balance, addBalance);
			if (success == 1) {
				if (isRecord) {
					recordBalanceBySource(userId, addBalance, source, orderId, detail, null, null);
				}
				if (envCommonService.isPrdEnv() && isRecord && yhsId != null && yhsId == 0) {
					try {
						//短信到账提醒
						String msg = String.format(Constant.BALANCE_NOTIFY_MSG, addBalance.divide(BigDecimal.valueOf(100)));
						smsService.sendSmsToRelationUser(userId, msg);
					} catch (Exception e) {
					}
				}
				break;
			}
		}
	}

	@Override
	public void deductUserBalance(Long userId, BigDecimal subBalance) {
		if (subBalance.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal balance = user.getBalance();
			Integer success = userMapper.deductUserBalance(user.getId(), balance, subBalance);
			if (success == 1) break;
		}
	}

	public void deductPointsRecord(Long orderId, Long userId, BigDecimal deductPoints) {
		try {
			DistributeOrdersDeductPointsRecord record = new DistributeOrdersDeductPointsRecord();
			record.setOrderId(orderId);
			record.setUserId(userId);
			record.setDeductPoints(deductPoints);
			distributeOrdersDeductPointsRecordMapper.insert(record);
		} catch (DuplicateKeyException e) {

		}
	}

	@Override
	public void recordBalanceBySource(Long userId, BigDecimal balance, UserBalanceSourceRecord.Source source, Long orderId, String detail, String remark, String operator) {
		UserBalanceSourceRecord userBalanceSourceRecord = new UserBalanceSourceRecord();
		userBalanceSourceRecord.setUserId(userId);
		userBalanceSourceRecord.setBalance(balance);
		userBalanceSourceRecord.setSource(source);
		userBalanceSourceRecord.setOrderId(orderId);
		userBalanceSourceRecord.setDetail(detail);
		userBalanceSourceRecord.setRemark(remark);
		userBalanceSourceRecord.setOperator(operator);
		userBalanceSourceRecordMapper.insert(userBalanceSourceRecord);
	}

	@Override
	public void addYhsMoney(Long userId, BigDecimal addYhsMoney) {
		if (addYhsMoney.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal yhsMoney = user.getYhsMoney();
			Integer success = userMapper.addYhsMoney(user.getId(), yhsMoney, addYhsMoney);
			if (success == 1) {
				break;
			}
		}
	}

	@Override
	public void deductYhShopTakeMoney(Long orderId, Long shopUserId, BigDecimal deductMoney) {
		if (deductMoney.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(shopUserId);
			if (user == null) break;
			BigDecimal yhsMoney = user.getYhsMoney();
			Integer success = userMapper.subYhShopUserMoney(shopUserId, yhsMoney, deductMoney);
			if (success == 1){
				//记录提成已发送后,退款后扣除用户店铺提成 记录
				deductYhsMoneyRecord(orderId, shopUserId, deductMoney);
				break;
			}
		}
	}

	@Override
	public void addDistributeSubMoney(Long userId, BigDecimal addApplyMoney) {
		if (addApplyMoney.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal subApplyMoney = user.getSubApplyMoney();
			Integer success = userMapper.addDistributeSubMoney(user.getId(), subApplyMoney, addApplyMoney);
			if (success == 1) {
				break;
			}
		}
	}

	@Override
	public void addMpMoney(Long userId, BigDecimal addMoney) {
		if (addMoney.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(userId);
			if (user == null) break;
			BigDecimal mpMoney = user.getMpMoney();
			Integer success = userMapper.addMpMoney(user.getId(), mpMoney, addMoney);
			if (success == 1) {
				break;
			}
		}
	}

	@Override
	public void deductUserMirrorShopTakeMoney(Long orderId, Long shopUserId, BigDecimal deductMoney) {
		if (deductMoney.compareTo(BigDecimal.ZERO) <= 0) {
			return;
		}
		while (true) {
			User user = userMapper.selectById(shopUserId);
			if (user == null) break;
			BigDecimal mpMoney = user.getMpMoney();
			Integer success = userMapper.subUserMirrorShopMoney(shopUserId, mpMoney, deductMoney);
			if (success == 1){
				break;
			}
		}
	}

	private void deductYhsMoneyRecord(Long orderId, Long shopUserId, BigDecimal deductMoney) {
		try {
			YhShopDistributeOrdersDeductPointsRecord record = new YhShopDistributeOrdersDeductPointsRecord();
			record.setOrderId(orderId);
			record.setShopUserId(shopUserId);
			record.setDeductMoney(deductMoney);
			yhShopDistributeOrdersDeductPointsRecordMapper.insert(record);
		} catch (DuplicateKeyException e) {

		}
	}
}
