package com.engine.htsc.duban.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;

public class CreateSubTaskCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> appdate = new HashMap<String, Object>();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        bs.writeLog("CreateSubTaskCmd");
        int userid = user.getUID();
        PropBean pb = new PropBean();
        String modeId = Util.null2String(params.get("modeId"));
        int formmodeid = 0 ;
        if(!"".equals(modeId) && !"null".equals(modeId) && modeId !=null)
        {
            formmodeid = Util.getIntValue(modeId);
        }
        bs.writeLog("formmodeid:"+formmodeid);

        ResourceComInfo resourceComInfo = null;
        try {
            String uuid = UUID.randomUUID().toString();
            String modedatacreater = userid+"";
            String modedatacreatertype = "0";
            String modedatacreatedate = TimeUtil.getCurrentDateString();
            String modedatacreatetime = TimeUtil.getOnlyCurrentTimeString();
            String cjrq = TimeUtil.getCurrentDateString()+" "+TimeUtil.getOnlyCurrentTimeString().substring(0,5);

            String cjzt ="-1";
            String bt = pb.getPropName("DB_ZRW_MC");
            Map<String,String> map = new HashMap<String,String>();

            map.put("formmodeid",formmodeid+"");
            map.put("modedatacreater",modedatacreater+"");
            map.put("modedatacreatertype",modedatacreatertype+"");
            map.put("modedatacreatedate",modedatacreatedate+"");
            map.put("modedatacreatetime",modedatacreatetime+"");
            map.put("modeuuid",uuid+"");

            map.put("cjr",userid+"");
            map.put("gzr",userid+"");
            map.put("cjrq",cjrq);
            map.put("rwzt","0");
            map.put("rwbt",bt);
            map.put("cjzt",cjzt);

            String dttablename = "";
            String dttablenvalue = "";
            for (Map.Entry<String, String> entry : map.entrySet())
            {
                String fieldname = Util.null2String(entry.getKey());
                String fieldvalue = Util.null2String(entry.getValue());
                dttablename += dttablename == "" ? fieldname : "," + fieldname;
                dttablenvalue += dttablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
            }

            String sql = "insert into uf_dbrw("+dttablename+") values("+dttablenvalue+")";
            bs.writeLog("sql:"+sql);
            boolean bool = rs.executeUpdate(sql);
            if(bool){
                rs.executeQuery( "select id from uf_dbrw where modeuuid='" + uuid + "'" );
                if (rs.next()) {
                    int id = Util.getIntValue( rs.getString( "id" ) );
                    bs.writeLog("id:"+id);
                    ModeRightInfo moderight = new ModeRightInfo();
                    moderight.editModeDataShare( userid, formmodeid, id );

                    appdate.put("billid",id);
                    appdate.put("status","1");
                    appdate.put("message","任务创建成功");
                }
            }else{
                appdate.put("billid","0");
                appdate.put("status","-1");
                appdate.put("message","新建任务异常，请联系系统管理员");
            }
        }catch (Exception e){
            appdate.put("billid","0");
            appdate.put("status","-2");
            appdate.put("message","系统错误,请联系系统管理员");
        }
        return appdate;
    }

    public static void main(String[] args) {
//        GCONST.setRootPath("WEB-INF/");
//        GCONST.setServerName("ecology");
//        Map<String, Object> params=new HashMap<>();
//
//        CreateSubTaskCmd htscMhjcCmd=new CreateSubTaskCmd(params);
//        CommandContext ceshi = new CommandContext(null,null);
//        htscMhjcCmd.execute(ceshi);
    }
}
