
using RMYY_CallCenter_Api.Models.Enum;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using RMYY_CallCenter_Api.Utility;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers.WorkOrder
{
    public class T_Hos_WorkOrderController : BaseController
    {
        private Bll.T_Hos_WorkOrder orderbll = new Bll.T_Hos_WorkOrder();
        private Bll.T_Sys_UserAccount userbll = new Bll.T_Sys_UserAccount();
        private Bll.T_Hos_WorkOrderItem itembll = new Bll.T_Hos_WorkOrderItem();
        private Bll.AddLogBll logbll = new Bll.AddLogBll();





        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult GetList(string keyword, string starttime = "", string endtime = "", int pageindex = 1, int pagesize = 10, int state = -1)
        {
            if (User != null)
            {
                string transql = "";
                List<string> wokerlist = new List<string>();
                #region 查询条件
                string sql = $" and F_IsDel=0";
                DataTable data = new DataTable();
                if (starttime.Trim() != "" && starttime != "undefined")
                    sql += $"and datediff(day,F_CreateTime,'" + starttime + "')<=0";
                if (endtime.Trim() != "" && endtime != "undefined")
                    sql += $"and datediff(day,F_CreateTime,'" + endtime + "')>=0";
                if (!string.IsNullOrEmpty(keyword))
                {
                    sql += $"and (F_WorkOrderCode like '%" + keyword.Trim() + "%' or F_ProposerCode like '%" + keyword.Trim() + "%' or F_ProposerPhon like '%" + keyword.Trim() + "%')";
                }
                if (state >= 0 && User.F_RoleCode != "ZYY")
                {
                    sql += $" and F_State=" + state;
                }
                if (User.F_DeptCode == "ZZWY")//综合保障中心
                {
                    if (User.F_UserCode != "8000" && User.F_RoleCode != "ZYY")
                        sql += $" and F_ControlManCode=" + User.F_UserCode;
                }
                else if (User.F_DeptCode == "LC")//需求科室临床
                {
                    //if (User.F_DeptId == 1)
                    //{
                    //    sql += $" and F_WoFlag=0 ";
                    //}
                    //else
                    //{
                    int[] wostate = { (int)EnumHosWorkOrderState.installcomplete, (int)EnumHosWorkOrderState.finish };
                    sql += $" and ((F_WoFlag=1 and F_State in("+ string.Join(",", wostate) + "))";
                    sql += $" or (F_WoFlag=0 and F_State = "+(int)EnumHosWorkOrderState.transcomplete + "))";
                    //}
                }
                else if (User.F_DeptCode == "SBK")//设备科
                {
                    if (state == 1)
                    {
                      
                    }
                    else if (state == -1)
                    {

                    }
                    if (state == -2)
                    {
                        sql += $" and  F_OutboundTime is not null and F_WoFlag=0 ";

                    }
                    sql += $" and F_ProposerDeptId=" + User.F_DeptId + "";
                }
                else if (User.F_DeptCode == "WYZX" && User.F_RoleCode != "ZYY")//维运中心
                {
                    int[] wostate = {(int)EnumHosWorkOrderState.awaitfetch, (int)EnumHosWorkOrderState.fetch, (int)EnumHosWorkOrderState.install , (int)EnumHosWorkOrderState.installcomplete };
                    sql += $" and ((F_State in(" + string.Join(",", wostate) + " ) and F_IsCooperate=1 and F_WoFlag=1)";
                    sql += $" or (F_State ="+(int)EnumHosWorkOrderState.transcomplete+ " and F_IsCooperate=1 and F_WoFlag=0))";
                }
                if (User.F_DeptCode == "ZZWY" && User.F_RoleCode == "ZYY")//转运人判断
                {
                    if (state == 3)
                    {
                        sql += $" and F_State=" + state;
                        //sql += $" and  F_Id in(select F_WoID from T_Hos_WorkOrderItem where F_WoID=aa.F_Id and F_WoState={state})";
                    }
                    //else
                    //{
                    transql = GetTranPersonsql(User.F_UserCode, 1, state);
                    if (transql != "")
                    {
                        wokerlist = transql.Split(',').Distinct().ToList();
                    }
                    if (wokerlist.Count > 0)
                    {
                        sql += $" and F_Id in (" + string.Join(",", wokerlist) + ")";
                    }
                    else
                    {
                        sql += $" and F_Id in (0)";
                    }
                    //}
                }
                if (User.F_DeptCode == "WYZX" && User.F_RoleCode == "ZYY")//维运转运人判断
                {
                    if (state == 7)
                    {
                        sql += $" and F_State=" + state;
                        //sql += $" and  F_Id in(select F_WoID from T_Hos_WorkOrderItem where F_WoID=aa.F_Id and F_WoState={state})";
                    }
                    transql = GetTranPersonsql(User.F_UserCode, 2, state);                 
                    if (transql != "")
                    {
                        wokerlist = transql.Split(',').Distinct().ToList();
                    }
                    if (wokerlist.Count > 0)
                    {
                        sql += $" and F_Id in (" + string.Join(",", wokerlist) + ")";
                    }
                    else
                    {
                        sql += $" and F_Id in (0)";
                    }
                }
                #endregion
                int recordCount = 0;
                if (!string.IsNullOrEmpty(sql))
                {
                    data = Bll.PagerBll.GetListPager
                        (
                        "T_Hos_WorkOrder aa",
                        "F_Id",
                        "*",
                        sql,
                        "ORDER BY F_Id desc",
                        pagesize,
                        pageindex,
                        true,
                        out recordCount
                        );
                }
                var modellist = orderbll.DataTableToList(data);

                if (modellist.Count > 0)
                {
                    //if (wokerlist.Count > 0 || User.F_UserCode== "ZYY")
                    //{
                    foreach (var item in modellist)
                    {

                        if (User.F_RoleCode == "ZYY")
                        {
                            int? wostate= itembll.GetModelList("F_WoID=" + item.F_Id + " and (F_CreateUser=" + User.F_UserCode + " or F_NextUser like '%" + User.F_UserCode + "%')").OrderByDescending(pp => pp.F_ID).FirstOrDefault()?.F_WoState??0;
                            item.F_Wostate = EnumHelper.GetDescription((EnumHosWorkOrderState)wostate);
                            item.F_State = wostate;
                        }
                        else
                        {
                            item.F_Wostate = EnumHelper.GetDescription((EnumHosWorkOrderState)item.F_State);
                        }
                        item.F_SitMan = item.F_ControlManName + '(' + item.F_ControlManCode + ')';
                        item.rolecode = User.F_RoleCode;
                        item.deptcode = User.F_DeptCode;
                        //var buttons = ButtonGroup.GetButtons(item.F_State, User.F_RoleCode, item.F_Type);

                    }
                }

                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = modellist,                  
                    total = recordCount
                };
                return Content(obj.ToJson());
            }
            return Error("无操作权限！");
        }


        /// <summary>
        /// 转运人待转运工单
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        private string GetTransql(string usercode, int type = 1)
        {
            string str = string.Empty;
            int state = 0;
            int optstate = 0;
            if (type == 1)
            {
                state = (int)EnumHosWorkOrderState.assign;
                optstate = (int)EnumHosItemOpt.assign;
            }
            else
            {
                state = (int)EnumHosWorkOrderState.awaitfetch;
                optstate = (int)EnumHosItemOpt.fetch;
            }
            var list = itembll.GetModelList("F_OptType=" + optstate + "and F_WoState=" + state + " and F_NextUser=" + usercode + " ").Select(t => t.F_WoID).Distinct<int>().ToList();
            if (list.Count > 0)
                foreach (var item in list)
                {
                    str = string.IsNullOrEmpty(str) ? item.ToString() : str + "," + item.ToString();
                }
            return str;
        }
        /// <summary>
        /// 转运人操作的工单
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        private string GetTranPersonsql(string usercode, int type = 1,int wostate=-1)
        {
            string wokerlist = "";
            string[] wokerarr = { };
            string statesql = "";
            string sql = "";

            if (type == 1)
            {
                statesql = (int)EnumHosItemOpt.assign + "," + (int)EnumHosItemOpt.receive + "," + (int)EnumHosItemOpt.transport + "," + (int)EnumHosItemOpt.transcomplete;
            }
            else
            {
                statesql = (int)EnumHosItemOpt.assign+","+(int)EnumHosItemOpt.fetch + "," + (int)EnumHosItemOpt.install + "," + (int)EnumHosItemOpt.installcomplete;
            }
            if (wostate > 0)
            {
                 sql = $" isnull(F_IsUsed,'0')='0' and (F_CreateUser=" + usercode + " or  F_NextUser like '%"+ usercode + "%') and F_OptType in(" + statesql + ") and F_WoState=" + wostate + "";
            }
            else
            {
                 sql = $" isnull(F_IsUsed,'0')='0' and (F_CreateUser=" + usercode + " or  F_NextUser like '%" + usercode + "%') and F_OptType in(" + statesql + " )";
            }
            var itemlist = itembll.GetModelList(sql).Select(t => t.F_WoID).Distinct<int>().ToList();
            //var itemlist = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and (F_CreateUser=" + usercode + " or  F_NextUser=" + usercode + ") and F_WoType=" + type + "").Select(t => t.F_WoID).Distinct<long>().ToList();
            if (itemlist.Count > 0)
            {
                foreach (var item in itemlist)
                {
                    wokerlist = string.IsNullOrEmpty(wokerlist) ? item.ToString() : wokerlist + "," + item.ToString();
                }
                if (wokerlist != "")
                    wokerarr = wokerlist.Split(',');
            }

            return wokerlist;
        }

        /// <summary>
        /// 获取工单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetModelDetail(int id)
        {
            if (User != null)
            {
                string sql = "F_WoID = " + id + "";
                if (id <= 0)
                {
                    return Error("请选择要查询的工单！");
                }
                var model = orderbll.GetModel(id);
                model.F_Wostate = EnumHelper.GetDescription((EnumHosWorkOrderState)model.F_State);
                model.F_SitMan = model.F_ControlManName + '(' + model.F_ControlManCode + ')';
                model.rolecode = User.F_RoleCode;
                model.deptcode = User.F_DeptCode;
                model.HosName = new Bll.T_Wowo_repairyqbll().GetModel(model.F_HosId ?? 0)?.T_Woname??"";
                var itemlist = itembll.GetModelList(sql);
                //List<Con_WorkOrderInput> Input = modeltoopi(list);
                if (model != null)
                {
                    var obj = new
                    {
                        state = "success",
                        message = "成功",
                        model = model,
                        itemlist = itemlist
                    };
                    return Content(obj.ToJson());
                }
                else
                    return Error("没有查询到此工单！");
            }
            return Error("无操作权限！");

        }


        /// <summary>
        /// 报表
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        public ActionResult GetListExpt(string keyword, string starttime = "", string endtime = "", int state = -1, int pageindex = 1, int pagesize = 10, int isdc = 0)
        {
            if (User!=null)
            {
                #region 查询条件
                string sql = $" and F_IsDel=0";
                DataTable data = new DataTable();
                if (starttime.Trim() != "" && starttime != "undefined")
                    sql += $" and datediff(day,F_CreateTime,'" + starttime + "') <=0 ";
                if (endtime.Trim() != "" && endtime != "undefined")
                    sql += $" and datediff(day,F_CreateTime,'" + endtime + "')>=0";
                if (!string.IsNullOrEmpty(keyword))
                {
                    sql += $"and (F_WorkOrderCode like '%" + keyword.Trim() + "%' or F_ProposerCode like '%" + keyword.Trim() + "%' or F_ProposerPhon like '%" + keyword.Trim() + "%')";
                }
                if (state >= 0)
                {
                    sql += $"and F_State=" + state;
                }
                #endregion
                if (isdc > 0)
                {
                    NPOIHelper nopi = new NPOIHelper();
                    data = new Bll.T_Hos_WorkOrder().GetListExpt(sql).Tables[0];
                    if (nopi.ExportToExcel("病床转移统计报表", data, col()) == "")
                    {
                        return Success("导出成功！");
                    }
                    else return Error("导出失败！");
                }
                else
                {
                    int recordCount = 0;
                    if (!string.IsNullOrEmpty(sql))
                    {
                        data = Bll.PagerBll.GetListPager
                            (
                            "T_Hos_WorkOrder aa",
                            "F_Id",
                            "*",
                            sql,
                            "ORDER BY F_Id desc",
                            pagesize,
                            pageindex,
                            true,
                            out recordCount
                            );
                    }
                    var modellist = orderbll.DataTableToList(data);

                    if (modellist.Count > 0)
                    {
                        //if (wokerlist.Count > 0 || User.F_UserCode== "ZYY")
                        //{
                        foreach (var item in modellist)
                        {

                            if (User.F_RoleId == 1003)
                            {
                                item.F_Wostate = EnumHelper.GetDescription((EnumHosWorkOrderState)itembll.GetModelList("F_WoID=" + item.F_Id + " and (F_CreateUser=" + User.F_UserCode + " or F_NextUser like '%" + User.F_UserCode + "%')").OrderByDescending(pp => pp.F_ID).FirstOrDefault().F_OptType);
                            }
                            else
                            {
                                item.F_Wostate = EnumHelper.GetDescription((EnumHosWorkOrderState)item.F_State);
                            }
                            item.F_SitMan = item.F_ControlManName + '(' + item.F_ControlManCode + ')';
                            item.rolecode = User.F_RoleCode;
                            item.deptcode = User.F_DeptCode;
                            //var buttons = ButtonGroup.GetButtons(item.F_State, User.F_RoleCode, item.F_Type);

                        }
                    }

                    var obj = new
                    {
                        state = "success",
                        message = "成功",
                        rows = modellist,
                        total = recordCount
                    };
                    return Content(obj.ToJson());
                }
            }
            return Error("无操作权限！");
        }




        private string[] col()
        {
            string[] cel =
            {
                "工单编号","类型","申请科室","申请人","联系电话","工单内容","调度员","上报时间","转运部门","转运人","协作部门","协作人","备注"
            };
            return cel;
        }


        /// <summary>
        /// 添加工单
        /// </summary>
        /// <param name="type"></param>
        /// <param name="deptid"></param>
        /// <param name="deptname"></param>
        /// <param name="usercode"></param>
        /// <param name="username"></param>
        /// <param name="phon"></param>
        /// <param name="bedmess"></param>
        /// <param name="remark"></param>
        /// <param name="callid"></param>
        /// <returns></returns>
        public ActionResult AddWorkOrder(int type,int hosid, int deptid, string deptname, string usercode, string username, string phon, string bedmess, string remark, string callid = "")
        {
            if (User != null)
            {
            #region 添加条件
            if (type != (int)EnumWorkOrder.Hos)
                return Error("工单类型错误！");
            if (hosid <= 0)
                return Error("请选择院区！");
            if (deptid == 0)
                return Error("请选择科室！");
            if (usercode == "")
                return Error("请选择申请人！");
            if (phon == "")
                return Error("请输入联系电话！");
            if (bedmess == "")
                return Error("请填写正确的病床转运信息！");
            #endregion

            var model = new Model.T_Hos_WorkOrder();
            model.F_Type = (int)EnumWorkOrder.Hos;//工单类型  1病床转移
            model.F_Source = "PC端";
            model.F_CallID = callid;
            model.F_State = (int)EnumHosWorkOrderState.neworder;//新工单
            model.F_WorkOrderCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");//工单编号
            model.F_ProposerDeptId = deptid;
            model.F_ProposerDeptName = deptname;
            model.F_ProposerCode = usercode;//申请人
            model.F_ProposerName = username;
            model.F_ProposerPhon = phon;
            model.F_IsTransfer = 1;
            model.F_Remark = remark;
            model.F_CreateTime = DateTime.Now;
            model.F_CreateCode = User.F_UserCode;
            model.F_CreateName = User.F_UserName;
            model.F_BedMesItem = bedmess;
            model.F_HosId = hosid;
            //model.F_BedNum = bednum;//病床数量
            //model.F_BedNeedMessage = needmess;//配送需求
                model.F_IsDel = 0;
            model.F_ControlManCode = User.F_UserCode;
            model.F_ControlManName = User.F_UserName;//调度人
            model.F_WoFlag = 0;//工单标记 原工单
            #region 病床信息处理
            string[] _list = bedmess.TrimEnd(',').Split(',');
            string bedmessages = "";
            int bednum = 0;
                try
                {
                    for (int i = 0; i < _list.Length; i++)
                    {
                        string[] _temp = _list[i].Split('-');
                        int num = Convert.ToInt32(_temp[0]);
                        string needmess = _temp[1];
                        if (num > 0)
                        {
                            if (needmess != "")
                            {
                                bedmessages = string.IsNullOrEmpty(bedmessages) ? ("病床数量" + num + "个,配送要求:" + needmess) : (bedmessages + " , " + "病床数量" + num + "个,配送要求:" + needmess);
                            }
                            else
                            {
                                bedmessages = string.IsNullOrEmpty(bedmessages) ? "病床数量" + num + "个,无配送要求" : bedmessages + "," + "病床数量" + num + "个,无配送要求";
                            }
                            bednum += num;
                        }
                        else return Error("病床数量不正确！");
                    }
                }
                catch { return Error("配送信息填写格式错误！"); }
            #endregion
            model.F_BedNeedMessage = bedmessages;//病床配送信息
            model.F_BedNum = bednum;//病床数量
            int n = orderbll.Add(model);
            if (n > 0)
            {
                int clbm = 2002;//设备科
                var wmodel = orderbll.GetModel(n);
                logbll.AddLog(n, 0, (int)EnumWorkOrder.Hos, User.F_UserName + "(" + User.F_UserCode + ")" + "创建新工单", (int)EnumHosItemType.deal, (int)EnumHosItemOpt.create, "", 0, User.F_UserCode);
                AssignWO(User, wmodel, "", "", clbm);
                return Success("操作成功！");
            }
            else return Error("添加失败");
            }
            return Error("无操作权限");
        }

        /// <summary>
        /// 修改工单
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="deptid"></param>
        /// <param name="deptname"></param>
        /// <param name="usercode"></param>
        /// <param name="username"></param>
        /// <param name="phon"></param>
        /// <param name="bedmess"></param>
        /// <param name="callid"></param>
        /// <returns></returns>
        public ActionResult UpdateWorkOrder(int woid,int hosid, int deptid, string deptname, string usercode, string username, string phon, string bedmess, string remark)
        {
            if (User != null)
            {
                var model = orderbll.GetModel(woid);
            if (model == null)
                return Error("不存在此工单编号！");
            if (model.F_State == (int)EnumHosWorkOrderState.sure)
                return Error("此工单已被确认不能修改！");
            #region 添加条件   
            if (hosid <= 0)
                return Error("请选择院区！");
            if (deptid == 0)
                return Error("请选择科室！");
            if (usercode == "")
                return Error("请选择申请人！");
            if (phon == "")
                return Error("请输入联系电话！");
            if (bedmess == "")
                return Error("请填写正确的病床转运信息！");
            #endregion
            model.F_ProposerDeptId = deptid;
            model.F_ProposerDeptName = deptname;
            model.F_ProposerCode = usercode;//申请人
            model.F_ProposerName = username;
            model.F_ProposerPhon = phon;
            model.F_Remark = remark;
            model.F_ControlManCode = User.F_UserCode;
            model.F_ControlManName = User.F_UserName;//调度人
            model.F_BedMesItem = bedmess;
            #region 病床信息处理
            string[] _list = bedmess.TrimEnd(',').Split(',');
            string bedmessages = "";
            int bednum = 0;
            try
            {
                for (int i = 0; i < _list.Length; i++)
                {
                    string[] _temp = _list[i].Split('-');
                    int num = Convert.ToInt32(_temp[0]);
                    string needmess = _temp[1];
                    if (num > 0)
                    {
                        if (needmess != "")
                        {
                            bedmessages = string.IsNullOrEmpty(bedmessages) ? ("病床数量" + num + "个,配送要求:" + needmess) : (bedmessages + " , " + "病床数量" + num + "个,配送要求:" + needmess);
                        }
                        else
                        {
                            bedmessages = string.IsNullOrEmpty(bedmessages) ? "病床数量" + num + "个,无配送要求" : bedmessages + "," + "病床数量" + num + "个,无配送要求";
                        }
                        bednum += num;
                    }
                    else return Error("病床数量不正确！");
                }
            }
            catch { return Error("配送信息填写格式错误！"); }
            #endregion
            model.F_BedNeedMessage = bedmessages;//病床配送信息
            model.F_BedNum = bednum;//病床数量
            bool n = orderbll.Update(model);
            if (n)
            {
                int clbm = 2002;//设备科
                logbll.AddLog(woid, 0, (int)EnumWorkOrder.Hos, User.F_UserName + "(" + User.F_UserCode + ")" + "修改工单", (int)EnumHosItemType.update, (int)EnumHosItemOpt.update, "", 0, User.F_UserCode);
                //AssignWO(User1, model, "", "", clbm);
                return Success("操作成功！");
            }
            else return Error("添加失败");
            }
            return Error("无操作权限");
        }


        /// <summary>
        /// 转派工单-设备科
        /// </summary>
        private bool AssignWO(Model.T_Sys_UserAccount nowUser, Model.T_Hos_WorkOrder wokerModel, string cont, string usercode = "", int clbm = 0)
        {
            string touser = ""; string tousername = "";
            int deptid = 0;
            #region 获取接收人
            if (usercode != "")
            {
                Model.T_Sys_UserAccount clus = userbll.GetModel(usercode);

                deptid = clus.F_DeptId;
                clbm = clus.F_DeptId;
                if (clus != null)
                {
                    touser = clus.F_UserCode;
                    tousername = clus.F_UserName + "(" + clus.F_UserCode + ")";
                }
            }
            else
            {
                tousername = "设备科";

            }

            #endregion

            #region 工单处理
            var opt = "指派";
            int optbut = (int)EnumHosItemOpt.assign;
            int wostate = (int)EnumHosWorkOrderState.assign;
            var optcont = "";
            if (!string.IsNullOrEmpty(cont))
                optcont = "，说明：" + cont;
            var content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt + "工单给 " + tousername + optcont;
            var itemid = logbll.AddLog(wokerModel.F_Id, wostate, (int)EnumWorkOrder.Hos,content, (int)EnumDevItemType.deal, optbut, touser, clbm, nowUser.F_UserCode);
            #endregion
            if (itemid > 0)
            {
                wokerModel.F_State = wostate;
                orderbll.Update(wokerModel);
                return true;
            }
            else return false;

        }

        /// <summary>
        /// 设备科确认
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="bedposition"></param>
        /// <param name="sureman"></param>
        /// <param name="phon"></param>
        /// <returns></returns>
        public ActionResult SureWo(int woid, string bedposition, string surecode, string sureman, string phon)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (bedposition == "")
                    return Error("病床位置不能为空！");
                if (surecode == "" || phon == "")
                    return Error("确认人和联系电话不能为空！");
                if (omodel != null)
                {
                    omodel.F_BedPosition = bedposition;
                    omodel.F_BedSureBy = surecode;
                    omodel.F_BedSureByName = sureman;
                    omodel.F_BedSurePhon = phon;
                    omodel.F_OutboundTime = DateTime.Now;//出库时间
                    omodel.F_State = (int)EnumHosWorkOrderState.sure;//已确认待指派
                    if (orderbll.Update(omodel))
                    {
                        #region 写入工单操作记录
                       logbll.AddLog(woid, (int)EnumHosWorkOrderState.sure, (int)EnumWorkOrder.Hos, User.F_UserName + "(" + User.F_UserCode + ")" + "已确认", (int)EnumHosItemType.deal, (int)EnumHosItemOpt.sure, "", 0, User.F_UserCode);
                        #endregion
                    }
                    return Success("操作成功！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 转派工单-转运人
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="transMestr"></param>
        /// <param name="iscooperate">是否协作</param>
        /// <returns></returns>
        public ActionResult AssginWo(int woid, string transMestr, bool iscooperate)
        {
            if (User != null)
            {
                string transperson = "";
                string transcodestr = "";
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (transMestr == "")
                        return Error("转运人的信息不能为空！");
                    string[] _list = transMestr.TrimEnd(',').Split(',');
                    try
                    {
                        for (int i = 0; i < _list.Length; i++)
                        {
                            string[] _temp = _list[i].Split('|');
                            string code = _temp[0];
                            string phon = _temp[1];
                            string talkie = _temp[2];
                            string name = "";
                            if (code != "" && phon != "")
                            {
                                try { name = userbll.GetModel(code).F_UserName; }
                                catch { return Error("转运人信息不正确"); }
                                bool n = AssignTrans(User, omodel, "", code, phon, talkie);
                                if (n)
                                {
                                    transperson = string.IsNullOrEmpty(transperson) ? name + "(" + code + ")" : transperson + "," + name + "(" + code + ")";
                                    transcodestr = string.IsNullOrEmpty(transcodestr) ? code : transcodestr + "," + code;
                                    continue;
                                }
                            }
                            else return Error("转运人编号和手机号不能为空！");
                        }
                    }
                    catch
                    {
                        return Error("转运人信息格式不正确！");
                    }
                    //omodel.F_ControlManCode = ua.F_UserCode;//调度人
                    //omodel.F_ControlManName = ua.F_UserName;
                    omodel.F_TransPersonCode = transcodestr;//转派人编码字符串
                    omodel.F_TransPerson = transperson;
                    omodel.F_State = (int)EnumHosWorkOrderState.work;//已指派待查收
                    if (iscooperate)
                    {
                        omodel.F_IsCooperate = 1;
                        omodel.F_CooperateDept = "维运中心";
                    }
                    if (orderbll.Update(omodel))
                        return Success("转派成功！");
                    else return Error("转派失败！");
                }
                return Error("转派失败,不存在此工单编号！");
            }
            return Error("无操作权限！");

        }


        /// <summary>
        /// 工单查收
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult ReceiveWo(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    string receiveman = "";
                    var optitem = itembll.GetModelList($"F_WoID={woid} and F_IsUsed=0   and F_OptType="+(int)EnumHosItemOpt.assign+ " order by F_ID desc").Where(t=>t.F_NextUser!="").Select(p => p.F_NextUser).ToArray();
                    if (optitem != null)
                        receiveman = string.Join(",", optitem);
                    if (receiveman.Contains(User.F_UserCode))
                    {
                        if (DealWO(User, omodel, 1))
                            return Success("操作成功，请尽快处理工单！");
                    }
                    else return Error("无操作权限！");

                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 工单转运
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult TransferWo(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (DealWO(User, omodel, 2))
                        return Success("操作成功，请尽快处理工单！");
                    else return Error("工单操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 工单完成
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult FinishWo(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (DealWO(User, omodel, 3))
                        return Success("操作成功!");
                    else return Error("工单操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 维运中心派单
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="transMestr"></param>
        /// <param name="assignman"></param>
        /// <returns></returns>
        public ActionResult OperationWo(int woid, string transMestr, string assigncode, string assignname)
        {
            if (User != null)
            {
                string transperson = "";
                string transcodestr = "";
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null && omodel.F_State == (int)EnumHosWorkOrderState.transcomplete && omodel.F_IsCooperate == 1)
                {
                    #region 创建新的工单
                    omodel.F_WorkOrderCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");//工单编号
                    omodel.F_CreateTime = DateTime.Now;
                    omodel.F_CreateCode = User.F_UserCode;
                    omodel.F_CreateName = User.F_UserName;
                    omodel.F_State = (int)EnumHosWorkOrderState.neworder;//新工单 待转派
                    omodel.F_WoFlag = 1;//工单标记 新派生工单
                    omodel.F_WoID = omodel.F_Id;
                    omodel.F_OperationCode = assigncode;
                    omodel.F_OperationName = assignname;
                    #endregion
                    int n = orderbll.Add(omodel);
                    if (n > 0)
                    {
                        var newmodel = orderbll.GetModel(n);
                        var oldmodel = orderbll.GetModel((int)newmodel.F_WoID);
                        logbll.AddLog(n, 0, (int)EnumWorkOrder.Hos, User.F_UserName + "(" + User.F_UserCode + ")" + "创建新工单", (int)EnumHosItemType.deal, (int)EnumHosItemOpt.create, "", 0, User.F_UserCode);
                        logbll.AddLog(oldmodel.F_Id, (int)oldmodel.F_State, (int)EnumWorkOrder.Hos, User.F_UserName + "(" + User.F_UserCode + ")" + "创建新工单", (int)EnumHosItemType.deal, (int)EnumHosItemOpt.create, "", 0, User.F_UserCode);
                        #region 处理转运人信息
                        if (transMestr == "")
                            return Error("转运人的信息不能为空！");
                        string[] _list = transMestr.TrimEnd(',').Split(',');
                        for (int i = 0; i < _list.Length; i++)
                        {
                            string[] _temp = _list[i].Split('|');
                            string code = _temp[0];
                            string phon = _temp[1];
                            string talkie = _temp[2];
                            string name = "";
                            if (code != "" && phon != "")
                            {
                                try { name = userbll.GetModel(code).F_UserName; }
                                catch { return Error("转运人信息不正确"); }
                                bool t = AssignTrans(User, newmodel, "", code, phon, talkie,2);
                                if (t)
                                {

                                    transperson = string.IsNullOrEmpty(transperson) ? name + "(" + code + ")" : transperson + "," + name + "(" + code + ")";
                                    transcodestr = string.IsNullOrEmpty(transcodestr) ? code : transcodestr + "," + code;
                                    continue;
                                }
                            }
                            else return Error("转运人编号和手机号不能为空！");
                        }
                        #endregion
                        logbll.AddLog(oldmodel.F_Id, (int)EnumHosWorkOrderState.awaitfetch, (int)EnumWorkOrder.Hos, User.F_UserName + "(" + User.F_UserCode + ")" + "指派工单给" + transperson, (int)EnumDevItemType.deal, (int)EnumHosItemOpt.assign, transcodestr, 0, User.F_UserCode);
                        oldmodel.F_State = (int)EnumHosWorkOrderState.awaitfetch;//已指派待取件
                        newmodel.F_ActorCode = transcodestr;//转派人编码字符串
                        newmodel.F_ActorName = transperson;
                        newmodel.F_State = (int)EnumHosWorkOrderState.awaitfetch;//已指派待取件
                        orderbll.Update(newmodel);
                        orderbll.Update(oldmodel);
                        return Success("转派成功！");
                    }
                    else return Error("转派失败！");
                }
                return Error("转派失败！");
            }
            return Error("无操作权限！");
        }


        /// <summary>
        /// 取配件
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult FetchParts(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (DealWO(User, omodel, 4))
                        return Success("操作成功，请尽快处理工单！");
                    else return Error("工单操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 安装配件
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult InstallParts(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (DealWO(User, omodel, 5))
                        return Success("操作成功，请尽快处理工单！");
                    else return Error("工单操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 安装完成
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult InstallFinish(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (DealWO(User, omodel, 6))
                        return Success("操作成功，请尽快处理工单！");
                    else return Error("工单操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限！");

        }



        /// <summary>
        /// 完结工单
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        //public ActionResult OverWo(int woid, string sureman, string phon, string talkie)
        //{
        //    if (User != null)
        //    {
        //        if (woid == 0)
        //            return Error("工单编号错误！");
        //        if (string.IsNullOrEmpty(sureman))
        //            return Error("请填写确认人");
        //        var user = userbll.GetModel(sureman);
        //        if (user == null)
        //            return Error("不存在此确认人信息请正确填写！");
        //        var omodel = orderbll.GetModel(woid);
        //        if (omodel != null)
        //        {
        //            //if (omodel.F_CreateCode != usercode)
        //            //    return Error("无操作权限！");
        //            if (omodel.F_State != (int)EnumHosWorkOrderState.installcomplete)
        //                return Error("此工单未安装结束不能完结！");
        //            //if (DealWO(User, omodel, 7))
        //            var n = AddLog(woid, (int)omodel.F_State, User.F_UserName + "(" + User.F_UserCode + ")" + "确认完结工单", (int)EnumDisItemType.deal, (int)EnumDisItemOpt.over, "", 0, User);
        //            if (n > 0)
        //            {
        //                omodel.F_State = (int)EnumDisWorkOrderState.finish;
        //                omodel.F_FinishTime = DateTime.Now;
        //                orderbll.Update(omodel);
        //                return Success("操作成功！");
        //            }
        //            else return Error("操作失败！");
        //        }
        //        return Error("操作失败,不存在此工单编号！");
        //    }
        //    return Error("无操作权限！");
        //}
        /// <summary>
        /// 完结工单
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult OverWo(int woid, string sureman, string phon, string talkie)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                if (string.IsNullOrEmpty(sureman))
                    return Error("请填写确认人");
                var user = userbll.GetModel(sureman);
                if (user == null)
                    return Error("不存在此确认人信息请正确填写！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (omodel.F_State != (int)EnumHosWorkOrderState.installcomplete && omodel.F_State != (int)EnumHosWorkOrderState.transcomplete)
                    {
                        return Error("此工单未完成不能完结！");
                    }
                    var n = AddLogTrans(woid, (int)omodel.F_State, user.F_UserName + "(" + user.F_UserCode + ")" + "确认完结工单", (int)EnumDisItemType.deal, (int)EnumDisItemOpt.over, "", "", phon, talkie, 0, User);
                    //if (DealWO(ua, omodel, "", "", 6))
                    if (n > 0)
                    {
                        #region 更新工单

                        omodel.F_FinishTime = DateTime.Now;
                        omodel.F_State = (int)EnumHosWorkOrderState.finish;
                        omodel.F_FinishMan = sureman;
                        omodel.F_FinishManName = user.F_UserName;
                        omodel.F_FinishManPhon = phon;
                        omodel.F_FinishManTalkie = talkie;
                        orderbll.Update(omodel);
                        #endregion
                        #region 原工单状态更新
                        if (omodel.F_IsCooperate == 1 && omodel.F_WoFlag == 1 && omodel.F_WoID > 0)
                        {
                            var oldmodel = orderbll.GetModel(Convert.ToInt32(omodel.F_WoID));
                            oldmodel.F_FinishTime = DateTime.Now;
                            oldmodel.F_State = (int)EnumHosWorkOrderState.finish;
                            oldmodel.F_FinishMan = sureman;
                            oldmodel.F_FinishManName = user.F_UserName;
                            oldmodel.F_FinishManPhon = phon;
                            oldmodel.F_FinishManTalkie = talkie;
                            orderbll.Update(oldmodel);
                        }
                        #endregion
                        return Success("操作成功！");
                    }
                    else return Error("操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            return Error("无操作权限!");
        }

        /// <summary>
        ///处理工单
        /// </summary>
        /// <param name="nowUser"></param>
        /// <param name="model"></param>
        /// <param name="cont"></param>
        /// <param name="F_Files"></param>
        /// <param name="isover"></param>
        /// <returns></returns>
        public bool DealWO(Model.T_Sys_UserAccount nowUser, Model.T_Hos_WorkOrder model, int type = 1)
        {
            #region 工单处理

            var opt = "";
            int optbut = 0;
            int wostate = 0;
            int wostate1 = 0;
            string[] transcode = { };
            int flag = 0;
            var fmodel = new Model.T_Hos_WorkOrder();
            if (type == 1)
            {
                opt = "查收工单";
                optbut = (int)EnumHosItemOpt.receive;//查收
                wostate1= (int)EnumHosWorkOrderState.receive;//已查收待转运
            }
            if (type == 2)
            {
                opt = "转运工单";
                optbut = (int)EnumHosItemOpt.transport;//转运
                wostate1 = (int)EnumHosWorkOrderState.transport;//转运中
            }
            if (type == 3)
            {
                opt = "完成工单";
                optbut = (int)EnumHosItemOpt.transcomplete;//完成
                wostate1 = (int)EnumHosWorkOrderState.transcomplete;//已完成
            }
            if (type == 4)
            {
                opt = "取配件";
                optbut = (int)EnumHosItemOpt.fetch;//去配件
                wostate1 = (int)EnumHosWorkOrderState.fetch;//待取配件
            }
            if (type == 5)
            {
                opt = "安装配件";
                optbut = (int)EnumHosItemOpt.install;//安装配件
                wostate1 = (int)EnumHosWorkOrderState.install;//待安装
            }
            if (type == 6)
            {
                opt = "安装完成";
                optbut = (int)EnumHosItemOpt.installcomplete;//安装完成
                wostate1 = (int)EnumHosWorkOrderState.installcomplete;//安装完成
            }
            //#region 读取当前登录人部门
            //string deptname = "";
            //var deptmodel = deptbll.GetModel(nowUser.F_DeptId);
            //if (deptmodel != null)
            //{
            //    deptname = deptmodel.F_DeptName + "-";
            //}
            //#endregion
            var content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt;
            var itemid =logbll.AddLog(model.F_Id, wostate1, (int)EnumWorkOrder.Hos, content, (int)EnumDevItemType.deal, optbut, "", 0, nowUser.F_UserCode);
            if (itemid > 0)
            {
                #region 判断工单的状态
                if (!string.IsNullOrEmpty(model.F_TransPersonCode) && type == 1 || type == 2 || type == 3)
                {
                    int i = 0;
                    transcode = model.F_TransPersonCode.Split(',');
                    if (transcode.Count() > 0)
                    {
                        foreach (var item in transcode)
                        {
                            if (type == 1)
                            {
                                if (itembll.GetModelList(" F_WoID=" + model.F_Id + " and F_OptType=" + (int)EnumHosItemOpt.receive + "and F_CreateUser=" + item.ToString() + "").FirstOrDefault() != null)
                                    i++;
                            }
                            else if (type == 2)
                            {
                                if (itembll.GetModelList(" F_WoID=" + model.F_Id + " and F_OptType=" + (int)EnumHosItemOpt.transport + " and F_CreateUser=" + item.ToString() + "").FirstOrDefault() != null)
                                    i++;
                            }
                            else if (type == 3)
                            {
                                if (itembll.GetModelList(" F_WoID=" + model.F_Id + " and F_OptType=" + (int)EnumHosItemOpt.transcomplete + " and F_CreateUser=" + item.ToString() + "").FirstOrDefault() != null)
                                    i++;
                            }
                        }
                        if (i == transcode.Count())
                        {
                            if (type == 1)
                            {
                                wostate = (int)EnumHosWorkOrderState.receive;//已查收待转运
                                flag = 1;
                            }
                            else if (type == 2)
                            {
                                wostate = (int)EnumHosWorkOrderState.transport;//转运中
                                flag = 1;
                            }
                            else if (type == 3)
                            {
                                wostate = (int)EnumHosWorkOrderState.transcomplete;//已完成
                                flag = 1;
                            }
                        }
                        else
                        {
                            wostate = (int)model.F_State;//工单状态不变
                        }
                    }

                }
                if (!string.IsNullOrEmpty(model.F_ActorCode) && type == 4 || type == 5 || type == 6)
                {
                    logbll.AddLog((int)model.F_WoID, (int)EnumWorkOrder.Hos,(int)model.F_State, content, (int)EnumDevItemType.deal, 0, "", 0, nowUser.F_UserCode);
                    int i = 0;
                    transcode = model.F_ActorCode.Split(',');
                    if (transcode.Count() > 0)
                    {
                        foreach (var item in transcode)
                        {
                            if (type == 4)
                            {
                                if (itembll.GetModelList(" F_WoID=" + model.F_Id + " and F_OptType=" + (int)EnumHosItemOpt.fetch + "and F_CreateUser=" + item.ToString() + "").FirstOrDefault() != null)
                                    i++;
                            }
                            else if (type == 5)
                            {
                                if (itembll.GetModelList(" F_WoID=" + model.F_Id + " and F_OptType=" + (int)EnumHosItemOpt.install + " and F_CreateUser=" + item.ToString() + "").FirstOrDefault() != null)
                                    i++;
                            }
                            else if (type == 6)
                            {
                                if (itembll.GetModelList(" F_WoID=" + model.F_Id + " and F_OptType=" + (int)EnumHosItemOpt.installcomplete + " and F_CreateUser=" + item.ToString() + "").FirstOrDefault() != null)
                                    i++;
                            }
                        }
                        if (i == transcode.Count())
                        {
                            if (type == 4)
                            {
                                wostate = (int)EnumHosWorkOrderState.fetch;//待取配件
                                flag = 1;
                            }
                            else if (type == 5)
                            {
                                wostate = (int)EnumHosWorkOrderState.install;//待安装
                                flag = 1;
                            }
                            else if (type == 6)
                            {
                                wostate = (int)EnumHosWorkOrderState.installcomplete;//安装完成
                                flag = 1;
                            }
                        }
                        else
                        {
                            wostate = (int)model.F_State;//工单状态不变
                        }
                    }

                }
                #endregion
                #region 处理工单      
                if (flag == 1)
                {
                    model.F_State = wostate;
                    orderbll.Update(model);
                    if (type == 4 || type == 5 || type == 6)
                    {
                        var oldmodel = orderbll.GetModel(Convert.ToInt32(model.F_WoID));
                        oldmodel.F_State = wostate;
                        orderbll.Update(oldmodel);
                    }
                }
                if (type == 6)
                {
                    model.F_FinishTime = DateTime.Now;
                    orderbll.Update(model);
                }              
                #endregion
                return true;
            }
            else
                return false;
            #endregion
        }


        /// <summary>
        /// 转派工单
        /// </summary>
        private bool AssignTrans(Model.T_Sys_UserAccount nowUser, Model.T_Hos_WorkOrder wokerModel, string cont, string usercode, string userphon, string usertalkie,int flag=1)
        {
            string touser = ""; string tousername = "";
            int clbm = 0;
            Model.T_Sys_UserAccount clus = new Model.T_Sys_UserAccount();
            #region 获取接收人
            if (usercode != "")
            {
                clus = userbll.GetModel(usercode);
                clbm = clus.F_DeptId;
                if (clus != null)
                {
                    touser = clus.F_UserCode;
                    tousername = clus.F_UserName + "(" + clus.F_UserCode + ")";
                }
            }
            #endregion
            #region 工单处理
            var opt = "指派";
            int optbut = 0;
            int wostate = 0;
            string content = "";
            long itemid = 0;
            var optcont = "";
            if (!string.IsNullOrEmpty(cont))
                optcont = "，说明：" + cont;
            optbut = (int)EnumHosItemOpt.assign;
            wostate = (int)EnumHosWorkOrderState.work;//已指派待查收
            if (flag == 2)
            {
                optbut = (int)EnumHosItemOpt.assign;
                wostate = (int)EnumHosWorkOrderState.awaitfetch;//已指派待取配件
            }
            content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt + "工单给 " + tousername + optcont;
            itemid = AddLogTrans(wokerModel.F_Id, wostate, content, (int)EnumHosItemType.deal, optbut, touser, clus.F_UserName, userphon, usertalkie, clbm, nowUser);
            #endregion
            if (itemid > 0)
            {
                return true;
            }
            else return false;

        }
        /// <summary>
        /// 添加工单记录
        /// </summary>
        public long AddLogTrans(int woid, int wostate, string content, int itemtype, int opttype, string nextuser, string nextusername, string nextuserphon, string nextusertalkie, int nextdept, Model.T_Sys_UserAccount nowUser, int F_IsUsed = 0)
        {

            Model.T_Hos_WorkOrderItem itemModel = new Model.T_Hos_WorkOrderItem();
            #region 
            //处理当前工单的记录之前的记录过期
            var itemlast = itembll.GetModelList($" F_OptType !=1 and isnull(F_IsUsed,'0')='0' and F_ItemType=" + itemtype + " and  F_WoID=" + woid + " and  (F_CreateUser=" + nowUser.F_UserCode + ")");
            if (F_IsUsed == 0)
            {

                if (itemlast.Count > 0)
                {
                    foreach (var it in itemlast)
                    {
                        it.F_IsUsed = 1;
                        itembll.Update(it);
                    }
                }
            }
            #endregion
            itemModel.F_WoID = woid;
            itemModel.F_WoState = wostate;
            itemModel.F_ItemType = itemtype;
            itemModel.F_OptType = opttype;
            itemModel.F_OptContent = content;
            itemModel.F_NextUser = nextuser;
            itemModel.F_NextUserName = nextusername;
            itemModel.F_NextUserTalkie = nextusertalkie;
            itemModel.F_NexUserPhon = nextuserphon;
            itemModel.F_NextDeptId = nextdept;
            itemModel.F_IsUsed = F_IsUsed;
            itemModel.F_CreateTime = DateTime.Now;
            itemModel.F_CreateUser = nowUser.F_UserCode;
            var res = itembll.Add(itemModel);
            return res;
        }
        /// <summary>
        /// 添加工单记录
        /// </summary>
        //public long AddLog(int woid, int wostate, string content, int itemtype, int opttype, string nextuser, int nextdept, Model.T_Sys_UserAccount nowUser, int F_IsUsed = 0)
        //{

        //    Model.T_Hos_WorkOrderItem itemModel = new Model.T_Hos_WorkOrderItem();
        //    #region 
        //    //处理当前工单的记录之前的记录过期
        //    var itemlast = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and F_ItemType=" + itemtype + " and  F_WoID=" + woid + " and  (F_CreateUser=" + nowUser.F_UserCode + " or F_NextUser like '%" + nowUser.F_UserCode + "%')");
        //    if (F_IsUsed == 0)
        //    {

        //        if (itemlast.Count > 0)
        //        {
        //            foreach (var it in itemlast)
        //            {
        //                it.F_IsUsed = 1;
        //                itembll.Update(it);
        //            }
        //        }
        //    }
        //    #endregion
        //    itemModel.F_WoID = woid;
        //    itemModel.F_WoState = wostate;
        //    itemModel.F_ItemType = itemtype;
        //    itemModel.F_OptType = opttype;
        //    itemModel.F_OptContent = content;
        //    itemModel.F_NextUser = nextuser;
        //    itemModel.F_NextDeptId = nextdept;
        //    itemModel.F_IsUsed = F_IsUsed;
        //    itemModel.F_CreateTime = DateTime.Now;
        //    itemModel.F_CreateUser = nowUser.F_UserCode;
        //    var res = itembll.Add(itemModel);
        //    return res;
        //}


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelWorkOrder(string[] ids)
        {
            string usercode = User.F_UserCode;
            if (usercode != "")
            {
                if (ids != null && ids.Length > 0)
                {
                    string strid = "";
                    foreach (string str in ids)
                    {
                        strid += str + ",";
                    }
                    if (orderbll.CustomDelete(strid, usercode))
                    {
                        return Success("删除成功！");
                    }
                    else return Error("删除失败！");
                }
                return Error("请选择要删除的记录！");

            }
            return Error("无操作权限！");
        }

    }
}