using System;
using System.Collections;
using System.Data;
using System.Text;
using Business.FileFlow.Entity;
using Business.FileFlow.Vo;
using Common;
using LogisticsSystem.Control;
using LogisticsSystem.Control.Helper;

namespace LogisticsSystem.SysMain.FileFlow
{
    public partial class FlowSrv : SrvBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var type = Request.QueryString["type"];

            if (!CheckUserLogin()) return;

            if (!CheckUserDo("用户执行--流程管理", type))
            {
                DataRow[] row = InitHelper.Ops.Select("code='" + type + "'");
                if (row.Length <= 0)
                {
                    Response.Write("用户没有权限操作");
                    return;
                }
                Response.Write("用户没有权限操作:" + row[0]["name"]);
                return;
            }

            //根据类型 执行相关操作
            switch (type)
            {
                case "flowSave":        //新增流程信息
                    FlowSave();
                    break;
                case "GetAllFlow":      //获取全部流程信息
                    GetAllFlow();
                    break;
                case "flowUpdate":       //更新流程信息
                    FlowUpdate();
                    break;
                case "FlowDel":         //删除流程信息
                    FlowDel();
                    break;
                case "SelectFlow":      //获取流程选择框
                    SelectFlow();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }

        /// <summary>
        /// 获取流程表头
        /// </summary>
        /// <returns>流程表头</returns>
        private StringBuilder FlowHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"flowSTD\"><thead><td></td>");
            tableHead.Append("<td>名称</td>");
            tableHead.Append("<td>操作步骤</td>");
            tableHead.Append("<td>备注</td>");
            tableHead.Append("<td>最后修改时间</td>");
            tableHead.Append("<td>添加人</td>");
            tableHead.Append("<td>修改人</td></thead>");
            return tableHead;
        }


        private void GetAllFlow()
        {
            //获取表头
            StringBuilder table = FlowHead();
            //获取流程数据
            DataTable data = new FlowVo().AllFlow(Request.QueryString["sT"]);
            //整合数据表
            VoStanderTable(table, data, "id", FlowActions(data),"id");
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增流程信息
        /// </summary>
        private void FlowSave()
        {
            var flow = new FlowEty();
            if (!GetFlowEty(flow))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new FlowVo().InsertFlow(flow);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改流程信息
        /// </summary>
        private void FlowUpdate()
        {
            var flow = new FlowEty();
            if (!GetFlowEty(flow))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new FlowVo().UpdateFlow(flow);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 删除流程信息
        /// </summary>
        private void FlowDel()
        {
            string id = "'";
            string[] ids = Request.QueryString["id"].Split(',');
            id += string.Join("','", ids) + "'";
            var result = new FlowVo().DelFlow(id);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }


        /// <summary>
        /// 实体类FlowEty 赋值
        /// </summary>
        /// <returns>bool</returns>
        private bool GetFlowEty(FlowEty flow)
        {

            flow.Name = Request.QueryString["name"];
            flow.Remark = Request.QueryString["remark"];
            flow.ActionIDs = Request.QueryString["aIDs"];
            flow.Id = Request.QueryString["id"] == "" ? Com.NewGuid() : Request.QueryString["id"];
            flow.AddUserId = Session["Userid"].ToString();
            flow.AddTime = DateTime.Now;
            flow.UpdateUserId = Session["Userid"].ToString();
            flow.LastTime = DateTime.Now;
            flow.DelFlag = false;
            return true;
        }


        /// <summary>
        /// 根据流程actionIDs, 找出所有流程操作名字
        /// </summary>
        /// <param name="data">流程数据表</param>
        /// <returns>嵌套哈希表</returns>
        private Hashtable FlowActions(DataTable data)
        {
            var hash = new Hashtable();
            DataTable actions = new ActionVo().AllAction("");       //读取所有流程操作
            var flowVo = new FlowVo();
            foreach (DataRow row in data.Rows)
            {
                hash[row["id"]] = flowVo.ActionNString(row["actionIDs"].ToString(), actions); //按照流程ID 流程操作名字
            }

            //返回嵌套哈希表
            var result=new Hashtable();
            result["actionIDs"] = hash;
            return result;
        }

        /// <summary>
        /// 获取流程选择框
        /// </summary>
        private void SelectFlow()
        {
            var options = new StringBuilder();

            //获取部门数据
            DataTable data = new FlowVo().AllFlow("");

            //打包成option
            foreach (DataRow row in data.Rows)
            {
                options.Append("<option value=\"" + row["id"] + "\">" + row["name"] + "</option>");
            }
            if (options.ToString() == "")
            {
                Response.Write("");
                return;
            }

            Response.Write(options.ToString());
        }
    }
}