package cn.hmsoft.skill.enrol.service;

import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.pay.data.model.PayBill;
import cn.hmsoft.pay.service.PayAbstractService;
import cn.hmsoft.skill.constants.StdLogType;
import cn.hmsoft.skill.data.dao.std.StdEnrolDao;
import cn.hmsoft.skill.data.dao.std.StdLogDao;
import cn.hmsoft.skill.data.model.school.SchoolAspect;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdEnrol;
import cn.hmsoft.skill.data.model.std.StdReg;

@Service
public class EnrolPayService extends PayAbstractService {
	@Autowired
	private StdEnrolDao daoEnrol;
	@Autowired
	private StdLogDao daoLog;

	@Override
	public boolean pay(PayBill bill) {
		StdEnrol enrol = this.daoEnrol.find(bill.getBusiness_id());
		if (enrol == null)
			return false;
		if (FrameStatus.Active.toString().equals(enrol.getPay_status()))
			return true;
		boolean flag =  this.daoEnrol.payEnrol(bill, enrol.getEnrol_id());
		if(flag) {
			//支付成功,
			SchoolAspect aspect = this.daoEnrol.find(SchoolAspect.class,enrol.getAspect_id());
			this.daoLog.log(enrol.getSchool_id(), enrol.getStd_id(), StdLogType.Pay, LocalDateTime.now(), null, 
					aspect.getAspect_id() + "", aspect.getAspect_name()+" -> " + enrol.getFee_amount(), LocalDateTime.now());
		}
		return flag;

	}

	/*@Override         TODO....
	public boolean check(String business_id) {
		try {
			return check(Integer.parseInt(business_id));
		} catch (Exception e) {
			return false;
		}
	}*/

	/******************
	 * 简单判断是否已经缴费
	 * 
	 * @param enrol_id
	 * @return
	 */
	public boolean check(int enrol_id) {
		StdEnrol enrol = this.daoEnrol.find(enrol_id);
		if (enrol == null || !FrameStatus.Active.toString().equals(enrol.getPay_status()))
			return false;
		return true;
	}

	public StdEnrol checkPay(StdReg std, int enrol_id) {
		StdEnrol enrol = this.daoEnrol.find(enrol_id);
		FrameAssertUtil.isNotNull(enrol, "未找到需要缴费的报考信息");
		FrameAssertUtil.isEqual(FrameStatus.Active.toString(), enrol.getEnrol_status(), "此报考项目已经取消，请勿进行缴费操作");
		FrameAssertUtil.isEqual(enrol.getStd_id(), std.getStd_id(), "不是当前考生的报考项目，请重新登陆");
		LocalDateTime now = LocalDateTime.now();
		// 判断是否已经缴费
		FrameAssertUtil.isNotEqual(FrameStatus.Active.toString(), enrol.getPay_status(), "你已经完成该报考的缴费操作");
		// 判断是否有单独时间限制
		if (enrol.getPay_end_time() != null ) {
			// 有单独时间限制，可能是特殊考生延长缴费时间
			if(enrol.getPay_end_time().isBefore(now))
				throw new BusinessException("超出报考时限，缴费通道已经关闭！");
		} else {
			// 没有单独时间限制，去找考点结束报名时间
			SchoolReg school = this.daoEnrol.find(SchoolReg.class, "school_id", enrol.getSchool_id());
			FrameAssertUtil.isNotNull(school, "未找到选中的考点信息，请联系招办");
			if(school.getEnrol_end_time()==null || school.getEnrol_end_time().isBefore(now))
				throw new BusinessException("超出报考时限，缴费通道已经关闭！");
		}
		return enrol;
	}
	
}
