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

import com.yihu.jw.base.dao.course.PatientOrderRefundDao;
import com.yihu.jw.base.dao.course.RecruitStudentsDao;
import com.yihu.jw.base.dao.course.RecruitStudentsRecordDao;
import com.yihu.jw.base.util.EntranceUtil;
import com.yihu.jw.base.util.WxMessageUtil;
import com.yihu.jw.entity.care.course.PatientOrderRefundDO;
import com.yihu.jw.entity.care.course.RecruitStudentsDO;
import com.yihu.jw.entity.care.course.RecruitStudentsRecordDO;
import com.yihu.mysql.query.BaseJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/12
 * @Description:
 */
@Service
public class RecruitStudentsRecordService extends BaseJpaService<RecruitStudentsRecordDO, RecruitStudentsRecordDao> {

    @Autowired
    private PatientOrderRefundDao patientOrderRefundDao;
    @Autowired
    private RecruitStudentsRecordDao recruitStudentsRecordDao;
    @Autowired
    private RecruitStudentsDao recruitStudentsDao;
    @Autowired
    private WxMessageUtil wxMessageUtil;
    @Autowired
    private EntranceUtil entranceUtil;

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


        }

    }

    public int leaveSchool(String id){
       int ret = recruitStudentsRecordDao.updateStatus(id);
        entranceUtil.deleteRecord(id);
       return ret;
    }

    /**
     * 批量录取
     * @param ids
     * @param recruitStudentsId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public synchronized String admissions(String ids,String recruitStudentsId){
        if(isAdmissionFull(recruitStudentsId)){
            return "超过录取名额，无法录取";
        }
        String idStr[] = ids.split(",");
        List<RecruitStudentsRecordDO> list = new ArrayList<>();
        String re = "";
        for(String id : idStr){
            RecruitStudentsRecordDO recruitStudentsRecordDO = recruitStudentsRecordDao.findOne(id);
            if("1".equals(recruitStudentsRecordDO.getStatus())){
                recruitStudentsRecordDO.setStatus("2");
                list.add(recruitStudentsRecordDO);
            }else{
                re += recruitStudentsRecordDO.getPatientName()+",";
            }

        }
        entranceUtil.updateStatus2(1,idStr);
        recruitStudentsRecordDao.save(list);
        wxMessageUtil.sengdWxTemplates(list);
//        if(re.length()>0){
//            re = re.substring(0,re.length()-2);
//            return "("+ re +")人员状态不是待录取，无法录取，其他人员录取成功";
//        }
        return null;
    }

    /**
     * 批量拒绝
     * @param ids
     */
    public synchronized void refuses(String ids){
        String idStr[] = ids.split(",");
        List<RecruitStudentsRecordDO> list = new ArrayList<>();
        for(String id : idStr){
            RecruitStudentsRecordDO recruitStudentsRecordDO = recruitStudentsRecordDao.findOne(id);
            if("1".equals(recruitStudentsRecordDO.getStatus())){
                recruitStudentsRecordDO.setStatus("4");
                list.add(recruitStudentsRecordDO);
            }
        }
        entranceUtil.deleteRecord2(idStr);
        recruitStudentsRecordDao.save(list);
    }

    /**
     * 是否录取满 true录取满了
     * @param id
     * @return
     */
    public synchronized boolean isAdmissionFull(String id){
        List<RecruitStudentsRecordDO> list = findListById(id);
        RecruitStudentsDO recruitStudentsDO = recruitStudentsDao.findOne(id);
        if(recruitStudentsDO.getNum()<=list.size()){
            return true;
        }
        return false;
    }

    public List<RecruitStudentsRecordDO> findListById(String id){
        String sql = "select * from base_recruit_students_record where recruit_students_id = '"+id+"' and status in ('2','3','6') and del =1 ";
        List<RecruitStudentsRecordDO> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RecruitStudentsRecordDO.class));
        return list;
    }

    /**
     * 录取单个
     * @param id
     * @return
     */
    public synchronized String admission(String id){
        RecruitStudentsRecordDO recruitStudentsRecordDO = recruitStudentsRecordDao.findOne(id);
        if(!"1".equals(recruitStudentsRecordDO.getStatus())){
            return "只有待审核状态的人员才能录取";
        }
        if(isAdmissionFull(recruitStudentsRecordDO.getRecruitStudentsId())){
            return "超过录取名额，无法录取";
        }
        recruitStudentsRecordDO.setStatus("2");
        recruitStudentsRecordDao.save(recruitStudentsRecordDO);
        return null;
    }

    /**
     * 拒绝单个
     * @param id
     */
    public synchronized String refuse(String id){
        RecruitStudentsRecordDO recruitStudentsRecordDO = recruitStudentsRecordDao.findOne(id);
        if(!"1".equals(recruitStudentsRecordDO.getStatus())){
            return "只有待审核状态的人员才能拒绝";
        }
        recruitStudentsRecordDO.setStatus("4");
        recruitStudentsRecordDao.save(recruitStudentsRecordDO);
        return null;
    }



}
