package com.engine.organization.util.saveimport;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.hrm.entity.FieldSelectOptionBean;
import com.engine.organization.entity.SelectOptionParam;
import com.engine.organization.entity.extend.po.ExtendInfoPO;
import com.engine.organization.entity.jclimport.po.JclImportHistoryDetailPO;
import com.engine.organization.entity.jclimport.po.JclImportHistoryPO;
import com.engine.organization.mapper.jclimport.JclImportHistoryDetailMapper;
import com.engine.organization.mapper.jclimport.JclImportHistoryMapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import weaver.common.DateUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.definedfield.HrmFieldManager;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2022/09/15
 * @version: 1.0
 */
public class OrgImportUtil {

    public static Long saveImportLog(String importType, String operateType, User user) {
        JclImportHistoryPO historyPO = JclImportHistoryPO.builder().operator((long) user.getUID()).operateTime(DateUtil.getFullDate()).clientAddress(user.getLoginip()).importType(importType).sourceFrom("excel").operateType(operateType).status("importing").build();
        MapperProxyFactory.getProxy(JclImportHistoryMapper.class).insertHistory(historyPO);
        return historyPO.getId();
    }

    public static void saveImportDetailLog(JclImportHistoryDetailPO historyDetailPO) {
        String detailMsg = "导入第" + historyDetailPO.getRowNums() + "行数据,[" + historyDetailPO.getRelatedName() + "]" + historyDetailPO.getOperateDetail();
        historyDetailPO.setOperateDetail(detailMsg);
        MapperProxyFactory.getProxy(JclImportHistoryDetailMapper.class).insertHistoryDetail(historyDetailPO);
    }
    /**
     * 转换存入数据库的值
     */
    public static Object getReallyValue(ExtendInfoPO extendInfo, String cellValue) throws Exception {
        if (StringUtils.isBlank(cellValue)) {
            return null;
        }
        Object object = null;
        JSONArray jsonArray = JSONObject.parseArray(extendInfo.getCustomValue());
        switch (extendInfo.getControlType()) {
            case 1:
                String valueType = (String) jsonArray.get(1);
                // INPUT
                if ("int".equals(valueType)) {
                    object = Integer.parseInt(cellValue);
                } else if ("float".equals(valueType)) {
                    object = Float.parseFloat(cellValue);
                } else {
                    object = cellValue;
                }
                break;
            case 3:
                // BROWSER
                org.json.JSONObject jsonObject = new org.json.JSONObject();
                jsonObject.put("fieldhtmltype", extendInfo.getControlType());
                jsonObject.put("type", extendInfo.getBrowserType());
                jsonObject.put("fieldvalue", cellValue);
                if ("161".equals(extendInfo.getBrowserType()) || "162".equals(extendInfo.getBrowserType())) {
                    jsonObject.put("dmlurl", SelectOptionParam.getCustomBrowserId(extendInfo.getCustomValue()));
                } else {
                    jsonObject.put("dmlurl", extendInfo.getBrowserType());
                }
                jsonObject.put("fieldid", 0);
                object = HrmFieldManager.getReallyFieldvalue(jsonObject);
                break;
            case 5:
                // SELECT
                JSONObject o = (JSONObject) jsonArray.get(2);
                JSONArray datas = o.getJSONArray("datas");
                List<FieldSelectOptionBean> options = datas.toJavaList(FieldSelectOptionBean.class);
                Map<String, String> optionMap = options.stream().collect(Collectors.toMap(FieldSelectOptionBean::getOption, FieldSelectOptionBean::getId, (k1, k2) -> k1));
                object = optionMap.get(cellValue);
                break;
            case 4:// CHECKBOX
            case 6:
                // FILEUPLOAD
                break;
            case 7:// TEXT
            case 2:// TEXTAREA
            default:
                object = cellValue;
                break;
        }
        return object;
    }

    public static String getCellValue(XSSFCell cell) {
        String cellValue = "";
        if (cell == null)
            return "";
        switch (cell.getCellType()) {
            case BOOLEAN: // 得到Boolean对象的方法
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {// 先看是否是日期格式
                    SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
                    cellValue = sft.format(cell.getDateCellValue()); // 读取日期格式
                } else {
                    cellValue = String.valueOf(new Double(cell.getNumericCellValue())); // 读取数字
                    if (cellValue.endsWith(".0"))
                        cellValue = cellValue.substring(0, cellValue.indexOf("."));
                }
                break;
            case FORMULA: // 读取公式
                cellValue = cell.getCellFormula();
                break;
            case STRING: // 读取String
                cellValue = cell.getStringCellValue();
                break;
        }
        cellValue = Util.toHtmlForHrm(cellValue);
        return cellValue;
    }
}
