package com.engine.htsc.smsd.dao;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataIsAddCmd extends AbstractCommonCommand<Map<String, Object>> {
    Logger logger = LoggerFactory.getLogger(DataIsAddCmd.class);

    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public DataIsAddCmd(Map<String, Object> params){
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        logger.info("进入CMD层，获取的参数是："+ JSONObject.toJSONString(params));
        Map<String, Object> result = new HashMap<>();
        try {
            String bmid = Util.null2String(params.get("bmid"));
            String lclx = Util.null2String(params.get("lclx"));
            Map<String, Object> okNeed = dataCanAdd(bmid,lclx);
            if ("true".equals(okNeed.get("status"))){
                result.put("isSuccess","true");
                result.put("message","true");
            }else {
                result.put("isSuccess","false");
                result.put("message",okNeed.get("message"));
            }
        }catch (Exception e){
            result.put("isSuccess","false");
            result.put("message",e.getMessage());
        }
        return result;
    }

    private Map<String, Object> dataCanAdd(String bmid, String lclx) {
        logger.info("==========进入查询的是否数据可以添加的方法==============");
        String isExist = "";
        List<String> hasExitBmmc = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql = "SELECT wm_concat_old(bmmc) as allbmmc from uf_CodeScanRec where  LCLX = ?";
        rs.executeQuery(sql,lclx);
        if (rs.next()){
            isExist = Util.null2String(rs.getString("allbmmc"));
        }
        if ("".equals(isExist)){
            result.put("status","true");
        }else {
            for (String oneBmid : bmid.split(",")) {
                if (isExist.contains(oneBmid)){
                    sql = "select DEPARTMENTNAME from HRMDEPARTMENT where id = ?";
                    rs.executeQuery(sql,oneBmid);
                    if (rs.next()){
                        hasExitBmmc.add(rs.getString("DEPARTMENTNAME"));
                    }
                }
            }
            if (hasExitBmmc.size() == 0){
                result.put("status","true");
            }else {
                result.put("status","false");
                result.put("message", StringUtils.join(hasExitBmmc,",")+"已添加到配置表");
            }
        }
        return result;
    }
}
