package weaver.formmode.recruit.modeexpand.entrymanager;

import cn.hutool.core.convert.Convert;
import com.engine.recruit.system.ResourceSyncUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 创建OA账号
 *
 * @author:dxfeng
 * @createTime: 2023/10/25
 * @version: 1.0
 */
public class CreateOaModeExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<>();
        try {
            int billId;
            int modeId;
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billId = Util.getIntValue(requestInfo.getRequestid());
                modeId = Util.getIntValue(requestInfo.getWorkflowid());
                if (billId > 0 && modeId > 0) {
                    MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
                    Property[] properties = mainTableInfo.getProperty();
                    Map<String, Object> mainDataMap = new HashMap<>();
                    for (Property property : properties) {
                        mainDataMap.put(property.getName(), property.getValue());
                    }
                    Map<String, Object> resourceParams = ResourceSyncUtil.convertEcResourceParams(mainDataMap);
                    User user = new User(1);
                    Map<String, Object> returnMap = ResourceSyncUtil.addResource(user, resourceParams);
                    if ("1".equals(returnMap.get("status"))) {
                        // 更新为已入职
                        RecordSet rs = new RecordSet();
                        rs.executeUpdate("update uf_jcl_rzgl set rzzt = 1 where id = ?", billId);

                        // 入职关联招聘需求的入职人数+1
                        String glzpxq = Util.null2String(mainDataMap.get("glzpxq"));
                        if (StringUtils.isNotBlank(glzpxq)) {
                            rs.executeQuery("select rzrs,zprs from uf_jcl_zp_zpxq where id = ? ", glzpxq);
                            if (rs.next()) {
                                int rzrs = Convert.toInt(rs.getString("rzrs"), 0) + 1;
                                int zprs = rs.getInt("zprs");
                                BigDecimal xqzhjd = new BigDecimal("0.00");
                                if (zprs > 0) {
                                    xqzhjd = new BigDecimal(rzrs).divide(new BigDecimal(zprs), 2, RoundingMode.HALF_DOWN);
                                }
                                rs.executeUpdate("update uf_jcl_zp_zpxq set rzrs = ?,xqzhjd = ? where id = ? ", rzrs, xqzhjd, glzpxq);
                            }
                        }

                    } else {
                        result.put("errmsg", returnMap.get("message").toString());
                        result.put("flag", "false");
                    }
                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog(e);
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}
