package com.engine.hrm.cmd.jobset;

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

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.hrm.entity.RuleCodeType;
import com.engine.hrm.util.CodeRuleManager;
import com.engine.hrm.util.face.HrmFaceCheckManager;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.common.DbFunctionUtil;
import weaver.hrm.common.database.dialect.DialectUtil;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.interfaces.hrm.HrmServiceManager;
import weaver.systeminfo.SysMaintenanceLog;
import weaver.systeminfo.SystemEnv;


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

    private SimpleBizLogger logger;

    public AddJobTitleCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
        this.logger = new SimpleBizLogger();

        String jobtitlemark = Util.null2String(params.get("jobtitlemark"));
        BizLogContext bizLogContext = new BizLogContext();
        bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
        bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_JOBSET);//所属大类型
        bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_JOBSET_JOBTITLE);//当前小类型
        bizLogContext.setParams(params);//当前request请求参数
        logger.setUser(user);//当前操作人
        String mainSql = "select * from hrmjobtitles where jobtitlemark = '" + jobtitlemark + "'";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameColumn("jobtitlemark");//当前targetName对应的列（对应日志中的对象名）
        logger.before(bizLogContext);
    }


    @SuppressWarnings({"deprecation", "unused"})
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> retmap = new HashMap<String, Object>();
        RecordSet RecordSet = new RecordSet();
        String operateIp = Util.null2String(params.get("operateIp"));
        String isBrowser = Util.null2String(params.get("isBrowser"));
        String jobtitlemark = Util.null2String(params.get("jobtitlemark"));
        String remarkMultilingual = Util.formatMultiLang(jobtitlemark,""+user.getLanguage());
        String jobtitlename = Util.null2String(params.get("jobtitlename"));
        String jobactivityid = Util.null2String(params.get("jobactivityid"));
        String jobresponsibility = Util.null2String(params.get("jobresponsibility"));
        String jobcompetency = Util.null2String(params.get("jobcompetency"));
        String jobtitleremark = Util.null2String(params.get("jobtitleremark"));
        String jobdoc = Util.null2String(params.get("jobdoc"));
        String jobtitlecode = Util.null2String(params.get("jobtitlecode"));

        String dept = Util.null2String(params.get("dept"));

        String[] strObj = {jobtitlemark, jobtitlename, jobactivityid,
                Util.null2String(params.get("jobresponsibility")),
                Util.null2String(params.get("jobcompetency")),
                Util.null2String(params.get("jobtitleremark"))};



        try {
            jobtitlecode = CodeRuleManager.getCodeRuleManager().generateRuleCode(RuleCodeType.JOBTITLES, jobtitlecode);
            if (StringUtils.isNotEmpty(jobtitlecode)) {
                RecordSet.executeQuery("select id from hrmjobtitles where jobtitlecode = ? ", jobtitlecode);
                if (RecordSet.getCounts() > 0) {
                    retmap.put("sign", "-1");
                    retmap.put("message", SystemEnv.getHtmlLabelName(524991,weaver.general.Util.getIntValue(user.getLanguage())));
                    return retmap;
                }
            }

            if (!HrmUserVarify.checkUserRight("HrmJobTitlesAdd:Add", user)) {
                retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return retmap;
            }
            String sqlTemp = "select * from hrmjobtitles where ltrim(rtrim(dbo.convToMultiLang(ltrim(rtrim(jobtitlemark))," + user.getLanguage() + ")))= ? and jobactivityid= ? ";
            if (RecordSet.getDBType().equalsIgnoreCase("oracle") || DialectUtil.isMySql(RecordSet.getDBType())) {
                sqlTemp = "select * from hrmjobtitles where ltrim(rtrim(convToMultiLang(ltrim(rtrim(jobtitlemark))," + user.getLanguage() + ")))= ? and jobactivityid= ? ";
            }
//            RecordSet.executeQuery(sqlTemp,remarkMultilingual,jobactivityid);
//            if (RecordSet.next()) {
//                retmap.put("sign", "-1");
//                retmap.put("message", SystemEnv.getHtmlLabelName(10000634,weaver.general.Util.getIntValue(user.getLanguage())));
//                return retmap;
//            }

            char separator = Util.getSeparator();
            String depid = "";
            //no.2583183 满足岗位关联归属部门相关功能改造开发
            String para = jobtitlemark + separator + jobtitlename + separator +
                    depid + separator + jobactivityid + separator +
                    jobresponsibility + separator + jobcompetency + separator +
                    jobtitleremark+ separator+dept;

            if (RecordSet.executeProc("HrmJobTitles_Insert", para)) {
                retmap.put("sign", "1");
                retmap.put("message", SystemEnv.getHtmlLabelName(83551, user.getLanguage()));
            } else {
                retmap.put("sign", "-1");
                retmap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));
                return retmap;
            }
            int id = 0;

            if (RecordSet.next()) {
                id = RecordSet.getInt(1);
                if (!jobdoc.equals(""))
                    RecordSet.executeSql("update HrmJobTitles set jobdoc=" + jobdoc + " where id=" + id);
                RecordSet.executeUpdate("update HrmJobTitles set jobtitlecode = ? where id = ?", jobtitlecode, id);
            }

            //记录创建时间
            String sql = "update HrmJobTitles set " + DbFunctionUtil.getInsertUpdateSetSql(RecordSet.getDBType(), user.getUID()) + " where id=" + id;
            RecordSet.executeUpdate(sql);

            HrmServiceManager HrmServiceManager = new HrmServiceManager();
            HrmServiceManager.SynInstantJobtitle("" + id, "1");

            HrmFaceCheckManager.sync(id + "", HrmFaceCheckManager.getOptInsert(), this.getClass().getName(), HrmFaceCheckManager.getOaJobtitle());

            JobTitlesComInfo JobTitlesComInfo = new JobTitlesComInfo();
            JobTitlesComInfo.removeJobTitlesCache();
            retmap.put("id",id);
            retmap.put("jobtitlemark",jobtitlemark);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return retmap;
    }


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

    @Override
    public List<BizLogContext> getLogContexts() {
        return logger.getBizLogContexts();
    }

}
