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

namespace Business.FileFlow.Vo
{
    public class ActionVo
    {
        /// <summary>
        /// 参数赋值
        /// </summary>
        /// <param name="action">实体类ActionEty</param>
        /// <returns>参数数组</returns>
        private SqlParameter[] SetPara(ActionEty action)
        {
            return new[]
                {
                    new SqlParameter("@id", action.Id),
                    new SqlParameter("@name", action.Name),
                    new SqlParameter("@remark", action.Remark),
                    new SqlParameter("@delflag", action.DelFlag),
                    new SqlParameter("@addUserID", action.AddUserId),
                    new SqlParameter("@addtime", action.AddTime),
                    new SqlParameter("@updateUserID", action.UpdateUserId),
                    new SqlParameter("@lastTime", action.LastTime),
                    new SqlParameter( "@approveId",action.ApproveId)
                };
        }

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

        /// <summary>
        /// 获取全部流程操作信息
        /// </summary>
        /// <param name="searchText">查询字段</param>
        /// <returns>数据表</returns>
        public DataTable AllAction(string searchText)
        {
            var sql =
                "select p.id,p.name,p.remark,sss.depid,p.approveId as pid,sss.username as approvename,p.lastTime,s.username as addusername ,ss.username as updateusername from action p left join SysUser s on s.userid=p.addUserID left join SysUser ss on ss.userid=p.updateUserID left join SysUser sss on sss.userid=p.approveId 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}%' or sss.username like '%{0}%' )",
                        searchText);
            }
            sql += " order by p.lastTime DESC";

            return SqlHelper.ExecSqlDateTable(sql);
        }

        /// <summary>
        /// 更新流程操作信息
        /// </summary>
        /// <param name="action">实体类ActionEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string UpdateAction(ActionEty action)
        {
            var sq1 =
                "update action set name=@name,remark=@remark,lastTime=@lastTime,updateUserID=@updateUserID,approveId=@approveId where id=@id";
            return SqlHelper.ExecSql(sq1, SetPara(action));
        }

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

        /// <summary>
        /// 根据userid 获取该用户可审批的操作 用','分割
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns>操作ID集合 用','分割</returns>
        public string MyActions(string userid)
        {
            string myactions = string.Empty;
            string sql = "select id from action where approveId=@approveId";
            foreach (DataRow row in SqlHelper.ExecSqlDateTable(sql, new SqlParameter("@approveId", userid)).Rows)
            {
                myactions += row["id"]+",";
            }
            return myactions.Equals(string.Empty) ? myactions : myactions.TrimEnd(',');
        }
    }
}
