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

import org.springblade.common.constant.SystemConstant;
import org.springblade.core.tool.api.R;
import org.springblade.modules.guosen.bonusgroup.entity.BonusGroup;
import org.springblade.modules.guosen.bonusgroup.service.IBonusGroupService;
import org.springblade.modules.guosen.bonusgroupaccount.entity.BonusGroupAccount;
import org.springblade.modules.guosen.bonusgroupaccount.service.IBonusGroupAccountService;
import org.springblade.modules.guosen.bonusgroupbill.entity.BonusGroupBill;
import org.springblade.modules.guosen.bonusgroupbill.service.IBonusGroupBillService;
import org.springblade.modules.guosen.bonusgroupdailystatic.entity.BonusGroupDailyStatic;
import org.springblade.modules.guosen.bonusgroupdailystatic.vo.BonusGroupDailyStaticVO;
import org.springblade.modules.guosen.bonusgroupdailystatic.mapper.BonusGroupDailyStaticMapper;
import org.springblade.modules.guosen.bonusgroupdailystatic.service.IBonusGroupDailyStaticService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.guosen.bonusgroupstrategy.entity.BonusGroupStrategy;
import org.springblade.modules.guosen.bonusgroupstrategy.service.IBonusGroupStrategyService;
import org.springblade.modules.guosen.bonusgroupstrategyaccount.entity.BonusGroupStrategyAccount;
import org.springblade.modules.guosen.bonusgroupstrategyaccount.service.IBonusGroupStrategyAccountService;
import org.springblade.modules.guosen.bonusscene.entity.BonusScene;
import org.springblade.modules.guosen.bonusscene.service.IBonusSceneService;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussiness.service.IBussinessService;
import org.springblade.modules.system.service.IParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 分润组每日分润金额统计 服务实现类
 *
 * @author BladeX
 * @since 2021-11-13
 */
@Service
public class BonusGroupDailyStaticServiceImpl extends BaseServiceImpl<BonusGroupDailyStaticMapper, BonusGroupDailyStatic> implements IBonusGroupDailyStaticService {

	@Autowired
	private IBonusSceneService sceneService;
	@Autowired
	private IBonusGroupService groupService;
	@Autowired
	private IBonusGroupStrategyService strategyService;
	@Autowired
	private IBussinessService bussinessService;
	@Autowired
	private IBonusGroupStrategyAccountService strategyAccountService;
	@Autowired
	private IBonusGroupAccountService accountService;
	@Autowired
	private IBonusGroupBillService bonusGroupBillService;

	@Override
	public IPage<BonusGroupDailyStaticVO> selectBonusGroupDaliyStaticPage(IPage<BonusGroupDailyStaticVO> page, BonusGroupDailyStaticVO bonusGroupDaliyStatic) {
		return page.setRecords(baseMapper.selectBonusGroupDaliyStaticPage(page, bonusGroupDaliyStatic));
	}

	@Override
	public R generateDailyStatic(Long platformBussinessId, Long bussinessId, BigDecimal rechargeAmt, BigDecimal rechargeServiceAmt, BigDecimal rechargeFee, Integer rechargeCount, BigDecimal bonusAmt, Date date) {
		BonusScene scene = this.sceneService.getByPlatform(platformBussinessId);
		BonusGroup group = this.groupService.getByBussinessId(scene.getId(), bussinessId);
		Bussiness bussiness = this.bussinessService.getById(bussinessId);
		BonusGroupDailyStatic dailyStatic = new BonusGroupDailyStatic();
		dailyStatic.setBonusSceneId(scene.getId());
		dailyStatic.setBonusGroupId(group.getId());
		dailyStatic.setRechargeAmt(rechargeAmt);
		dailyStatic.setServiceFee(rechargeServiceAmt);
		dailyStatic.setChannelFee(rechargeFee);
		dailyStatic.setBonusAmt(bonusAmt);
		dailyStatic.setRechargeNum(rechargeCount);
		dailyStatic.setBonusDate(date);
		boolean save = this.save(dailyStatic);
		if(save){
			List<BonusGroupStrategy> strategyList = this.strategyService.getBySceneGroup(scene.getId(), group.getId());
			for(BonusGroupStrategy strategy : strategyList){
				BigDecimal strategyRate = strategy.getRate();
				BigDecimal strategyBonusAmt = strategyRate.divide(bussiness.getServiceRate().divide(new BigDecimal(10)), 2, BigDecimal.ROUND_HALF_UP).multiply(bonusAmt);
				List<BonusGroupStrategyAccount> strategyAccountList = this.strategyAccountService.getBySceneGroupStrategy(scene.getId(), group.getId(), strategy.getId());
				for(BonusGroupStrategyAccount strategyAccount : strategyAccountList){
					Long accountId = strategyAccount.getBonusGroupAccountId();
					BonusGroupAccount account = this.accountService.getById(accountId);
					BonusGroupBill bill = new BonusGroupBill();
					bill.setReceiverBankCode(account.getReceiverBankCode());
					bill.setReceiverAccountNo(account.getReceiverAccountNo());
					bill.setAccountId(accountId);
					bill.setAccountReceiverName(account.getAccountReceiverName());
					bill.setBankAccountType(account.getBankAccountType());
					bill.setBonusAmt(strategyAccount.getRate().divide(new BigDecimal(100)).multiply(strategyBonusAmt));
					bill.setBonusGroupId(group.getId());
					bill.setBonusSceneId(scene.getId());
					bill.setBonusSceneName(scene.getSceneName());
					bill.setBonusStrategyId(strategy.getId());
					bill.setBonusStrategyAccountId(strategyAccount.getId());
					bill.setRate(strategyAccount.getRate());
					bill.setBonusDate(date);
					bill.setBonusSceneName(scene.getSceneName());
					bill.setBonusGroupName(group.getGroupName());
					bill.setBonusStrategyName(strategy.getStrategyName());
					bill.setAccountIsDefault(account.getIsDefault());
					bill.setBonusDailyStaticId(dailyStatic.getId());
					this.bonusGroupBillService.save(bill);
				}
			}
		}
		return R.status(false);
	}

	@Override
	public R bonus(Long dailyStaticId, Bussiness bussiness, Bussiness platformBussiness) {
		Assert.notNull(dailyStaticId, "报表ID必传");
		R result = bonusGroupBillService.bonusByDailyStatic(dailyStaticId, bussiness, platformBussiness);
		return result;
	}

}
