using System.Data;
using System.Data.SqlClient;
using Business.System.Dep.Entity;
using Common;

namespace Business.System.Dep.Vo
{
    public class DepVo
    {
        /// <summary>
        /// 参数赋值
        /// </summary>
        /// <param name="dep">实体类DepEty</param>
        /// <returns>参数数组</returns>
        private SqlParameter[] SetPara(DepEty dep)
        {
            return new[]
                {
                    new SqlParameter("@depid", dep.Id),
                    new SqlParameter("@name", dep.Name),
                    new SqlParameter("@remark", dep.Remark),
                    new SqlParameter("@delflag", dep.DelFlag),
                    new SqlParameter("@addUserID", dep.AddUserId),
                    new SqlParameter("@addtime", dep.AddTime),
                    new SqlParameter("@updateUserID", dep.UpdateUserId),
                    new SqlParameter("@lastTime", dep.LastTime)
                };
        }

        /// <summary>
        /// 新增部门记录
        /// </summary>
        /// <param name="dep">实体类DepEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string InsertSysDep(DepEty dep)
        {
            var sql =
                "insert into SysDep(depid,name,remark,delflag,addUserID,addtime,updateUserID,lastTime) values(@depid,@name,@remark,@delflag,@addUserID,@addtime,@updateUserID,@lastTime)";
            return SqlHelper.ExecSql(sql, SetPara(dep));
        }

        /// <summary>
        /// 获取全部部门信息
        /// </summary>
        /// <param name="searchText">查询字段</param>
        /// <returns>数据表</returns>
        public DataTable AllSysDep(string searchText)
        {
            var sql =
                "select p.depid,p.name,p.remark,p.lastTime,s.username as addusername ,ss.username as updateusername from SysDep p left join SysUser s on s.userid=p.addUserID left join SysUser ss on ss.userid=p.updateUserID where p.delflag ='false' ";

            if (!string.IsNullOrEmpty(searchText))
            {
                //根据查询字段 进行查询
                sql +=
                    string.Format(
                        "and( p.name like '%{0}%' or  p.remark like '%{0}%' or  s.username like '%{0}%' or ss.username like '%{0}%')",
                        searchText);
            }
            sql += " order by p.lastTime DESC";

            return SqlHelper.ExecSqlDateTable(sql);
        }

        /// <summary>
        /// 更新部门信息
        /// </summary>
        /// <param name="dep">实体类DepEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string UpdateSysDep(DepEty dep)
        {
            var sq1 =
                "update SysDep set name=@name,remark=@remark,lastTime=@lastTime,updateUserID=@updateUserID where depid=@depid";
            return SqlHelper.ExecSql(sq1, SetPara(dep));
        }

        /// <summary>
        /// 根据depid删除部门信息
        /// </summary>
        /// <param name="depid">部门depid</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string DelSysDep(string depid)
        {
            var sql = "update SysDep set delflag='true' where depid in (" + depid + ")";
            return SqlHelper.ExecSql(sql);
        }

    }
}
