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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.common.constant.SystemConstant;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.guosen.bonusgroupbill.entity.BonusGroupBill;
import org.springblade.modules.guosen.bonusgroupbill.vo.BonusGroupBillVO;
import org.springblade.modules.guosen.bonusgroupbill.mapper.BonusGroupBillMapper;
import org.springblade.modules.guosen.bonusgroupbill.service.IBonusGroupBillService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussinesspayorder.entity.BussinessPayOrder;
import org.springblade.modules.guosen.bussinesspayorder.service.IBussinessPayOrderService;
import org.springblade.modules.system.service.IParamService;
import org.springblade.yeePay.service.BussinessPayService;
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.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 分润组收款账号 服务实现类
 *
 * @author BladeX
 * @since 2021-11-13
 */
@Service
public class BonusGroupBillServiceImpl extends BaseServiceImpl<BonusGroupBillMapper, BonusGroupBill> implements IBonusGroupBillService {

	@Autowired
	private BussinessPayService bussinessPayService;
	@Autowired
	private IBussinessPayOrderService payOrderService;
	@Autowired
	private IParamService paramService;

	@Override
	public IPage<BonusGroupBillVO> selectBonusGroupBillPage(IPage<BonusGroupBillVO> page, BonusGroupBillVO bonusGroupBill) {
		return page.setRecords(baseMapper.selectBonusGroupBillPage(page, bonusGroupBill));
	}

	@Override
	public R bonusByDailyStatic(Long dailyStaticId, Bussiness bussiness, Bussiness platformBussiness) {
		List<String> bonusStatus = new ArrayList<>();
		bonusStatus.add(SystemConstant.PayOrderStatus.WAITING.getValue());
		bonusStatus.add(SystemConstant.PayOrderStatus.ERROR.getValue());
		List<BonusGroupBill> billList = this.getByDailyStaticId(dailyStaticId, bonusStatus);
		if(billList == null || billList.size() <= 0){
			return R.fail("分润账单不存在或已分润");
		}
		for(BonusGroupBill bill : billList){
			this.bonusById(bill.getId(), bussiness, platformBussiness);
		}
		return R.status(true);
	}

	@Override
	public R bonusById(Long id, Bussiness bussiness, Bussiness platformBussiness) {
		BonusGroupBill bill = this.getById(id);
		BussinessPayOrder payOrder = new BussinessPayOrder();
		String requestNo = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		payOrder.setPayStatus(SystemConstant.PayOrderStatus.WAITING.getValue());
		payOrder.setRequestNo(requestNo);
		payOrder.setBankAccountType(bill.getBankAccountType());
		payOrder.setReceiverBankCode(bill.getReceiverBankCode());
		payOrder.setMethod(2);
		payOrder.setBizType(2); //平台分润
		payOrder.setBizId(id.toString());
		payOrder.setReceiverAccountName(bill.getAccountReceiverName());
		payOrder.setReceiverAccountNo(bill.getReceiverAccountNo());
		payOrder.setPayFlag("10");
		payOrder.setOrderAmount(bill.getBonusAmt());
		payOrder.setBussinessId(platformBussiness.getId());
		BigDecimal bonusLeastAmt = new BigDecimal(paramService.getValue("bonus_least_amt"));
		if(bill.getBonusAmt().compareTo(bonusLeastAmt) == -1){
			payOrder.setPayStatus(SystemConstant.PayOrderStatus.FAIL.getValue());
			payOrder.setFailReason("分润金额不能低于最低分润金额");
			boolean save = this.payOrderService.save(payOrder);
			return R.status(save);
		}else{
			boolean save = this.payOrderService.save(payOrder);
			if(save){
				R payResult = this.bussinessPayService.payOrder(payOrder, platformBussiness.getMerchantNo(), "PAYEE");
				return payResult;
			}
			return R.status(save);
		}
	}

	@Override
	public List<BonusGroupBill> getByDailyStaticId(Long dailyStaticId, List<String> bonusStatus) {
		LambdaQueryWrapper<BonusGroupBill> lambda = new LambdaQueryWrapper<>();
		lambda.eq(BonusGroupBill::getBonusDailyStaticId, dailyStaticId);
		lambda.in(bonusStatus != null && bonusStatus.size() > 0, BonusGroupBill::getBonusStatus, bonusStatus);
		return this.list(lambda);
	}

}
