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

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.common.enums.FrozenSatus;
import org.springblade.common.enums.FrozenType;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.ldt.bills.entity.Bills;
import org.springblade.ldt.bills.entity.FrozenRec;
import org.springblade.ldt.bills.mapper.FrozenRecMapper;
import org.springblade.ldt.bills.service.IFrozenRecService;
import org.springblade.ldt.bills.vo.FrozenRecVO;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.entity.UserChannelPoint;
import org.springblade.payment.handle.handler.DataHandle;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-10-30
 */
@Service
public class FrozenRecServiceImpl extends BaseServiceImpl<FrozenRecMapper, FrozenRec> implements IFrozenRecService {

	private DataHandle dataHandle;

	@Override
	public IPage<FrozenRecVO> selectFrozenRecPage(IPage<FrozenRecVO> page, FrozenRecVO frozenRec) {
		return page.setRecords(baseMapper.selectFrozenRecPage(page, frozenRec));
	}

	@Override
	public FrozenRec addFrozenRec(BigDecimal handlePrice, Bills bills, LoginUser user, FrozenType type, UserChannelPoint userChannelPoint) {

		FrozenRec frozenRec = new FrozenRec();
		frozenRec.setFrozenNum(handlePrice);
		frozenRec.setUserId(user.getId());
		frozenRec.setUserName(user.getNickName());
		frozenRec.setTradeNo(bills.getId());
		frozenRec.setFrozenType(type);
		frozenRec.setStatus(1);
		frozenRec.setFrozenSatus(FrozenSatus.SUCCEED_FROZEN);

		if (Objects.equals(type, FrozenType.POINT)) {
			frozenRec.setBeforeNum(userChannelPoint.getAvailable());
			user.setChannelPoint(user.getChannelPoint().subtract(handlePrice));
			user.setFrozenPoint(user.getFrozenPoint().add(handlePrice));
			userChannelPoint.setAvailable(userChannelPoint.getAvailable().subtract(handlePrice));
		} else if (Objects.equals(type, FrozenType.BALANCE)) {
			frozenRec.setBeforeNum(user.getBalance());
			user.setBalance(user.getBalance().subtract(handlePrice));
			user.setFrozenBalance(user.getFrozenBalance().add(handlePrice));
		}

		return frozenRec;
	}

}
