package com.ym.mec.collectfee.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.mec.collectfee.common.dao.BaseDAO;
import com.ym.mec.collectfee.common.service.impl.BaseServiceImpl;
import com.ym.mec.collectfee.common.sms.SmsExample;
import com.ym.mec.collectfee.dao.ApplyInfoDao;
import com.ym.mec.collectfee.dao.SchoolDao;
import com.ym.mec.collectfee.entity.ApplyInfo;
import com.ym.mec.collectfee.entity.Course;
import com.ym.mec.collectfee.entity.ResponseCourseEntity;
import com.ym.mec.collectfee.entity.School;
import com.ym.mec.collectfee.service.SchoolService;
import com.ym.mec.collectfee.utils.Constants;
import com.ym.mec.collectfee.utils.ShortUrlUtil;
import com.ym.mec.collectfee.utils.yqpay.DateUtils;

@Service
public class SchoolServiceImpl extends BaseServiceImpl<Integer, School> implements SchoolService {
	
	@Autowired
	private SchoolDao schoolDao;
	@Autowired
	private ApplyInfoDao applyInfoDao;

	@Value("${common.properties.sms-appId}")
	private String appId;
	@Value("${common.properties.sms-secretKey}")
	private String secretKey;
	@Value("${common.properties.sms-host-dev}")
	private String host;
	@Value("${common.properties.sms-send-request-url}")
	private String sendUrl;
	
	// 加密算法
	private String algorithm = "AES/ECB/PKCS5Padding";
	// 编码
	private String encode = "UTF-8";
	// 是否压缩
	private boolean isGizp = true;

	@Override
	public BaseDAO<Integer, School> getDAO() {
		return schoolDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public School upsetSchool(ResponseCourseEntity courseEntity) {
		List<Course> courses = courseEntity.getCourses();
		if(courses != null && courses.size() > 0){
			Course course = courses.get(0);
			School school = schoolDao.get(course.getClassId());
			Date date = new Date();
			if(school == null){
				school = new School();
				school.setBranchId(course.getBranchId());
				school.setName(course.getClassName());
				school.setId(course.getClassId());
				school.setStatus(course.getStatus());
				school.setCreateTime(date);
				school.setUpdateTime(date);
				schoolDao.insert(school);
			}
			return school;
		}
		return null;
	}

	@Override
	public void openClassPay(Integer classId,String smsMsg,int type,int isSingle) {
		School school = schoolDao.get(classId);
		if(school != null && school.getStatus() == 1){
			school.setUpdateTime(new Date());
			school.setStatus(2);
			school.setIsSingle(isSingle);
			schoolDao.update(school);
			//发送短信
			//获取所有用户手机号列表
			List<ApplyInfo> users = applyInfoDao.findUserByClass(classId,null);
			String url = null;
			if(users != null){
				for(ApplyInfo info : users){
					url = sendUrl + "branchId=" + info.getBranchId() + "&stuId=" + info.getId() + "&classId=" + info.getClassId();
					if(type == 1){
						smsMsg = String.format(Constants.SMS_TEMPLATE, info.getName(),DateUtils.getDayForAfter(new Date(), 2), url);
					}
					SmsExample.setSingleSms(appId,secretKey,host,algorithm,
							smsMsg,
							null,null,info.getPatriarchPhone(),isGizp,encode);
				}
			}
		}
	}

	@Override
	public void sendPayMsg(String mobile,String amount){
		SmsExample.setSingleSms(appId,secretKey,host,algorithm,
				String.format(Constants.PAY_SMS_TEMPLATE,amount),null,null, mobile,isGizp,encode);
	}
	
//	public static void main(String[] args) {
//		System.out.println(String.format(Constants.SMS_TEMPLATE, DateUtils.getDayForAfter(new Date(), 2)));
//	}
}