package com.kingdee.eas.custom.facade.housrapproval;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.base.param.IParamControl;
import com.kingdee.eas.base.param.ParamControlFactory;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.utils.SendUtils;
import com.kingdee.eas.mobile.BOSMsgTypeEnum;
import com.kingdee.eas.mobile.MimeTypeEnum;
import com.kingdee.eas.mobile.PriorityEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;

// 工时提报审批预警
public class HourapprovalFacadeControllerBean extends AbstractHourapprovalFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.facade.housrapproval.HourapprovalFacadeControllerBean");
	@Override
	public void __hourApproval(Context ctx) {

		// LocalDate today = LocalDate.now(); // 获取当前日期
		String sql = "select s.FPERSONUSERID,p.fname_l1 as shenpiName,p.fid,t.fname_l1 as person from T_BAS_AssignRead r left join T_WFR_Assign s on \r\n" + 
				"	r.FASSIGNID = s.FASSIGNID  left join T_PM_User c on c.fid=s.FPERSONUSERID left join T_BD_Person p on c.FPersonId =p.fid\r\n" + 
				"left join (SELECT a.fid,c.fname_l1,c.fid as psid FROM T_HR_SBatchSubmitShemeBill a left join T_HR_SchemeBillEntry b on a.fid=b.fbillid left join T_BD_Person c on b.fpersonid= c.fid )t on t.fid=s.FBIZOBJID \r\n" + 
				"where r.FBOSTYPE = 'B11DBA5C' and s.FBIZOBJID \r\n" + 
				"in(select fid from T_HR_SBatchSubmitShemeBill FBillState where FBillState='1')";
		logger.error("工时提报单审批人SQL：" + sql);
		System.out.println("工时单审批的sql"+sql);
		try {
			// 获取链接
				IParamControl ipc = ParamControlFactory.getLocalInstance(ctx);
				String shrEnvironmentIP = ipc.getParamValue(null, "shrEnvironmentIP");
				String website=	shrEnvironmentIP+"/home.do";
			IRowSet rowSet = DbUtil.executeQuery(ctx, sql);
			// 为了防止重复发送需要去重
			Set<String>personSet=new HashSet<>();
			
			while(rowSet.next()) {
				personSet.add(rowSet.getString("fid")+rowSet.getString("person"));
			}
			System.out.print("personSet的size是"+personSet.size());
			rowSet.beforeFirst();
			while (rowSet.next()) {
				if(personSet.contains(rowSet.getString("fid")+rowSet.getString("person"))) {
					
					StringBuffer strBuffer = new StringBuffer();
					//String title = "Workflow "+rowSet.getString("person")+"Work hour submission approval form awaiting your approval";// 标题
					String title="Reminder to working hours approval";
					// 拼接内容
					strBuffer.append("<html><head></head><body>");
					strBuffer.append("<span>Dear "+rowSet.getString("shenpiName")+","+"</span><br>");
					strBuffer.append("<span></span><br>");
					strBuffer.append("<span>"+"AS "+rowSet.getString("person")+" has submitted the working hours for your approval, could you please login to the system by using below link at your earliest convenience?"+"</span><br>");
					strBuffer.append("<span></span><br>");
					strBuffer.append("<span>");
					strBuffer.append(website);
//					strBuffer.append("<a href=\"https://gtiit.kdeascloud.com/shr/home.do\">https://gtiit.kdeascloud.com/shr/home.do</a>");
					strBuffer.append("</span><br>");
					strBuffer.append("<span></span><br>");
					strBuffer.append("<span>Please kindly check To-Be-Processed in My Tasks and approve/reject by 8th of this month or the DDL required by HR. Late submission will not be accepted. Feel free to let us know if any problem. Many thanks!</span><br>");	
					strBuffer.append("<span></span><br>");
					strBuffer.append("<span>Best regards,</span><br>");
					strBuffer.append("<span>Human Resources Department</span><br>");
					strBuffer.append("</body></html>");
					String content = strBuffer.toString();
					System.out.println("输出是"+strBuffer.toString());
//				MsgUtil.msgSend(ctx, title, PriorityEnum.HIGHT_VALUE, false, content, rowSet.getString("fid"),
//						BOSMsgTypeEnum.V_TYPE_EMAIL, null, null, null);
					SendUtils.msgSend(ctx, title, PriorityEnum.HIGHT_VALUE, false, content, rowSet.getString("fid"),
							BOSMsgTypeEnum.V_TYPE_EMAIL, null, null, MimeTypeEnum.HTML);
					personSet.remove(rowSet.getString("fid")+rowSet.getString("person"));
					System.out.println("发送成功");
				}
			}

		} catch (EASBizException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (BOSException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("工时提报提醒通知失败：" + e.getMessage());
		}

	}

	public static String emailTable() {
		StringBuilder content = new StringBuilder("<html><head></head><body>");
		content.append("<a href=\"https://gtiit.kdeascloud.com/shr\">https://gtiit.kdeascloud.com/shr</a>");
		content.append("</body></html>");
		return content.toString();
	}

}