package com.engine.zjrb.pattern.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> returnMap = new HashMap<String,Object>();
        BaseBean bb = new BaseBean();
        int userid = user.getUID();

        int departmentid = user.getUserDepartment();
        int subcompanyid = user.getUserSubCompany1();
        bb.writeLog("departmentid:"+departmentid);
        bb.writeLog("subcompanyid:"+subcompanyid);

        RecordSet rs = new RecordSet();
        boolean data = false;

        String uftable = "uf_pattern";

        String patternvalue = Util.null2String(params.get("patternvalue"));
        try{
            String id = "";
            String sql = " select id from "+uftable+" where ry = ? " ;
            rs.executeQuery(sql,new Object[]{userid});
            if(rs.next()){
                id = Util.null2String(rs.getString("id"));
            }
            bb.writeLog("id:"+id);

            if(StringUtils.isNotBlank(id)){
                sql = " update "+uftable+" set taz = ?,ryszbm=?,ryszfb=? where id = ?";
                boolean isTrue = rs.executeUpdate(sql,new Object[]{patternvalue,departmentid,subcompanyid,id});
                if(isTrue){
                    data = true;
                }
            }else{

                ModeRightInfo modeRightInfo = new ModeRightInfo();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");//当前时间日期
                SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");

                int formmodeid = 0 ;
                String modedatacreater = "1" ;
                String modedatacreatertype = "0" ;
                String modedatacreatedate = sdf1.format(new Date());
                String modedatacreatetime = sdf2.format(new Date());
                String uuid = UUID.randomUUID().toString();

                sql =   " select k.id from modeinfo k  \n" +
                        " inner join workflow_bill l on formid = l.id\n" +
                        " where l.tablename = ? " ;
                rs.executeQuery(sql,new Object[]{uftable});
                if(rs.next()){
                    formmodeid = Util.getIntValue(Util.null2String(rs.getString("id")));
                }

                sql =   " insert into "+uftable+"(uuid,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,ry,taz,ryszbm,ryszfb)" +
                        " values(?,?,?,?,?,?,?,?,?,?)" ;
                boolean flag = rs.executeUpdate(sql,new Object[]{uuid,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,userid,patternvalue,departmentid,subcompanyid});
                bb.writeLog("sql;"+sql);
                if(flag){
                    String dataid = "";
                    sql = " select * from "+uftable+" where uuid=? ";
                    bb.writeLog("sql:" + sql);
                    rs.executeQuery(sql,new Object[]{uuid});
                    if (rs.next()) {
                        dataid = Util.null2String(rs.getString("id"));
                    }
                    bb.writeLog("sql:" + sql);
                    bb.writeLog("dataid:" + dataid);
                    bb.writeLog("modeid:" + formmodeid);
                    if (StringUtils.isNotEmpty(dataid))
                    {
                        modeRightInfo.setNewRight(true);
                        modeRightInfo.editModeDataShare(Integer.valueOf(modedatacreater), formmodeid, Integer.parseInt(dataid));

                        data = true;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("e:"+e);
        }
        returnMap.put("data",data);
        return returnMap;
    }
}
