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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.val;
import net.bytebuddy.asm.Advice;
import org.springblade.core.tool.api.R;
import org.springblade.modules.guosen.billRecord.entity.BillRecord;
import org.springblade.modules.guosen.bonusgroupaccount.entity.BonusGroupAccount;
import org.springblade.modules.guosen.bonusgroupaccount.service.IBonusGroupAccountService;
import org.springblade.modules.guosen.bonusgroupstrategy.entity.BonusGroupStrategy;
import org.springblade.modules.guosen.bonusgroupstrategy.vo.BonusGroupStrategyVO;
import org.springblade.modules.guosen.bonusgroupstrategy.mapper.BonusGroupStrategyMapper;
import org.springblade.modules.guosen.bonusgroupstrategy.service.IBonusGroupStrategyService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.guosen.bonusgroupstrategyaccount.service.IBonusGroupStrategyAccountService;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
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 java.math.BigDecimal;
import java.util.List;

/**
 * 分润业务场景 服务实现类
 *
 * @author BladeX
 * @since 2021-11-13
 */
@Service
public class BonusGroupStrategyServiceImpl extends BaseServiceImpl<BonusGroupStrategyMapper, BonusGroupStrategy> implements IBonusGroupStrategyService {

	@Autowired
	private IBonusGroupStrategyAccountService strategyAccountService;
	@Autowired
	private IBonusGroupAccountService accountService;

	@Override
	public IPage<BonusGroupStrategyVO> selectBonusGroupStrategyPage(IPage<BonusGroupStrategyVO> page, BonusGroupStrategyVO bonusGroupStrategy) {
		return page.setRecords(baseMapper.selectBonusGroupStrategyPage(page, bonusGroupStrategy));
	}

	@Override
	public R generateGroupStrategy(Long sceneId, Long groupId, Integer isDefault, String strategyName, BigDecimal rate, Integer strategyType, Bussiness bussiness) {
		BonusGroupStrategy strategy = new BonusGroupStrategy();
		strategy.setIsDefault(isDefault);
		strategy.setStrategyName(strategyName);
		strategy.setRate(rate);
		strategy.setBonusGroupId(groupId);
		strategy.setBonusSceneId(sceneId);
		strategy.setStrategyType(strategyType);
		boolean save = save(strategy);
		if(save){
			if(isDefault.intValue() == 1){ //创建了默认策略后，需要绑定默认的分润账户
				List<BonusGroupAccount> defaultAccountList = this.accountService.getDefaultAccounts();
				if(defaultAccountList != null && defaultAccountList.size() > 0){
					this.strategyAccountService.generateStrategyAccount(sceneId, groupId, strategy.getId(), defaultAccountList);
				}
			}
		}
		return R.status(save);
	}

	@Override
	public BigDecimal strategyRateStatic(Long sceneId, Long groupId) {
		LambdaQueryWrapper<BonusGroupStrategy> lambda = new LambdaQueryWrapper<>();
		lambda.eq(BonusGroupStrategy::getBonusSceneId, sceneId);
		lambda.eq(BonusGroupStrategy::getBonusGroupId, groupId);
		List<BonusGroupStrategy> list = this.list(lambda);
		BigDecimal strategyRateStatic = list.stream().map(item -> item.getRate()).reduce(BigDecimal.ZERO, BigDecimal::add);
		return strategyRateStatic;
	}

	@Override
	public List<BonusGroupStrategy> getBySceneGroup(Long sceneId, Long groupId) {
		LambdaQueryWrapper<BonusGroupStrategy> lambda = new LambdaQueryWrapper<>();
		lambda.eq(BonusGroupStrategy::getBonusSceneId, sceneId);
		lambda.eq(BonusGroupStrategy::getBonusGroupId, groupId);
		return this.list(lambda);
	}

}
