package weaver.interfaces.mingfeng.cron;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.*;

public class BirthdayWishesCronJob extends BaseCronJob
{
	/**
	 * 
	 * 计划任务的入口，五任何输入参数
	 */
	@Override
	public void execute()
	{
		updateBirthdayWishes();
	}

	public void updateBirthdayWishes(){

		BaseBean log = new BaseBean();
		log.writeLog("updateBirthdayWishes! ");

		String uftable  = "uf_srzf";
		RecordSet rs = new RecordSet();

		List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
		Map<String,String> dataMap = null;

		BaseBean bb = new BaseBean();

		try{
			SimpleDateFormat sdf = new SimpleDateFormat("MM") ;
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy" ) ;

			String currentMonth = sdf.format(new Date());
			bb.writeLog("currentMonth:"+currentMonth);
			String currentYear = sdf2.format(new Date());
			bb.writeLog("currentYear:"+currentYear);

			String sql =" select h.id,h.lastname,h.sex,h.departmentid,d.departmentname,s.subcompanyname from hrmresource h\n" +
						" inner join HrmDepartment d on d.id = h.departmentid\n" +
						" inner join HrmSubCompany s on s.id = h.subcompanyid1\n" +
						" where right(left(h.birthday,7),2) ='"+currentMonth+"'\n" +
						" and h.status in(0,1,2,3) and h.birthday is not null " +
						" and (h.belongto is null or  h.belongto = -1 ) " ;

			bb.writeLog("sql:"+sql);
			rs.executeQuery(sql);
			while (rs.next()){

				String userid = Util.null2String(rs.getString("id"));
				String lastname = Util.null2String(rs.getString("lastname"));
				String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
				String departmentid = Util.null2String(rs.getString("departmentid"));
				String birthday = Util.null2String(rs.getString("birthday"));
				String sryr = Util.null2String(rs.getString("startday"));
				String zfy = getZfy(lastname);

				dataMap = new HashMap<String, String>();
				dataMap.put("ry", userid);
				dataMap.put("rymc", lastname);
				dataMap.put("ryid", userid);
				dataMap.put("zfy", zfy);
				dataMap.put("bm", departmentid);
				dataMap.put("sr", birthday);
				dataMap.put("fb", subcompanyid1);
				dataMap.put("nf", currentYear);
				dataMap.put("sryr", sryr);

				dataList.add(dataMap);
			}


//			for(int i=0;i<days;i++) {
//				Calendar calendar = Calendar.getInstance();
//				calendar.add(Calendar.DAY_OF_YEAR, i);
//				String currentDay = sdf.format(calendar.getTime());
//				bb.writeLog("currentDay:" + currentDay);
//
//				String currentYear = sdf2.format(calendar.getTime());
//				bb.writeLog("updateBirthdayWishes-currentYear:" + currentYear);
//
//				String sql =" select h.id,h.lastname,h.sex,h.birthday,h.departmentid,h.subcompanyid1,right(h.birthday,5) startday " +
//							" from hrmresource h\n" +
//							" inner join HrmDepartment d on d.id = h.departmentid\n" +
//							" inner join HrmSubCompany s on s.id = h.subcompanyid1\n" +
//							" where birthday like '%" + currentDay + "'\n" +
//							" and h.status in(0,1,2,3) " +
//							" and (h.belongto is null or  h.belongto = -1 ) ";
//
//				bb.writeLog("sql:" + sql);
//				rs.executeQuery(sql);
//				while (rs.next()) {
//					String userid = Util.null2String(rs.getString("id"));
//					String lastname = Util.null2String(rs.getString("lastname"));
//					String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
//					String departmentid = Util.null2String(rs.getString("departmentid"));
//					String birthday = Util.null2String(rs.getString("birthday"));
//					String sryr = Util.null2String(rs.getString("startday"));
//					String zfy = getZfy(lastname);
//
//					dataMap = new HashMap<String, String>();
//					dataMap.put("ry", userid);
//					dataMap.put("rymc", lastname);
//					dataMap.put("ryid", userid);
//					dataMap.put("zfy", zfy);
//					dataMap.put("bm", departmentid);
//					dataMap.put("sr", birthday);
//					dataMap.put("fb", subcompanyid1);
//					dataMap.put("nf", currentYear);
//					dataMap.put("sryr", sryr);
//
//					dataList.add(dataMap);
//
//				}
//
				for (int k = 0; k < dataList.size(); k++) {
					Map<String, String> dMap = dataList.get(k);
					String ry = dMap.get("ry");
					String nf = dMap.get("nf");
					String ufid = "";
					sql = " select id from " + uftable + " where ry='" + ry + "' and nf = '" + nf + "'";
					rs.executeQuery(sql);
					if (rs.next()) {
						ufid = Util.null2String(rs.getString("id"));
					}
					if (StringUtils.isBlank(ufid)) {
						updateBrithdayInfo(uftable, dMap);
					}
				}
//			}

		}catch (Exception e){
			bb.writeLog("AnniversaryWishesCronJob--updateAnniversaryWishes:e"+e);
		}
	}

	/***
	 *
	 * @param fromtable
	 * @param datas
	 * @return
	 */
	public int updateBrithdayInfo(String fromtable,Map<String,String> datas){
		RecordSet rs = new RecordSet();
		BaseBean bb = new BaseBean();
		int count = 0;
		ModeRightInfo modeRightInfo = new ModeRightInfo();
		String uuid = UUID.randomUUID().toString().replace("-","");
		bb.writeLog("uuid:"+uuid);

		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
		String currentdate = formatter1.format(new Date());
		String currenttime = formatter2.format(new Date());
		String modedatacreater = "1";
		String modedatacreatertype = "0";

		String formmodeid = "" ;
		String sql =" select k.id from modeinfo k  \n" +
					" inner join workflow_bill l on formid = l.id\n" +
					" where l.tablename = '"+fromtable+"' " ;
		rs.execute(sql);
		if(rs.next()){
			formmodeid = Util.null2String(rs.getString("id"));
		}

		bb.writeLog("formmodeid:"+formmodeid);
		datas.put("formmodeid", formmodeid);
		datas.put("modedatacreater", modedatacreater);
		datas.put("modedatacreatertype", modedatacreatertype);
		datas.put("modedatacreatedate", currentdate);
		datas.put("modedatacreatetime", currenttime);
		datas.put("uuid", uuid);

		String tablenvalue = "";
		String tablename = "";
		for (Map.Entry<String, String> entry : datas.entrySet()) {
			String fieldname = Util.null2String(entry.getKey()).trim();
			String fieldvalue = Util.null2String(entry.getValue()).replace("'", "''").trim();
			tablename += tablename == "" ? fieldname : "," + fieldname;
			tablenvalue += tablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
		}
		if (tablename != "" && tablenvalue != "") {

			sql = " insert into " + fromtable + "(" + tablename + ") values (" + tablenvalue + ")";
			bb.writeLog("sql:" + sql);
			boolean boo = rs.executeUpdate(sql);
			if (boo) {
				count++;
			}
			bb.writeLog("count:" + count);
			if (count > 0) {
				String dataid = "";
				sql = " select id from " + fromtable + " where uuid='" + uuid + "'";
				bb.writeLog("sql:" + sql);
				rs.execute(sql);
				if (rs.next()) {
					dataid = Util.null2String(rs.getString("id"));
				}
				if (!"".equals(dataid)) {
					modeRightInfo.editModeDataShare(Integer.valueOf(modedatacreater), Util.getIntValue(formmodeid), Integer.parseInt(dataid));
				}
			}
		}

		return count;
	}

	/***
	 *
	 * @param lastname
	 * @return
	 */
	public String getZfy(String lastname){

		String zfy =" <div style=\"width: 500px;font-size: 14px !important;text-align: left;\">\n" +
					"    <p class=\"p1\" style=\"color: #202e7f;font-size: 18px;font-weight: 600;\">亲爱的<span class=\"s1\">"+lastname+"</span>：</p>\n" +
					"    <p class=\"p2\">&nbsp;</p>\n" +
					"    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">时光是一把神奇的钥匙，让每个脚印都坚实而有力量；</p>\n" +
					"    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">岁月是看不见的年轮，让每次付出都收获希望与果实；</p>\n" +
					"    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">感谢您的全力奔跑，感恩您的笃定前行；</p>\n" +
					"    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">今天是您的生日！</p>\n" +
					"    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">在这个没有流星也可以许愿的日子里，祝您生日快乐！</p>\n" +
					"    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">愿您心之所向，光芒万丈，所盼皆所成，所思终所拥！</p>\n" +
					"    <p class=\"p2\">&nbsp;</p>\n" +
					"    <p class=\"p2\">&nbsp;</p>\n" +
					"    <p class=\"p4\" style=\"float: right;color: #202e7f;font-size: 18px;font-weight: 600;\">&nbsp;MATFRON</p>\n" +
					"</div>";
		return zfy;
	}

}
