package com.engine.hrm.cmd.jobset;

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

import com.api.browser.bean.SearchConditionItem;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;

import weaver.conn.RecordSet;
import weaver.docs.docs.DocComInfo;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.job.JobActivitiesComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.systeminfo.SystemEnv;


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


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


    @Override
    public BizLogContext getLogContext() {
        return null;
    }


    @SuppressWarnings("deprecation")
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> retmap = new HashMap<String, Object>();
        List<Object> lsGroup = new ArrayList<Object>();
        Map<String, Object> groupitem = null;
        List<Object> itemlist = null;
        boolean cansave = HrmUserVarify.checkUserRight("HrmJobTitlesAdd:Add", user);
        boolean canedit = HrmUserVarify.checkUserRight("HrmJobTitlesEdit:Edit", user);
        String jobtitleid = Util.null2String(params.get("jobtitleid"));
        String jobactivityid = Util.null2String(params.get("jobactivityid"));
        String type = Util.null2String(params.get("type"));
        String id = Util.null2String(params.get("id"));

        RecordSet rst = new RecordSet();
        if ("JobTitle".equals(type)) {
            rst.executeSql("select jobactivityid from  hrmjobtitles where  id = " + id + "");
            if (rst.next()) {
                jobactivityid = rst.getString("jobactivityid");
            }else{
                jobactivityid = "";
            }
        }

        jobactivityid = jobactivityid.indexOf("__") > -1 ? jobactivityid.split("__")[1] : jobactivityid;
        boolean onlyView = false;
        if (jobtitleid.length() == 0) {

            if (!cansave) {
                /*retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return retmap;*/
                onlyView = true;
            }
        } else {

            if (!canedit) {
                /*retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return retmap;*/
                onlyView = true;
            }
        }

        List<Map<String, Object>> replaceDatas = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> replaceDatas2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> datas = new HashMap<String, Object>();

        String canceled =null;
        RecordSet rs = new RecordSet();
        boolean hasFieldvalue = false;
        if (jobtitleid.length() != 0) {
            rs.executeSql("select * from hrmjobtitles  t1  left join hrmjobActivities  t2    on t1.jobactivityid = t2.id  where t1.id =" + jobtitleid + " ");
            if (rs.next()) {
                hasFieldvalue = true;
                jobactivityid = rs.getString("jobactivityid");
                //已封存的岗位不可以编辑，解封才可以
                canceled =rs.getString("canceled");
            }
        }

        if("1".equals(canceled)){
            onlyView = true;
        }
        if (!jobactivityid.equals("")) {
            try {
                JobActivitiesComInfo coun = new JobActivitiesComInfo();
                String jobactivityname = coun.getJobActivitiesmarks(jobactivityid);
                datas.put("id", jobactivityid);
                datas.put("name", jobactivityname);
                replaceDatas.add(datas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (!jobtitleid.equals("")) {
            try {
                datas = new HashMap<String, Object>();
                DocComInfo dComInfo = new DocComInfo();
                JobTitlesComInfo con = new JobTitlesComInfo();
                String docid = con.getJobdoc(jobtitleid);
                String docname = dComInfo.getDocname(docid);
                datas.put("id", docid);
                datas.put("name", docname);
                replaceDatas2.add(datas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //no.2583183 满足岗位关联归属部门相关功能改造开发
        String[] fields = new String[]{"jobtitlemark,382413,1,1", "jobtitlename,382414,1,1", "jobtitlecode,524944,1,1", "jobactivityid,15855,3,282", "jobresponsibility,15856,2,1", "jobdoc,857,3,9", "jobcompetency,895,2,1", "jobtitleremark,454,2,1", "dept,541143,3,4"};
        HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
        SearchConditionItem searchConditionItem = null;
        HrmFieldBean hrmFieldBean = null;
        itemlist = new ArrayList<Object>();
        groupitem = new HashMap<String, Object>();
        if (jobtitleid.length() != 0) {
            groupitem.put("title", SystemEnv.getHtmlLabelName(386216,weaver.general.Util.getIntValue(user.getLanguage())));
        } else {
            groupitem.put("title", SystemEnv.getHtmlLabelName(125302,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        groupitem.put("defaultshow", true);
        for (int i = 0; i < fields.length; i++) {
            String[] fieldinfo = fields[i].split(",");
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(fieldinfo[0]);
            hrmFieldBean.setFieldlabel(fieldinfo[1]);
            hrmFieldBean.setFieldhtmltype(fieldinfo[2]);
            hrmFieldBean.setType(fieldinfo[3]);
            if (!fieldinfo[0].equals("jobactivityid") && !fieldinfo[0].equals("jobdoc")) {
                if(hasFieldvalue){
                    String jobtitlemark = Util.null2String(rs.getString(fieldinfo[0]));
                    hrmFieldBean.setFieldvalue(jobtitlemark);
                    if("1".equals(canceled)&&"jobtitlemark".equals(fieldinfo[0])){
                        if(onlyView){
                            hrmFieldBean.setFieldvalue(jobtitlemark+"<span style=\"color:#FF0000\">（已封存）</span>");
                        }else{
                            hrmFieldBean.setFieldvalue(jobtitlemark+"（已封存）");
                        }
                    }
                }else{
                    hrmFieldBean.setFieldvalue("");
                }

            }
            hrmFieldBean.setIsFormField(true);
            if (fieldinfo[0].equals("jobresponsibility") || fieldinfo[0].equals("jobcompetency") || fieldinfo[0].equals("jobtitleremark")) {

            }
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            if ((fieldinfo[0].equals("jobactivityid"))) {
                searchConditionItem.getBrowserConditionParam().setReplaceDatas(replaceDatas);
                if (HrmUserVarify.checkUserRight("HrmJobActivitiesAdd:Add", user)) {
                    searchConditionItem.getBrowserConditionParam().setHasAddBtn(true);
                    searchConditionItem.getBrowserConditionParam().setAddOnClick("addOnClick");
                    searchConditionItem.getBrowserConditionParam().set_callbackForAdd("");
                }
            }
            if (fieldinfo[0].equals("jobresponsibility") || fieldinfo[0].equals("jobcompetency") || fieldinfo[0].equals("jobtitleremark")) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("maxRows", 2);
                searchConditionItem.setOtherParams(map);
                searchConditionItem.setValue(hasFieldvalue ? Util.null2String(rs.getString(fieldinfo[0])) : "");
            }

            if ((fieldinfo[0].equals("jobdoc")) && hasFieldvalue) {
                searchConditionItem.getBrowserConditionParam().setReplaceDatas(replaceDatas2);
            }

            searchConditionItem.setLabelcol(6);
            searchConditionItem.setFieldcol(18);

            if (hrmFieldBean.getFieldname().equals("jobtitlemark") || hrmFieldBean.getFieldname().equals("jobtitlename") || hrmFieldBean.getFieldname().equals("jobactivityid") || hrmFieldBean.getFieldname().equals("dept")) {
                searchConditionItem.setViewAttr(3);
                searchConditionItem.setRules("required|string");
            }
            if(onlyView){
                searchConditionItem.setViewAttr(1);
            }
            itemlist.add(searchConditionItem);
        }
        groupitem.put("items", itemlist);
        lsGroup.add(groupitem);
        retmap.put("condition", lsGroup);
        retmap.put("hasSaveBtn",!onlyView);
        return retmap;
    }
}
