using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.DBA.DBModle;
using JLHHJSvr.LJException;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;
using LJLib.Tools.DEncrypt;
using LJLib.Tools.Encry;

namespace JLHHJSvr.Excutor
{
    internal sealed class ChangePasswordExcutor : ExcutorBase<ChangePasswordRequest, ChangePasswordResponse>
    {
        protected override void ExcuteInternal(ChangePasswordRequest request, object state, ChangePasswordResponse rslt)
        {
            //if (string.IsNullOrEmpty(request.newpassword))
            //{
            //    rslt.ErrMsg = "新密码不能为空";
            //    return;
            //}

            psw_bczh3 pswhelper = new psw_bczh3();

            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {

                con.Open();
                var sys_power_key = "123457851239866";

                cmd.CommandText =
                    @"select psw from u_user_jlhprice where userid = @userid";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@userid", request.userid);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        var psw = Convert.ToString(reader["psw"]).Trim();


                        if (pswhelper.GetEntrypt(request.oldpassword, 0, sys_power_key) != psw)  //BC493812B6664BECBF44C21C3BB043C4
                        {
                            rslt.ErrMsg = "当前密码错误";
                            return;
                        }
                    }
                    else
                    {
                        rslt.ErrMsg = "用户名不存在或密码错误";
                        return;
                    }
                }

                //if (!UserHelper.CheckL1LoginPasswordStrength(option707, request.newpassword))
                //{
                //    string pswErr = string.Empty;
                //    switch (option707)
                //    {
                //        case 3:
                //            pswErr = "数字字母加特殊符号混合(至少8位长度校验)";
                //            break;
                //        case 2:
                //            pswErr = "数字字母混合(至少6位长度校验)";
                //            break;
                //        case 1:
                //            pswErr = "纯数字(至少6位长度校验)";
                //            break;
                //    }
                //    rslt.ErrMsg = $"新密码强度不匹配,匹配规则:{pswErr}";
                //    return;
                //}

                cmd.Transaction = cmd.Connection.BeginTransaction(IsolationLevel.ReadUncommitted);
                var pswd1 = pswhelper.GetEntrypt(request.newpassword, 0, sys_power_key);

                cmd.CommandText = @"update u_user_jlhprice 
                                    set psw = @psw
                                    where userid = @userid";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@userid", request.userid);
                cmd.Parameters.AddWithValue("@psw", pswd1);

                if (cmd.ExecuteNonQuery() == 1)
                {
                    cmd.Transaction.Commit();
                }
                else
                {
                    cmd.Transaction.Rollback();
                    rslt.ErrMsg = "修改失败，修改数量不为1";
                }

            }
        }
    }
}