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

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

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

        /// <summary>
        /// 获取全部流程信息
        /// </summary>
        /// <param name="searchText">查询字段</param>
        /// <returns>数据表</returns>
        public DataTable AllFlow(string searchText)
        {
            var sql =
                "select p.id,p.name,p.actionIDs,p.remark,p.lastTime,s.username as addusername ,ss.username as updateusername from flow 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="flow">实体类FlowEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string UpdateFlow(FlowEty flow)
        {
            var sq1 =
                "update flow set name=@name,actionIDs=@actionIDs,remark=@remark,lastTime=@lastTime,updateUserID=@updateUserID where id=@id";
            return SqlHelper.ExecSql(sq1, SetPara(flow));
        }

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

        /// <summary>
        /// 根据流程id 获取某一步的操作ID
        /// </summary>
        /// <param name="flowid">流程id</param>
        /// <param name="num">第几步</param>
        /// <returns>操作id</returns>
        public string GetAction(string flowid)
        {
            string sql = "select actionIDs from flow where id=@id";
            return SqlHelper.ExecSqlSc(sql, new SqlParameter("@id", flowid)); 
        }

        /// <summary>
        /// 根据流程ID获取流程名字
        /// </summary>
        /// <param name="flowid"></param>
        /// <returns></returns>
        public string GetFlowNameById(string flowid)
        {
            string sql = "select name from flow where id=@id";
            return SqlHelper.ExecSqlSc(sql, new SqlParameter("@id", flowid)); 
        }

  

        /// <summary>
        /// 根据id返回FlowEty实体类
        /// </summary>
        /// <param name="id">flowid</param>
        public FlowEty GetFlowById(string id)
        {
            var con = new SqlConnection(SqlHelper.ConStr);
            var cmd = new SqlCommand { Connection = con, CommandType = CommandType.Text, CommandText = "select * from  flow where id=@id and delflag='false'" };
            cmd.Parameters.AddWithValue("@id", id);
            var flow = new FlowEty();
            try
            {
                con.Open();
            }
            catch
            {
                return flow;
            }
            using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                if (!reader.HasRows)
                    return flow;
                while (reader.Read())
                {
                    flow.Id = SqlHelper.ReaderString("id", reader);
                    flow.Num = SqlHelper.ReaderInt("num", reader);
                    flow.Name = SqlHelper.ReaderString("name", reader);
                    flow.Remark = SqlHelper.ReaderString("remark", reader);
                    flow.DelFlag = SqlHelper.ReaderBool("delflag", reader);
                    flow.AddUserId = SqlHelper.ReaderString("addUserID", reader);
                    flow.AddTime = SqlHelper.ReaderDateTime("addtime", reader);
                    flow.UpdateUserId = SqlHelper.ReaderString("updateUserID", reader);
                    flow.LastTime = SqlHelper.ReaderDateTime("lastTime", reader);
                    flow.ActionIDs = SqlHelper.ReaderString("actionIDs", reader);
                }
            }
            return flow;
        }


        /// <summary>
        /// 根据actionID获取操作顺序名字
        /// </summary>
        /// <param name="actionIDs">流程的actionIDs</param>
        /// <param name="actionTable">所有流程操作数据表</param>
        /// <returns>流程操作顺序名字</returns>
        public string ActionNString(string actionIDs ,DataTable actionTable)
        {

            Hashtable temphash = FlowAction(actionIDs);           //保存流程操作的顺序号及名字
            string actionName = string.Empty;
            for (int i = 0; i < temphash.Count; i++)            //根据顺序号,组织流程操作名字
            {
                DataRow[] temprow = actionTable.Select("id='" + temphash[i.ToString()] + "'");
                actionName += temprow[0]["name"] + "-->";
            }
            return actionName.Substring(0, actionName.Length - 3); 
        }


        /// <summary>
        /// 根据actionID获取操作顺序名字
        /// </summary>
        /// <param name="actionIDs">流程的actionIDs</param>
        /// <param name="actionTable">所有流程操作数据表</param>
        /// <param name="now">当前执行步骤id</param>
        /// <returns>流程操作顺序名字</returns>
        public string ActionNString(string actionIDs, DataTable actionTable,int now)
        {
            string actionName = string.Empty;
            Hashtable temphash = FlowAction(actionIDs);
            for (int i = 0; i < temphash.Count; i++)        //根据顺序号,组织流程操作名字
            {
                DataRow[] temprow = actionTable.Select("id='" + temphash[i.ToString()] + "'");

                if (now == i)
                {
                    actionName += "<font color=\"red\">" + temprow[0]["name"] + "</font>-->";
                }
                else
                {
                    actionName += temprow[0]["name"] + "-->";
                }
            }
            return actionName.Substring(0, actionName.Length - 3);
        }

        /// <summary>
        /// 根据actionIDs分离对应的序号和操作id
        /// </summary>
        /// <param name="actionIDs">actionIDs</param>
        /// <returns>Hashtable</returns>
        public Hashtable FlowAction(string actionIDs)
        {
            string[] temp = actionIDs.Split(',');
            var temphash = new Hashtable();                //保存流程操作的顺序号及名字
           
            foreach (string s in temp)
            {
                string[] action = s.Split(':');
                temphash[action[0]] = action[1];
            }
            return temphash;
        }

        /// <summary>
        /// 根据操作步骤获取操作ID和名字
        /// </summary>
        /// <param name="actionIDs">流程操作ids</param>
        /// <param name="actionTable">所有流程操作数据表</param>
        /// <param name="now">步骤</param>
        /// <returns>ActionEty实体类</returns>
        public ActionEty NowActionId(string actionIDs, DataTable actionTable, string now)
        {
            string[] temp = actionIDs.Split(',');
            var actionEty = new ActionEty();                 //保存流程操作的顺序号及名字
 
            foreach (string s in temp)
            {
                string[] action = s.Split(':');
                if (action[0]==now)
                {
                    DataRow[] temprow = actionTable.Select("id='" + action[1] + "'");
                    actionEty.Id = action[1];                         //操作id 
                    actionEty.Name = temprow[0]["name"].ToString();    //操作名字
                    return actionEty;
                }
            }
            return actionEty;
        }
    }
}
