package com.hzs.third.pay.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.third.pay.TOnlinePaymentSepAcc;
import com.hzs.common.domain.third.pay.TOnlinePaymentSepAccD;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.third.pay.mapper.TOnlinePaymentSepAccMapper;
import com.hzs.third.pay.service.ITOnlinePaymentSepAccDService;
import com.hzs.third.pay.service.ITOnlinePaymentSepAccService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 支付请求主表 服务实现类
 * </p>
 *
 * @author bd
 * @since 2025-07-01
 */
@Service
public class TOnlinePaymentSepAccServiceImpl extends ServiceImpl<TOnlinePaymentSepAccMapper, TOnlinePaymentSepAcc> implements ITOnlinePaymentSepAccService {

    @Autowired
    private ITOnlinePaymentSepAccDService itOnlinePaymentSepAccDService;
    @Override
    public void saveOrUpdateReq(TOnlinePaymentSepAcc tOnlinePaymentSepAcc) {
        LambdaQueryWrapper<TOnlinePaymentSepAcc> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TOnlinePaymentSepAcc::getPkPayment, tOnlinePaymentSepAcc.getPkPayment());
        TOnlinePaymentSepAcc source = this.getOne(wrapper, false);
        if(ObjectUtil.isNotEmpty(source)){
            tOnlinePaymentSepAcc.setPkId(source.getPkId());
        }else{
            tOnlinePaymentSepAcc.setCreationTime(DateUtils.currentDateTime());
            tOnlinePaymentSepAcc.setPkCreator(SecurityUtils.getUserId());
        }
        tOnlinePaymentSepAcc.setDelFlag(EDelFlag.UN_DELETE.getValue());
        tOnlinePaymentSepAcc.setModifiedTime(DateUtils.currentDateTime());
        tOnlinePaymentSepAcc.setPkModified(SecurityUtils.getUserId());
        this.saveOrUpdate(tOnlinePaymentSepAcc);
        LambdaQueryWrapper<TOnlinePaymentSepAccD> wrapperD = new LambdaQueryWrapper<>();
        wrapperD.eq(TOnlinePaymentSepAccD::getPkSepAcc, tOnlinePaymentSepAcc.getPkId());
        itOnlinePaymentSepAccDService.remove(wrapperD);
        if(CollUtil.isNotEmpty(tOnlinePaymentSepAcc.getSepAccDList())){
            for (TOnlinePaymentSepAccD tOnlinePaymentSepAccD : tOnlinePaymentSepAcc.getSepAccDList()) {
                tOnlinePaymentSepAccD.setPkSepAcc(tOnlinePaymentSepAcc.getPkId());
                tOnlinePaymentSepAccD.setCreationTime(DateUtils.currentDateTime());
                tOnlinePaymentSepAccD.setPkCreator(SecurityUtils.getUserId());
                tOnlinePaymentSepAccD.setModifiedTime(DateUtils.currentDateTime());
                tOnlinePaymentSepAccD.setPkModified(SecurityUtils.getUserId());
                tOnlinePaymentSepAccD.setDelFlag(EDelFlag.UN_DELETE.getValue());
            }
            itOnlinePaymentSepAccDService.saveBatch(tOnlinePaymentSepAcc.getSepAccDList());
        }

    }

    @Override
    public TOnlinePaymentSepAcc getSepAccByOrderCode(String code) {
        return baseMapper.getSepAccByOrderCode(code);
    }
}
