package com.engine.recruit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.recruit.exception.CustomizeRunTimeException;
import com.engine.recruit.service.RecruitOfferService;
import com.weaver.formmodel.data.model.Formfield;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.recruit.modeexpand.offer.CreateOfferModeExpand;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2024/05/20
 * @version: 1.0
 */
public class RecruitOfferServiceImpl extends Service implements RecruitOfferService {
    @Override
    public Map<String, Object> updatePreContent(Map<String, Object> param) {
        String urlInfoStr = Util.null2String(param.get("urlInfo"));
        String submitDataStr = Util.null2String(param.get("submitData"));
        JSONObject urlInfo = JSONObject.parseObject(urlInfoStr);
        JSONObject submitData = JSONObject.parseObject(submitDataStr);
        String billId = Util.null2String(urlInfo.get("billid"));
        List<Formfield> fieldList = RecruitModeUtil.getFieldList("uf_jcl_offer");
        Map<String, String> fieldIdMap = fieldList.stream().collect(Collectors.toMap(item -> "field" + item.getId()
                , Formfield::getFieldname));
        Map<String, Object> mainDataMap = new HashMap<>();
        Map<String,Object> innerMap = submitData.getInnerMap();
        for (Map.Entry<String,Object> entry : innerMap.entrySet()) {
            String key = entry.getKey();
            String value = Util.null2String(entry.getValue());
            mainDataMap.put(fieldIdMap.get(key), value);
        }
        String tznr = Util.null2String(mainDataMap.get("tznr"));
        String yx = Util.null2String(mainDataMap.get("dzyx"));
        String msgContent = CreateOfferModeExpand.getOfferMsgContent(billId, fieldList, mainDataMap, tznr, yx);

        RecordSet rs = new RecordSet();
        // 更新
        rs.executeUpdate("update uf_jcl_offer set ylnr = ? where id = ? ", msgContent, billId);
        String exceptionMsg = rs.getExceptionMsg();
        if (StringUtils.isNotBlank(exceptionMsg)) {
            throw new CustomizeRunTimeException(exceptionMsg);
        }
        return null;
    }
}
