package com.yihu.jw.base.service.course;

import com.yihu.jw.base.dao.course.CourseSalesOrderRecordDao;
import com.yihu.jw.base.dao.course.PatientOrderRefundDao;
import com.yihu.jw.base.dao.course.RecruitStudentsRecordDao;
import com.yihu.jw.entity.care.course.CourseSalesOrderRecordDO;
import com.yihu.jw.entity.care.course.PatientOrderRefundDO;
import com.yihu.jw.entity.care.course.RecruitStudentsRecordDO;
import com.yihu.mysql.query.BaseJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/17
 * @Description:
 */
@Service
public class PatientOrderRefundService extends BaseJpaService<PatientOrderRefundDO, PatientOrderRefundDao> {

    @Autowired
    private PatientOrderRefundDao patientOrderRefundDao;
    @Autowired
    private CourseSalesOrderRecordDao courseSalesOrderRecordDao;
    @Autowired
    private RecruitStudentsRecordDao recruitStudentsRecordDao;

    /**
     * 退费操作
     * @pa id
     */
    @Transactional(rollbackFor = Exception.class)
    public synchronized void refund(String id){
        PatientOrderRefundDO refundDO = patientOrderRefundDao.findOne(id);
        CourseSalesOrderRecordDO courseSalesOrderRecordDO = null;
        RecruitStudentsRecordDO recruitStudentsRecordDO = null;
        String orderType = refundDO.getType();
        String orderStatus = "";
        if("2".equals(orderType)){
            courseSalesOrderRecordDO = courseSalesOrderRecordDao.findOne(refundDO.getOrderId());
            orderStatus = courseSalesOrderRecordDO.getStatus();
        }else if("1".equals(orderType)){
            recruitStudentsRecordDO = recruitStudentsRecordDao.findOne(refundDO.getOrderId());
            orderStatus = recruitStudentsRecordDO.getStatus();
        }

        if("1".equals(refundDO.getStatus())&&"3".equals(orderStatus)){
            //待退费的状态才能操作
            refundDO.setStatus("2");
            patientOrderRefundDao.save(refundDO);
            if("2".equals(orderType)){
                courseSalesOrderRecordDO.setStatus("5");
                courseSalesOrderRecordDao.save(courseSalesOrderRecordDO);
            }else if("1".equals(orderType)){
                recruitStudentsRecordDO.setStatus("5");
                recruitStudentsRecordDao.save(recruitStudentsRecordDO);
            }
            //微信退款操作
            //待开发
        }

    }

}
