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 weaver.interfaces.htsc.util.HT_Util;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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

    public CreateTaskCmd(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>();
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        bs.writeLog("CreateTaskCmd");
        int userid = user.getUID();
        int formmodeid = Util.getIntValue(Util.null2String(params.get("modeId")));

        ResourceComInfo resourceComInfo = null;
        try {

            resourceComInfo = new ResourceComInfo();
            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";
            PropBean pb = new PropBean();
            String bt =pb.getPropName("DB_RW_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("cjzt",cjzt+"");
            map.put("bt",bt+"");
            map.put("modeuuid",uuid+"");

            HT_Util ht_util = new HT_Util();
            map.put("cjr",userid+"");
            map.put("tzydr",userid+"");
            map.put("szbm",resourceComInfo.getDepartmentID(userid+""));
            map.put("szjg",ht_util.getszjg(userid+""));
            map.put("cjrq",cjrq);
            map.put("xzzt","0");

            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_xzsx("+dttablename+") values("+dttablenvalue+")";
            bs.writeLog("sql:"+sql);
            boolean bool = rs.executeUpdate(sql);
            if(bool){
                rs.executeQuery( "select id from uf_xzsx 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;
    }
}
