using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.DBA.DBModle;
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 SaveUserListExcutor : ExcutorBase<SaveUserListRequest, SaveUserListResponse>
    {
        protected override void ExcuteInternal(SaveUserListRequest request, object state, SaveUserListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

            if (request.userList == null || request.userList.Count == 0)
            {
                rslt.ErrMsg = "未提交用户信息";
                return;
            }
            foreach (var user in request.userList)
            {
                if (user.userid == null)
                {
                    rslt.ErrMsg = "用户ID异常";
                    return;
                }
                if (string.IsNullOrEmpty(user.username))
                {
                    rslt.ErrMsg = "用户名不能为空";
                    return;
                }
            }

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

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        psw_bczh3 pswhelper = new psw_bczh3();
                        foreach (var user in request.userList)
                        {
                            cmd.CommandText = "SELECT userid FROM u_user_jlhprice WHERE userid = @userid AND empid <> @empid";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@userid", user.userid);
                            cmd.Parameters.AddWithValue("@empid", user.empid);
                            using (var reader = cmd.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    throw new Exception(string.Format("已存在用户名为[{0}]的记录，不能重复添加", user.userid));
                                }
                            }
                            var fields = "userid,username,descrp,rightstring,deptstr,outrepstr,pricelist_seestr,pricelist_editstr,usermode";
                            if (user.empid == 0)
                            {
                                //新建
                                user.empid = BllHelper.GetID(cmd, "u_user_jlhprice");
                                user.userid = user.userid;
                                user.username = user.username;
                                user.psw = pswhelper.GetEntrypt(user.userid, 0, "123457851239866");
                                fields += ",psw,empid";
                                DbSqlHelper.Insert(cmd, "u_user_jlhprice", null, user, fields);
                            }
                            else
                            {
                                //修改
                                DbSqlHelper.Update(cmd, "u_user_jlhprice", null, user, "empid", fields);
                            }
                        }
                        rslt.empid = request.userList[0].empid;
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
