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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.constant.SystemConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.modules.guosen.bussiness.entity.BussinessDebit;
import org.springblade.modules.guosen.bussiness.vo.BussinessDebitVO;
import org.springblade.modules.guosen.bussiness.mapper.BussinessDebitMapper;
import org.springblade.modules.guosen.bussiness.service.IBussinessDebitService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

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

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-07-20
 */
@Service
public class BussinessDebitServiceImpl extends BaseServiceImpl<BussinessDebitMapper, BussinessDebit> implements IBussinessDebitService {

	@Override
	public IPage<BussinessDebitVO> selectBussinessDebitPage(IPage<BussinessDebitVO> page, BussinessDebitVO bussinessDebit) {
		return page.setRecords(baseMapper.selectBussinessDebitPage(page, bussinessDebit));
	}

	@Override
	public BussinessDebit getByBussinessOrderId(Long bussinessId, String bussinesssOrderId) {
		return baseMapper.getByBussinessOrderId(bussinessId, bussinesssOrderId);
	}

	@Override
	public BigDecimal debitStat(BussinessDebit bussinessDebit) {
		Date createTime = bussinessDebit.getCreateTime();
		String createTimeFormat = null;
		if(createTime != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			createTimeFormat = sdf.format(createTime);
			bussinessDebit.setCreateTime(null);
		}
		LambdaQueryWrapper<BussinessDebit> lambda = Condition.getQueryWrapper(bussinessDebit).lambda();
		lambda.like(createTime != null, BussinessDebit::getCreateTime, createTimeFormat);
		lambda.eq(BussinessDebit::getStatus, SystemConstant.BussinessDebitStatus.SUCCESS.getValue());
		lambda.eq(BussinessDebit::getType, SystemConstant.BussinessDebitType.COMMISSION.getValue());
		List<BussinessDebit> list = baseMapper.selectList(lambda);
		BigDecimal stat = list.stream().map(debit -> debit.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
		return stat;
	}

	@Override
	public BussinessDebit getByRequestNo(String requestNo) {
		LambdaQueryWrapper<BussinessDebit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BussinessDebit::getRequestNo, requestNo);
		return this.getOne(lambdaQueryWrapper);
	}

	@Override
	public BussinessDebit getByTransferOrderId(Long bussinessId, Long transferOrderId) {
		LambdaQueryWrapper<BussinessDebit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BussinessDebit::getBussinessId, bussinessId);
		lambdaQueryWrapper.eq(BussinessDebit::getTransferOrderId, transferOrderId);
		return this.getOne(lambdaQueryWrapper);
	}

}
