package nckd.jimin.jyyy.hr.hrmp.hrobs.formplugin.portal.pc;

import com.kingdee.util.StringUtils;
import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.db.DB;
import kd.bos.db.DBRoute;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Label;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.hr.hbp.formplugin.web.HRDynamicFormBasePlugin;
import kd.hrmp.hrobs.business.domain.service.portal.IEmpInfoService;
import nckd.jimin.jyyy.hr.hrmp.hbpm.task.SyncUtil;

import java.util.EventObject;
import java.util.Map;

/**
 * description: 员工自助人员信息卡片扩展，携带入职日期，劳动合同开始/结束时间，职级，办公地
 * @author Tyx
 * @date  2025/6/3
 */
public class PcEmpInfoPluginEx extends HRDynamicFormBasePlugin {
    private static final Log LOGGER = LogFactory.getLog(PcEmpInfoPluginEx.class);


    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        Long startTime = System.currentTimeMillis();
        this.initHeadCard();
        Long endTime = System.currentTimeMillis();
        LOGGER.info("PcEmpInfoPluginEx_initHeadCard total time : {}", endTime - startTime);
    }

    private void initHeadCard() {
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        // personInfo格式：{person=2199974856575944704, cmpemp=2199974861768493056, personindexid=2199974856575944704, mid=2199974856584332288, employee=2199974856584332288, user=2199974880760301568}
        Map<String, Long> personInfo = IEmpInfoService.getInstance().getPersonModelId();
        Long personId = personInfo.get("person");
        //获取入职日期，劳动合同签订开始/结束时间，职级，办公地
        StringBuilder sb = SyncUtil.getPersonSqlForHrobs(personId);
        DataSet dataSet = DB.queryDataSet(this.getClass().getName(), DBRoute.of("hr"), sb.toString());
        while(dataSet.hasNext()) {
            Label lblJoinDate = (Label)this.getView().getControl("nckd_joindate");
            Label lblPeriod = (Label)this.getView().getControl("nckd_contractperiod");
            Label lblJobGrade = (Label)this.getView().getControl("nckd_jobgrade");
            Label lblPlace = (Label)this.getView().getControl("nckd_place");
            Row rowData = dataSet.next();
            lblJoinDate.setText(rowData.getString("joinDate"));
            lblJobGrade.setText(rowData.getString("jobLevel"));
            lblPlace.setText(rowData.getString("placeName"));

            String startDate = rowData.getString("startDate");
            String endDate = rowData.getString("endDate");

            //如果结束时间为空 则是无期限
            if(!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
                lblPeriod.setText(startDate + " ~ 无期限");
            }
            else if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
                lblPeriod.setText("未签订");
            }
            else if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
                lblPeriod.setText(startDate + " ~ " + endDate);
            }
        }
    }
}
