package com.engine.hrm.cmd.jobset;

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

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocComInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.systeminfo.SystemEnv;

public class GetJobDetailCmd extends AbstractCommonCommand<Map<String, Object>> {

    public static BaseBean logger;

    public GetJobDetailCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        // TODO Auto-generated method stub
        return null;
    }

    @SuppressWarnings("deprecation")
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> retmap = new HashMap<String, Object>();
        Map<String, Object> datas = null;
        String sql = "";
        RecordSet rs = new RecordSet();
        List<Object> list = new ArrayList<Object>();
        String jobGroupid = Util.null2String(params.get("jobGroupid"));
        String jobActivityid = Util.null2String(params.get("jobActivityid"));
        String jobTitleid = Util.null2String(params.get("jobTitleid"));
        String table = "";
        String supId = "";

        table = (jobGroupid.length() > 0) ? "hrmjobGroups" : (jobActivityid.length() > 0) ? "hrmjobActivities t1" : (jobTitleid.length() > 0) ? "hrmjobtitles  t1 " : "";
        if (!(table.length() > 0)) {
            retmap.put("datas", list);
            return retmap;
        }

        sql = "select  * from  " + table + " ";
        if (!jobGroupid.equals("")) {
            sql += "  where id =" + jobGroupid + " ";
            rs.executeSql(sql);
            if (rs.first()) {
                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(382407,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString(3));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(382408,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString(2));
                list.add(datas);
            }
        } else if (!jobActivityid.equals("")) {
            sql += " left join hrmjobGroups  t2 on t1.jobgroupid = t2.id  where t1.id =" + jobActivityid + " ";
            rs.executeSql(sql);
            if (rs.first()) {
                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(382410,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString(2));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(382409,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString(3));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(805,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString("jobgroupremark"));
                list.add(datas);

                supId = rs.getString("jobgroupid");
            }
        } else if (!jobTitleid.equals("")) {
            sql += " left join hrmjobActivities  t2    on t1.jobactivityid = t2.id  where t1.id =" + jobTitleid + " ";
            rs.executeSql(sql);
            if (rs.first()) {
                String canceled  = rs.getString("canceled");
                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(382413,weaver.general.Util.getIntValue(user.getLanguage())));
                String isCanceled = SystemEnv.getHtmlLabelName(22205,weaver.general.Util.getIntValue(user.getLanguage()));
                if("1".equals(canceled)){
                    datas.put("value", rs.getString(2)+"<span style=\"color:#FF0000\">（"+isCanceled+"）</span>");
                }else{
                    datas.put("value", rs.getString(2));
                }

                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(382414,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString(3));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(524944,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString("jobtitlecode"));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(15855,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString("jobactivitymark"));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(15856,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString("jobresponsibility"));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(857,weaver.general.Util.getIntValue(user.getLanguage())));
                DocComInfo dComInfo = new DocComInfo();
                if (rs.getString("jobdoc").length() != 0) {
                    datas.put("value", dComInfo.getDocname(rs.getString("jobdoc")));
                    datas.put("link", "/spa/document/index.jsp?id=" + rs.getString("jobdoc") + "");
                }
                list.add(datas);
                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(895,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString("jobcompetency"));
                list.add(datas);

                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(454,weaver.general.Util.getIntValue(user.getLanguage())));
                datas.put("value", rs.getString("jobtitleremark"));
                list.add(datas);

                //no.2583183 满足岗位关联归属部门相关功能改造开发
                datas = new HashMap<String, Object>();
                datas.put("label", SystemEnv.getHtmlLabelName(541143,weaver.general.Util.getIntValue(user.getLanguage())));
                DepartmentComInfo departmentComInfo = new DepartmentComInfo();
                String deptStr = rs.getString("dept");
                String deptName="";
                if(deptStr != null){
                    if(deptStr.contains(",")){
                        String[] deptIdArr = deptStr.split(",");
                        for(String deptId : deptIdArr){
                            try{
                                String name = departmentComInfo.getDepartmentName(deptId);
                                deptName +=","+ name;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        deptName = deptName.substring(1,deptName.length()-1);
                    }else{
                        try{
                            String name = departmentComInfo.getDepartmentName(deptStr);
                            deptName += name;
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }

                datas.put("value", deptName);
                list.add(datas);

                supId = rs.getString("jobactivityid");
            }
        }

        retmap.put("superNodeId",supId);
        retmap.put("datas", list);
        return retmap;
    }





}
