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

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
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;

/**
 * 个人兼职提报提醒
 *
 * @author ISSUSER
 *
 */
public class WorkhoursFacadeControllerBean extends AbstractWorkhoursFacadeControllerBean {
    private static Logger logger = Logger
            .getLogger("com.kingdee.eas.custom.facade.houtsrepot.WorkhoursFacadeControllerBean");

    @Override
    public void __workHour(Context ctx) {

        try {
            // 获取链接
            IParamControl ipc = ParamControlFactory.getLocalInstance(ctx);
            String shrEnvironmentIP = ipc.getParamValue(null, "shrEnvironmentIP");
            String website = shrEnvironmentIP + "/home.do";
            IRowSet rowSet = getRowset(ctx);
            Set<String> personSet = new HashSet();
            // 获取日期名称
            String fname = getFname(ctx);
            LocalDate currentDate = LocalDate.now();
            // 格式化为英文月份
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH);
            //获取这个月的英文月份
            String thisMonthName = currentDate.format(formatter);
            //获取上个月的英文月份
            LocalDate previousMonthDate = currentDate.minusMonths(1);
            String LastMonthName = previousMonthDate.format(formatter);
            int count =0;
            while (rowSet.next()) {

                if (!personSet.contains(rowSet.getString("fid"))) {
                    count++;
                    String title = "Working hours collection";// 标题
                    StringBuilder content = new StringBuilder("<html><head></head><body>");
                    content.append("<span>Dear " + rowSet.getString("perosnnaem") + ":" + "</span><br>");
                    content.append("<span></span><br>");
                    content.append(
                            "<span>This is a kind reminder to submit your working hours in "+LastMonthName+" <strong><u> by "+thisMonthName+" "+fname+".</u></strong> Please login to the system by below link and submit your working hours on time.</span><br>");
                    content.append("<span></span><br>");
                    // content.append("<span><a
                    // href=\"https://gtiit.kdeascloud.com/shr\">https://gtiit.kdeascloud.com/shr</a></span><br>");
                    content.append("<span><a href=\""+website+"\">"+website+"</a></span><br>");
                    content.append("<span></span><br>");
                    content.append(
                            "<span>Please remind that the working hours collection is once a month. Late submission will not be accepted.</span><br>");
                    content.append("<span></span><br>");
                    content.append(
                            "<span>Those who have no hours to submit please disregard this reminder. Please feel free to contact us if you have any problems.Thanks.</span><br>");
                    content.append("<span></span><br>");
                    content.append("<span>Best regards,</span><br>");
                    content.append("<span>Human Resources Department</span><br>");
                    content.append("</body></html>");
                    SendUtils.msgSend(ctx, title, PriorityEnum.HIGHT_VALUE, false, content.toString(),
                            rowSet.getString("fid"), BOSMsgTypeEnum.V_TYPE_EMAIL, null, null, MimeTypeEnum.HTML);
                    personSet.add(rowSet.getString("fid"));
                    logger.error("我的邮件收件人的fid是"+rowSet.getString("fid")+"...."+Integer.toString(count));
                }
            }

        } catch (EASBizException e) {

            e.printStackTrace();
        } catch (SQLException e) {

            e.printStackTrace();
        } catch (BOSException e) {
            logger.error("个人提报兼职工时申请提醒通知失败：" + e.getMessage());
            e.printStackTrace();
        }catch(Exception e) {
            logger.info("个人提报兼职工时申请提醒通知失败： ",e);
        }

    }

    // 查询任职类型是兼职 当天时期在失效和开始日期之间 时薪大于0
    public IRowSet getRowset(Context ctx) {
        // 获取当前日期
        Date nowdate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String stringDate = sdf.format(nowdate);
        String sql = "select a.FName_L1 as perosnnaem,e.FName_L1 as postonName,a.fid from T_BD_Person a  left \r\n"
                + "join T_HR_EMPORGRELATION c on a.fid=c.FPERSONID\r\n"
                + "left join T_ORG_Position e on e.fid=c.fpositionid\r\n"
                //新增用工关系判断,不占人头的员工不发送邮件
                + "left join T_HR_EMPLABORRELATIONHIS ehis on ehis.fpersonid=a.fid and ehis.feffdt<='" + stringDate + "' and ehis.fleffdt >='" + stringDate + "'\r\n"
                + "left join T_HR_BDEmployeeType type on ehis.flaborrelationstateid = type.fid\r\n"
                + "where  c.CFHourlywage>'0' and c.feffdt<='" + stringDate + "' and c.fleffdt>='" + stringDate + "' and type.FISINCOUNT =1 ";
        IRowSet rowSet = null;
        try {
            rowSet = DbUtil.executeQuery(ctx, sql);
        } catch (BOSException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return rowSet;

    }

    /**
     * 薪酬预留基础数据的名称
     *
     * @return
     */
    public String getFname(Context ctx) {
        String sql = "SELECT FName_l1 as fname FROM T_HR_SHRRsvItem0 where FNumber='1004'";
        String fname = null;
        try {
            IRowSet rs = DbUtil.executeQuery(ctx, sql);
            while (rs.next()) {
                fname = rs.getString("fname");
            }
        } catch (BOSException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return fname;
    }

}