package com.fs.hospital.service.impl;

import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.HospitalOutpatientOrder;
import com.fs.hospital.mapper.HospitalOutpatientOrderMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalPaymentMapper;
import com.fs.hospital.domain.HospitalPayment;
import com.fs.hospital.service.IHospitalPaymentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 支付明细Service业务层处理
 *
 * @author fs
 * @date 2024-03-26
 */
@Service
public class HospitalPaymentServiceImpl implements IHospitalPaymentService
{
    @Autowired
    private HospitalPaymentMapper hospitalPaymentMapper;
    @Autowired
    private HospitalOutpatientOrderMapper outpatientOrderMapper;

    /**
     * 查询支付明细
     *
     * @param paymentId 支付明细主键
     * @return 支付明细
     */
    @Override
    public HospitalPayment selectHospitalPaymentByPaymentId(Long paymentId)
    {
        return hospitalPaymentMapper.selectHospitalPaymentByPaymentId(paymentId);
    }

    /**
     * 查询支付明细列表
     *
     * @param hospitalPayment 支付明细
     * @return 支付明细
     */
    @Override
    public List<HospitalPayment> selectHospitalPaymentList(HospitalPayment hospitalPayment)
    {
        return hospitalPaymentMapper.selectHospitalPaymentList(hospitalPayment);
    }

    /**
     * 新增支付明细
     *
     * @param hospitalPayment 支付明细
     * @return 结果
     */
    @Override
    public R insertHospitalPayment(HospitalPayment hospitalPayment)
    {
        hospitalPayment.setCreateTime(DateUtils.getNowDate());
        hospitalPayment.setPayTime(DateUtils.getNowDate());
//        if (hospitalPaymentMapper.insertHospitalPayment(hospitalPayment)>0){
//            if (hospitalPayment.getBusinessType()==1){
//
//            }
//        }
        return R.error();
    }

    /**
     * 修改支付明细
     *
     * @param hospitalPayment 支付明细
     * @return 结果
     */
    @Override
    public int updateHospitalPayment(HospitalPayment hospitalPayment)
    {
        return hospitalPaymentMapper.updateHospitalPayment(hospitalPayment);
    }

    /**
     * 批量删除支付明细
     *
     * @param paymentIds 需要删除的支付明细主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPaymentByPaymentIds(Long[] paymentIds)
    {
        return hospitalPaymentMapper.deleteHospitalPaymentByPaymentIds(paymentIds);
    }

    /**
     * 删除支付明细信息
     *
     * @param paymentId 支付明细主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPaymentByPaymentId(Long paymentId)
    {
        return hospitalPaymentMapper.deleteHospitalPaymentByPaymentId(paymentId);
    }

    @Override
    @Transactional
    public R finishPayment(HospitalPayment hospitalPayment) {
        hospitalPayment.setPayTime(new Date());
        if (hospitalPaymentMapper.updateHospitalPayment(hospitalPayment)>0){
            if (hospitalPayment.getBusinessType()==1){
                HospitalOutpatientOrder outpatientOrder = outpatientOrderMapper.selectHospitalOutpatientOrderByOrderId(Long.valueOf(hospitalPayment.getBusinessId()));
                if (outpatientOrder!=null && outpatientOrder.getIsPay()==0){
                    outpatientOrder.setIsPay(1);
                    outpatientOrderMapper.updateHospitalOutpatientOrder(outpatientOrder);
                }
            }
            return R.ok();
        }
        return R.error();
    }

    @Override
    public HospitalPayment selectHospitalPayment(Long orderId, Integer type) {
        String businessId = orderId.toString();
        return  hospitalPaymentMapper.selectHospitalPayment(businessId,type);
    }
}
