package com.weaver.seconddev.interfaces.workflow.action;

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

import com.weaver.common.distribution.genid.IdGenerator;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.framework.rpc.context.impl.TenantRpcContext;
import com.weaver.mc.util.StringUtils;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.Util;

import com.weaver.verupgrade.hrm.resource.ResourceComInfo;

/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 */
@org.springframework.stereotype.Service("htjt_mode_UpdateSecLevelAndLockStatus")
public class UpdateSecLevelAndLockStatus implements EsbServerlessRpcRemoteInterface {

    public static final org.slf4j.Logger logger_b15f4fa5 = LoggerFactory.getLogger(UpdateSecLevelAndLockStatus.class);

    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public WeaResult<Map<String, Object>> execute(Map<String, Object> param) {

        logger_b15f4fa5.error("htjt_mode_UpdateSecLevelAndLockStatus start param == " + param);
        Long billid = Long.parseLong(String.valueOf(param.getOrDefault("billid", -1)));
        logger_b15f4fa5.error("UpdateSecLevelAndLockStatus billid == "+ billid );

        Map<String, String> result = new HashMap<String, String>();
        try {
            if (billid != null) {

                if (billid > 0 ) {
                    RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                    String sql = "select * from e10_common.dbo.uf_sellevellock_chg where id=" + billid;
                    logger_b15f4fa5.error("UpdateSecLevelAndLockStatus sql == "+ sql );
                    String ry = "";
                    String aqjb = "";
                    String zhsd = "";
                    rs.execute(sql);
                    if (rs.next()) {
                        ry = Util.null2String(rs.getString("ry"));
                        aqjb = Util.null2String(rs.getString("aqjb"));
                        zhsd = Util.null2String(rs.getString("zhsd"));
                        logger_b15f4fa5.error("zhsd1 == "+ zhsd );
                    }
                    if ("".equals(zhsd)) {
                        zhsd = "0";
                    }
                    sql = "update eteams.dbo.employee  set sec_level=" + aqjb + "  where id=" + ry + "";
                    logger_b15f4fa5.error("UpdateSecLevelAndLockStatus updatesec_level  == "+ sql );
                    boolean execute = rs.execute(sql);
                    logger_b15f4fa5.error("execute == "+ execute );



                    //更新账号锁定记录
                    sql = "select id, user_id from  eteams.dbo.employee a where  a.id = " + ry;
                    logger_b15f4fa5.error("UpdateSecLevelAndLockStatus updatesec_level  == "+ sql );
                    boolean execute2 = rs.execute(sql);
                    logger_b15f4fa5.error("execute2 == "+ execute2 );
                    String userid = "";
                    if(rs.next()){
                        userid = rs.getString("user_id");
                    }
                    logger_b15f4fa5.error("userid == "+ userid );
                    if(StringUtils.isEmpty(userid)){
                        return  WeaResult.fail(500, "人员id查询为空");
                    }
                    logger_b15f4fa5.error("zhsd2 == "+ zhsd );
                    if(StringUtils.isNotEmpty(zhsd)){
                        if(zhsd.equals("0")){  //解锁，删除所有锁定记录
                            sql = "update  ec_passport.dbo.user_lock_info set delete_type = 1 where user_id =  " + userid + "  and delete_type  = 0 ";

                            logger_b15f4fa5.error("UpdateSecLevelAndLockStatus updatedelete_type == "+ sql );
                            boolean execute1 = rs.execute(sql);
                            logger_b15f4fa5.error("execute1 == "+ execute1 );
                        }else if(!zhsd.equals("0")){//锁定，新增一条数据
                            sql = "SELECT COUNT (1) AS sd FROM  eteams.dbo.employee a ,ec_passport.dbo.user_lock_info b  " +
                                    "WHERE a.id = '" + ry +"' AND a.user_id = b.user_id  AND a.delete_type = 0 and b.delete_type = 0 ";
                            boolean b1 = rs.executeQuery(sql);
                            if(rs.next()){
                                int sd = rs.getInt("sd");
                                logger_b15f4fa5.error("sd == "+ sd );
                                if(sd > 0 ){
                                    //有记录
                                    logger_b15f4fa5.error("该账号已锁定");
                                }else{

                                    String currentEmployeeId = TenantRpcContext.getCurrentEmployeeId();
                                    String currentTenantKey = TenantRpcContext.getCurrentTenantKey();

                                    Date date = new Date();

                                    // 创建 SimpleDateFormat 对象，指定包含毫秒的日期时间格式
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

                                    // 使用 SimpleDateFormat 的 format 方法将 Date 对象格式化为字符串
                                    String formattedDateTime = sdf.format(date);


                                    Long id = IdGenerator.generate();
                                    sql = "INSERT INTO ec_passport.dbo.user_lock_info (" +
                                            "id, create_time, update_time, creator, delete_type, " +
                                            "tenant_key, user_id, employee_id, overdue_time, user_status, lock_type" +
                                            ") VALUES ('"+id+"', '"+formattedDateTime +"', '"+formattedDateTime+"', '"+currentEmployeeId+
                                            "', 0, '"+currentTenantKey  +"', " + userid + ", " + userid +
                                            " , '2099-01-01 00:00:00.000', 'normal', 'ACCOUNT_LOCK')";

                                    logger_b15f4fa5.error("UpdateSecLevelAndLockStatus insert == "+ sql );
                                    boolean execute1 = rs.execute(sql);
                                    logger_b15f4fa5.error("execute1 == "+ execute1 );


                                }
                            }
                        }

                    }



                    ResourceComInfo rci = I18nContextUtil.getBean(ResourceComInfo.class);
                    rci.updateResourceInfoCache(ry);
                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
            return  WeaResult.fail(500, "UpdateSecLevelAndLockStatus e {}"+ e);

        }
        logger_b15f4fa5.error("htjt_mode_UpdateSecLevelAndLockStatus stop result == " + result);
        return WeaResult.success(new HashMap<>(result));
    }
}
