/*
* Copyright (c) 2021-2031, 河北计全科技有限公司 (https://www.jeequan.com & jeequan@126.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jeequan.jeepay.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.IsvInfo;
import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.entity.PayWay;
import com.jeequan.jeepay.service.mapper.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;
/**
* <p>
* 支付订单表 服务实现类
* </p>
*
* @author [mybatis plus generator]
* @since 2021-04-27
*/
@Service
public class PayOrderService extends ServiceImpl<PayOrderMapper, PayOrder> {
@Autowired private PayOrderMapper payOrderMapper;
@Autowired private MchInfoMapper mchInfoMapper;
@Autowired private IsvInfoMapper isvInfoMapper;
@Autowired private PayWayMapper payWayMapper;
@Autowired private PayOrderDivisionRecordMapper payOrderDivisionRecordMapper;
/** 更新订单状态 【订单生成】 --》 【支付中】 **/
public boolean updateInit2Ing(String payOrderId, PayOrder payOrder){
PayOrder updateRecord = new PayOrder();
updateRecord.setState(PayOrder.STATE_ING);
//同时更新, 未确定 --》 已确定的其他信息。 如支付接口的确认、 费率的计算。
updateRecord.setIfCode(payOrder.getIfCode());
updateRecord.setWayCode(payOrder.getWayCode());
updateRecord.setMchFeeRate(payOrder.getMchFeeRate());
updateRecord.setMchFeeAmount(payOrder.getMchFeeAmount());
updateRecord.setChannelUser(payOrder.getChannelUser());
updateRecord.setChannelOrderNo(payOrder.getChannelOrderNo());
return update(updateRecord, new LambdaUpdateWrapper<PayOrder>()
.eq(PayOrder::getPayOrderId, payOrderId).eq(PayOrder::getState, PayOrder.STATE_INIT));
}
/** 更新订单状态 【支付中】 --》 【支付成功】 **/
public boolean updateIng2Success(String payOrderId, String channelOrderNo, String channelUserId){
PayOrder updateRecord = new PayOrder();
updateRecord.setState(PayOrder.STATE_SUCCESS);
updateRecord.setChannelOrderNo(channelOrderNo);
updateRecord.setChannelUser(channelUserId);
updateRecord.setSuccessTime(new Date());
return update(updateRecord, new LambdaUpdateWrapper<PayOrder>()
.eq(PayOrder::getPayOrderId, payOrderId).eq(PayOrder::getState, PayOrder.STATE_ING));
}
/** 更新订单状态 【支付中】 --》 【订单关闭】 **/
public boolean updateIng2Close(String payOrderId){
PayOrder updateRecord = new PayOrder();
updateRecord.setState(PayOrder.STATE_CLOSED);
return update(updateRecord, new LambdaUpdateWrapper<PayOrder>()
.eq(PayOrder::getPayOrderId, payOrderId).eq(PayOrder::getState, PayOrder.STATE_ING));
}
/** 更新订单状态 【订单生成】 --》 【订单关闭】 **/
public boolean updateInit2Close(String payOrderId){
PayOrder updateRecord = new PayOrder();
updateRecord.setState(PayOrder.STATE_CLOSED);
return update(updateRecord, new LambdaUpdateWrapper<PayOrder>()
.eq(PayOrder::getPayOrderId, payOrderId).eq(PayOrder::getState, PayOrder.STATE_INIT));
}
/** 更新订单状态 【支付中】 --》 【支付失败】 **/
public boolean updateIng2Fail(String payOrderId, String channelOrderNo, String channelUserId, String channelErrCode, String channelErrMsg){
PayOrder updateRecord = new PayOrder();
updateRecord.setState(PayOrder.STATE_FAIL);
updateRecord.setErrCode(channelErrCode);
updateRecord.setErrMsg(channelErrMsg);
updateRecord.setChannelOrderNo(channelOrderNo);
updateRecord.setChannelUser(channelUserId);
return update(updateRecord, new LambdaUpdateWrapper<PayOrder>()
.eq(PayOrder::getPayOrderId, payOrderId).eq(PayOrder::getState, PayOrder.STATE_ING));
}
/** 更新订单状态 【支付中】 --》 【支付成功/支付失败】 **/
public boolean updateIng2SuccessOrFail(String payOrderId, Byte updateState, String channelOrderNo, String channelUserId, String channelErrCode, String channelErrMsg){
if(updateState == PayOrder.STATE_ING){
return true;
}else if(updateState == PayOrder.STATE_SUCCESS){
return updateIng2Success(payOrderId, channelOrderNo, channelUserId);
}else if(updateState == PayOrder.STATE_FAIL){
return updateIng2Fail(payOrderId, channelOrderNo, channelUserId, channelErrCode, channelErrMsg);
}
return false;
}
/** 查询商户订单 **/
public PayOrder queryMchOrder(String mchNo, String payOrderId, String mchOrderNo){
if(StringUtils.isNotEmpty(payOrderId)){
return getOne(PayOrder.gw().eq(PayOrder::getMchNo, mchNo).eq(PayOrder::getPayOrderId, payOrderId));
}else if(StringUtils.isNotEmpty(mchOrderNo)){
return getOne(PayOrder.gw().eq(PayOrder::getMchNo, mchNo).eq(PayOrder::getMchOrderNo, mchOrderNo));
}else{
return null;
}
}
public Map payCount(String mchNo, Byte state, Byte refundState, String dayStart, String dayEnd) {
Map param = new HashMap<>();
if (state != null) {
param.put("state", state);
}
if (refundState != null) {
param.put("refundState", refundState);
}
if (StrUtil.isNotBlank(mchNo)) {
param.put("mchNo", mchNo);
}
if (StrUtil.isNotBlank(dayStart)) {
param.put("createTimeStart", dayStart);
}
if (StrUtil.isNotBlank(dayEnd)) {
param.put("createTimeEnd", dayEnd);
}
return payOrderMapper.payCount(param);
}
public List<Map> payTypeCount(String mchNo, Byte state, Byte refundState, String dayStart, String dayEnd) {
Map param = new HashMap<>();
if (state != null) {
param.put("state", state);
}
if (refundState != null) {
param.put("refundState", refundState);
}
if (StrUtil.isNotBlank(mchNo)) {
param.put("mchNo", mchNo);
}
if (StrUtil.isNotBlank(dayStart)) {
param.put("createTimeStart", dayStart);
}
if (StrUtil.isNotBlank(dayEnd)) {
param.put("createTimeEnd", dayEnd);
}
return payOrderMapper.payTypeCount(param);
}
/** 更新订单为 超时状态 **/
public Integer updateOrderExpired(){
PayOrder payOrder = new PayOrder();
payOrder.setState(PayOrder.STATE_CLOSED);
return baseMapper.update(payOrder,
PayOrder.gw()