package com.jeequan.jeepay.service.impl;

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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.entity.AccountOperate;
import com.jeequan.jeepay.db.entity.AccountChangeInfo;
import com.jeequan.jeepay.db.entity.AccountFundInfo;
import com.jeequan.jeepay.service.mapper.AccountFundInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-03-28
 */
@Service
public class AccountFundInfoService extends ServiceImpl<AccountFundInfoMapper, AccountFundInfo>{

    @Autowired
    private AccountInfoService accountInfoService;

    @Autowired
    private AccountFundInfoMapper accountFundInfoMapper;
    /**
     * 分页查询
     * @param iPage
     * @param wrapper
     * @param info
     * @param paramJSON
     * @return
     */
    public Page<AccountFundInfo> pageList(Page<AccountFundInfo> iPage, LambdaQueryWrapper<AccountFundInfo> wrapper, AccountFundInfo info, JSONObject paramJSON) {
        // 时间范围条件
        Date[] dateRange = info.buildQueryDateRange();
        if (dateRange[0] != null) {
            wrapper.ge(AccountFundInfo::getUpdatedAt, dateRange[0]);
        }
        if (dateRange[1] != null) {
            wrapper.le(AccountFundInfo::getUpdatedAt, dateRange[1]);
        }
        wrapper.orderByDesc(AccountFundInfo::getUpdatedAt);
        return page(iPage, wrapper);
    }


    /**
     * 确认充值操作
     * @param payOrderId
     */
    public void confirmRecharge(String payOrderId) {
        AccountFundInfo fundInfo = accountFundInfoMapper.selectById(payOrderId);
        if(fundInfo != null){
            AccountOperate operate = new AccountOperate(payOrderId,true, AccountFundInfo.SUCCESS);
            accountInfoService.upAccount(operate);
        }
    }

    /**
     * 更新成付款失败
     * @param rid
     * @param channelErrMsg
     * @return
     */
    public boolean updateIng2Fail(String rid, String channelErrMsg) {
        AccountFundInfo updateRecord = new AccountFundInfo();
        updateRecord.setState(AccountFundInfo.FAIL);
        updateRecord.setRemark(channelErrMsg);
        return update(updateRecord, new LambdaUpdateWrapper<AccountFundInfo>()
                .eq(AccountFundInfo::getOrderId, rid).eq(AccountFundInfo::getState,AccountFundInfo.PAYMENT));
    }

    public boolean updateIng2Success(String rid, String channelOrderId) {
        AccountFundInfo updateRecord = new AccountFundInfo();
        updateRecord.setState(AccountFundInfo.SUCCESS);
        updateRecord.setChannelOrderId(channelOrderId);
        updateRecord.setRemark("打款成功");
        return update(updateRecord, new LambdaUpdateWrapper<AccountFundInfo>()
                .eq(AccountFundInfo::getOrderId, rid).eq(AccountFundInfo::getState,AccountFundInfo.PAYMENT));
    }

    public boolean updateChannelRemark(String rid, String channelErrMsg) {
        AccountFundInfo updateRecord = new AccountFundInfo();
        updateRecord.setRemark(channelErrMsg);
        return update(updateRecord, new LambdaUpdateWrapper<AccountFundInfo>()
                .eq(AccountFundInfo::getOrderId, rid).eq(AccountFundInfo::getState,AccountFundInfo.PAYMENT));
    }
}
