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.entity.care.course.CourseSalesOrderRecordDO;
import com.yihu.jw.entity.care.course.PatientOrderRefundDO;
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 CourseSalesOrderRecordService extends BaseJpaService<CourseSalesOrderRecordDO, CourseSalesOrderRecordDao> {

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

    /**
     * 退费操作
     * @pa id
     */
    @Transactional(rollbackFor = Exception.class)
    public synchronized void refund(String id){
        CourseSalesOrderRecordDO courseSalesOrderRecordDO = courseSalesOrderRecordDao.findOne(id);
        PatientOrderRefundDO refundDO = patientOrderRefundDao.findByTypeAndOrderId("2",id);
        boolean flag = "3".equals(courseSalesOrderRecordDO.getStatus())&&
                (refundDO == null ||"1".equals(refundDO.getStatus()));
        if(flag){
            //待退费的状态才能操作
            refundDO.setStatus("2");
            patientOrderRefundDao.save(refundDO);
            courseSalesOrderRecordDO.setStatus("5");
            courseSalesOrderRecordDao.save(courseSalesOrderRecordDO);
            //微信退款操作
            //待开发
        }

    }

}
