package weaver.formmode.recruit.modeexpand.interview;

import com.engine.common.util.ServiceUtil;
import com.engine.recruit.entity.workplan.WorkPlanCreateParams;
import com.engine.recruit.service.impl.WorkPlanServiceImpl;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2024/09/24
 * @version: 1.0
 */
public class EditInterviewEvaluateExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> params) {
        Map<String, String> result = new HashMap<>();
        RecordSet rs = new RecordSet();
        try {
            //数据id
            int billId;
            //模块id
            int modeId;
            RequestInfo requestInfo = (RequestInfo) params.get("RequestInfo");
            User user = (User) params.get("user");
            if (requestInfo != null) {
                billId = Util.getIntValue(requestInfo.getRequestid());
                modeId = Util.getIntValue(requestInfo.getWorkflowid());
                if (billId > 0 && modeId > 0) {
                    String creater = requestInfo.getCreatorid();
                    MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
                    Property[] properties = mainTableInfo.getProperty();
                    Map<String, Object> mainDataMap = new HashMap<>(16);
                    for (Property property : properties) {
                        mainDataMap.put(property.getName(), property.getValue());
                    }
                    String msid = Util.null2String(mainDataMap.get("msid"));
                    String jg = Util.null2String(mainDataMap.get("jg"));
                    rs.executeQuery("select * from uf_jcl_mspjfk where msid = ? and id != ? and jg is not null", msid, billId);
                    boolean hasEvaluate = rs.next() || StringUtils.isNotBlank(jg);

                    if (hasEvaluate) {
                        // 更新为已反馈
                        rs.executeUpdate("update uf_jcl_ms set zt = 2 where id = ? ", msid);
                        // 完成关联的日程
                        rs.executeQuery("select glrc from uf_jcl_ms where id = ?", msid);
                        if(rs.next()){
                            String glrc = rs.getString("glrc");
                            if(StringUtils.isNotBlank(glrc)) {
                                WorkPlanCreateParams workPlanCreateParams = new WorkPlanCreateParams();
                                workPlanCreateParams.setWorkId(glrc);
                                workPlanCreateParams.setOperateType("evaluate");
                                workPlanCreateParams.setBillId(msid);
                                ServiceUtil.getService(WorkPlanServiceImpl.class, user).operate(workPlanCreateParams);
                            }
                        }

                    } else {
                        // 更新为未反馈
                        rs.executeUpdate("update uf_jcl_ms set zt = 0 where id = ? ", msid);
                    }

                    //// 发送消息
                    //String messageType = RecruitConstant.INTERVIEW_MESSAGE_TYPE;
                    //String title = RecruitConstant.INTERVIEW_EVALUATE_MESSAGE_TITLE;
                    //
                    //
                    //String ypz = Util.null2String(mainDataMap.get("ypz"));
                    //String ypzw = Util.null2String(mainDataMap.get("ypzw"));
                    //String msfkr = Util.null2String(mainDataMap.get("msfktxr"));
                    //String msrq = Util.null2String(mainDataMap.get("msrq"));
                    //String msg = Util.null2String(mainDataMap.get("msg"));
                    //String ptmsg = Util.null2String(mainDataMap.get("ptmsg"));
                    //String formId = Util.null2String(params.get("formId"));
                    //
                    //StringBuilder msgBuilder = new StringBuilder();
                    //msgBuilder.append(RecruitModeUtil.getResourceNames(msfkr)).append("已经提交了面试评价");
                    //msgBuilder.append("<br/>");
                    //msgBuilder.append("应聘者：").append(ApplicantCommonInfo.getApplicantName(ypz)).append("；应聘职位：").append(ApplicantCommonInfo.getApplicantPosition(ypzw));
                    //msgBuilder.append("<br/>");
                    //msgBuilder.append("面试时间：").append(msrq).append("；面试官：").append(RecruitModeUtil.getResourceNames(msg)).append("，").append(RecruitModeUtil.getResourceNames(ptmsg));
                    //msgBuilder.append("<br/>");
                    //msgBuilder.append("面试结果：").append(ApplicantCommonInfo.getSelectName(formId, "jg", jg));
                    //
                    //
                    //// 消息发送,通知创建人
                    //Set<String> userIdSet = new HashSet<>();
                    //userIdSet.add(creater);
                    //String linkUrl = "spa/cube/index.html#/main/cube/card?billid=" + billId + "&type=0&modeId=" + modeId;
                    //RecruitModeUtil.messagePush(messageType, title, msgBuilder.toString(), userIdSet, user.getUID(), linkUrl, "");


                }
            }
        } catch (Exception e) {
            rs.writeLog(e);
            result.put("errmsg", e.getMessage());
            result.put("flag", "false");
        }

        return result;
    }
}
