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 EditJobTitleCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

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

        String id = Util.null2String(params.get("id"));
        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 id in(" + id + ")";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameColumn("jobtitlemark");//当前targetName对应的列（对应日志中的对象名）
        logger.before(bizLogContext);
    }


    @SuppressWarnings("deprecation")
    @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 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 id = Util.null2String(params.get("id"));
        String jobtitlecode = Util.null2String(params.get("jobtitlecode"));
        //no.2583183 满足岗位关联归属部门相关功能改造开发
        String dept = Util.null2String(params.get("dept"));
        try {
//            jobtitlecode = CodeRuleManager.getCodeRuleManager().generateRuleCode(RuleCodeType.JOBTITLES, jobtitlecode);
            if (StringUtils.isNotEmpty(jobtitlecode)) {
                CodeRuleManager.getCodeRuleManager().checkReservedIfDel(RuleCodeType.JOBTITLES.getValue(), jobtitlecode);
            }
            if (StringUtils.isNotEmpty(jobtitlecode)) {
                RecordSet.executeQuery("select id from hrmjobtitles where jobtitlecode = ? and id != ? ", jobtitlecode, id);
                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("HrmJobTitlesEdit:Edit", 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= ? and id not in ( ? ) ";
            if (RecordSet.getDBType().equalsIgnoreCase("oracle") || DialectUtil.isMySql(RecordSet.getDBType())) {
                sqlTemp = "select * from hrmjobtitles where ltrim(rtrim(convToMultiLang(ltrim(rtrim(jobtitlemark))," + user.getLanguage() + ")))= ? and jobactivityid= ? and id not in ( ? ) ";
            }
//            RecordSet.executeQuery(sqlTemp,remarkMultilingual,jobactivityid,id);
//            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();
            //no.2583183 满足岗位关联归属部门相关功能改造开发
            String depid = "";
            String para = "" + id + separator + jobtitlemark + separator + jobtitlename + separator +
                    depid + separator + jobactivityid + separator +
                    jobresponsibility + separator + jobcompetency + separator +
                    jobtitleremark+separator +dept;

            if (RecordSet.executeProc("HrmJobTitles_Update", 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;
            }

            if (!jobdoc.equals("")) {
                RecordSet.executeSql("update HrmJobTitles set jobdoc=" + jobdoc + " where id=" + id);
            } else {
                RecordSet.executeSql("update HrmJobTitles set jobdoc=null where id=" + id);
            }

            // 更新编号
            RecordSet.executeUpdate("update HrmJobTitles set jobtitlecode = ? where id = ?", jobtitlecode, id);
            //记录更新时间
            String sql = "update HrmJobTitles set "+ DbFunctionUtil.getUpdateSetSql(RecordSet.getDBType(),user.getUID())+" where id="+id;
            RecordSet.executeUpdate(sql);

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

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

            JobTitlesComInfo.removeJobTitlesCache();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retmap;
    }

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

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