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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.val;
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.vo.BonusGroupVO;
import org.springblade.modules.guosen.bonusgroup.mapper.BonusGroupMapper;
import org.springblade.modules.guosen.bonusgroup.service.IBonusGroupService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.guosen.bonusgroupstrategy.service.IBonusGroupStrategyService;
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.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;

/**
 * 分润组 服务实现类
 *
 * @author BladeX
 * @since 2021-11-13
 */
@Service
public class BonusGroupServiceImpl extends BaseServiceImpl<BonusGroupMapper, BonusGroup> implements IBonusGroupService {
	@Autowired
	private IParamService paramService;
	@Autowired
	private IBonusGroupStrategyService strategyService;
	@Autowired
	private IBonusSceneService sceneService;

	@Override
	public IPage<BonusGroupVO> selectBonusGroupPage(IPage<BonusGroupVO> page, BonusGroupVO bonusGroup) {
		return page.setRecords(baseMapper.selectBonusGroupPage(page, bonusGroup));
	}

	@Override
	public R generateBonusGroup(Bussiness bussiness) {
		BonusScene scene = sceneService.getByPlatform(bussiness.getParentBussinessId());
		if(scene == null){
			R.fail("请先创建业务场景");
		}
		BonusGroup group = new BonusGroup();
		group.setBounsSceneId(scene.getId());
		group.setBussinessId(bussiness.getId());
		group.setGroupName(bussiness.getName());
		group.setServiceRate(bussiness.getServiceRate().divide(new BigDecimal(10)));
		boolean save = this.save(group);
		if(save){
		  BigDecimal platformBonusStrategyRate = new BigDecimal(paramService.getValue("platform_bonus_strategy_rate"));
          BigDecimal bussinessRate = (bussiness.getServiceRate().divide(new BigDecimal(10))).subtract(platformBonusStrategyRate);
          R platformStrategySave = this.strategyService.generateGroupStrategy(scene.getId(), group.getId(), 1, bussiness.getName() + "平台分润策略", platformBonusStrategyRate, SystemConstant.BonusStrategyType.PLATFORM_BONUS_STRATEGY.getValue(), bussiness);
//          R bussinessStrategySave = this.strategyService.generateGroupStrategy(scene.getId(), group.getId(), 0, bussiness.getName() + "商户分润策略", bussinessRate, SystemConstant.BonusStrategyType.BUSSINESS_BONUS_STRATEGY.getValue(), bussiness);
          return R.status(platformStrategySave.isSuccess());
		}else{
			return R.fail("分润商户创建失败");
		}
	}

	@Override
	public BonusGroup getByBussinessId(Long sceneId, Long bussinessId) {
		LambdaQueryWrapper<BonusGroup> lambda = new LambdaQueryWrapper<>();
		lambda.eq(BonusGroup::getBounsSceneId, sceneId);
		lambda.eq(BonusGroup::getBussinessId, bussinessId);
		BonusGroup group = this.baseMapper.selectOne(lambda);
		return group;
	}

}
