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

namespace RMYY_CallCenter_Api.Controllers.WorkOrder
{
    public class T_Dev_WorkOrderController : BaseController
    {
        private Bll.T_Dev_Operate optbll = new Bll.T_Dev_Operate();
        private Bll.T_Dev_WorkOrder orderbll = new Bll.T_Dev_WorkOrder();
        private Bll.T_Dev_WorkOrderItem itembll = new Bll.T_Dev_WorkOrderItem();
        private Bll.T_Sys_UserAccount userbll = new Bll.T_Sys_UserAccount();
        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 && state<90)
                {
                    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 == "SBK")//设备管理部
                {
                    if (state == 5)
                    {
                        sql += $" and F_DevOutState in(1,2) and F_WoFlag=1";
                    }
                    else if (state == 7)
                    {
                        sql += $" and F_DevOutState in(3)";
                    }
                }
                else if (User.F_DeptCode == "LC")
                {
                    if (state == 5 || state == 6)
                    {
                       sql += $" and F_WoFlag=0 and  not exists(select * from T_Dev_WorkOrder b where b.F_WoID=aa.F_Id)";
                    }
                    if (state == 99)
                    {
                        sql += $" and F_WoFlag=1";
                    }
                    sql += $" and F_ProposerDeptId={User.F_DeptId}";
                }
                else if (User.F_RoleCode == "ZYY")
                {
                    transql = GetTranPersonsql(User.F_UserCode);                  
                    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_Dev_WorkOrder aa",
                        "F_Id",
                        "*,(select F_DeptName from T_Sys_Department  where F_DeptId= aa.F_ProposerDeptId)as F_DeptName",
                        sql,
                        "ORDER BY F_Id desc",
                        pagesize,
                        pageindex,
                        true,
                        out recordCount
                        );
                }
                var modellist = orderbll.DataTableToList(data);
                if (modellist.Count > 0)
                {
                    foreach (var item in modellist)
                    {
                        item.F_Wostate = EnumHelper.GetDescription((EnumDevWorkOrderState)item.F_State);
                        item.F_DevState = EnumHelper.GetDescription((EnumDevState)item.F_DevOutState);
                        item.F_SitMan = item.F_ControlManName + '(' + item.F_ControlManName + ')';
                        item.F_ProposerDeptName = new Bll.T_Sys_Department().GetModel(Convert.ToInt32(item.F_ProposerDeptId))?.F_DeptName ?? "";

                    }
                    //var obj = new
                    //{
                    //    state = "success",
                    //    message = "成功",
                    //    rows = modellist.Select(p =>
                    //    new
                    //    { Wostate = Enum.GetName(typeof(EnumDevWorkOrderState),p.F_State),
                    //      DevState= Enum.GetName(typeof(EnumDevState),p.F_DevOutState),
                    //      SitMan=p.F_ControlManName+'('+p.F_ControlManName+')',
                    //      modellist = p
                    //    }),
                    //    total = recordCount
                    //};                
                }
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = modellist,
                    total = recordCount
                };
                return Content(obj.ToJson());
            }
            return Error("无操作权限！");
        }


        private string GetTransql(string usercode)
        {
            string str = string.Empty;
            str = "select F_WoID from T_Dev_WorkOrderItem where F_OptType=" + (int)EnumDevWorkOrderState.assign + ",and F_CreateUser=" + usercode;
            return str;
        }
        private string GetTranPersonsql(string usercode)
        {
            string wokerlist = "";
            string[] wokerarr = { };
            var itemlist = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and (F_CreateUser=" + usercode + " or  F_NextUser=" + usercode + ")").Select(t => t.F_WoID).Distinct<int>().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="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_Dev_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_Dev_WorkOrder aa",
                            "F_Id",
                            "*",
                            sql,
                            "ORDER BY F_Id desc",
                            pagesize,
                            pageindex,
                            true,
                            out recordCount
                            );
                    }
                    var modellist = orderbll.DataTableToList(data);
                    if (modellist.Count > 0)
                    {
                        foreach (var item in modellist)
                        {
                            item.F_Wostate = EnumHelper.GetDescription((EnumDevWorkOrderState)item.F_State);
                            item.F_DevState = EnumHelper.GetDescription((EnumDevState)item.F_DevOutState);
                            item.F_SitMan = item.F_ControlManName + '(' + item.F_ControlManName + ')';
                            item.HosName= new Bll.T_Wowo_repairyqbll().GetModel(item.F_HosId ?? 0)?.T_Woname ?? "";
                            item.F_ProposerDeptName = new Bll.T_Sys_Department().GetModel(Convert.ToInt32(item.F_ProposerDeptId))?.F_DeptName ?? "";

                        }                
                    }
                    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="id"></param>
        /// <returns></returns>
        public ActionResult GetModelDetail(int id)
        {
            if (id <= 0)
            {
                return Error("请选择要查询的工单！");
            }
            string sql = "F_WoID = " + id + "";
            var model = orderbll.GetModel(id);
            model.F_Wostate = EnumHelper.GetDescription((EnumDevWorkOrderState)model.F_State);
            model.HosName = new Bll.T_Wowo_repairyqbll().GetModel(model.F_HosId ?? 0)?.T_Woname??"";
            model.F_ProposerDeptName = new Bll.T_Sys_Department().GetModel(Convert.ToInt32(model.F_ProposerDeptId))?.F_DeptName??"";
            model.F_DevState = EnumHelper.GetDescription((EnumDevState)model.F_DevOutState);
            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("没有查询到此工单！");

        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="type1"></param>
        /// <param name="type2"></param>
        /// <param name="deptid"></param>
        /// <param name="usercode"></param>
        /// <param name="username"></param>
        /// <param name="phon"></param>
        /// <param name="devmessages"></param>
        /// <param name="opt"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public ActionResult AddWorkOrder(int hosid, int deptid, string usercode, string username, string phon, string devmessages, string remark, string callid = "")
        {
            if (User != null)
            {
                #region 添加条件              
                if (hosid <= 0)
                    return Error("请选择院区！");
                if (deptid == 0)
                    return Error("请选择科室！");
                if (usercode == "")
                    return Error("请选择申请人！");
                if (phon == "")
                    return Error("请输入联系电话！");
                if (devmessages == "")
                    return Error("请输入设备名称和数量！");
                #endregion

                var model = new Model.T_Dev_WorkOrder();
                model.F_Type =(int)EnumWorkOrder.Dev;//工单类型  1设备租赁
                model.F_Source = "PC端";
                model.F_CallID = callid;
                model.F_State = (int)EnumDevWorkOrderState.assign;//已转派
                model.F_WorkOrderCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");//工单编号
                model.F_ProposerDeptId = deptid;
                model.F_ProposerCode = usercode;//申请人
                model.F_ProposerName = username;
                model.F_ProposerPhon = phon;
                model.F_DealType = "转派";
                model.F_Remark = remark;
                model.F_CreateTime = DateTime.Now;
                model.F_CreateCode = User.F_UserCode;
                model.F_CreateName = User.F_UserName;
                model.F_CarryContents = devmessages.TrimEnd(',');//设备信息
                model.F_IsDel = 0;
                model.F_ControlManCode = User.F_UserCode;
                model.F_ControlManName = User.F_UserName;//调度人
                model.F_HosId = hosid;//院区id
                int n = orderbll.Add(model);
                if (n > 0)
                {
                    int clbm = 2002;//设备科
                    var wmodel = orderbll.GetModel(n);
                    logbll.AddLog(n, 0, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + "创建新工单", (int)EnumDevItemType.deal, (int)EnumDevItemOpt.create, "", 0, User.F_UserCode);
                    AssignWO(User, wmodel, "", "", clbm);
                    string[] _list = devmessages.TrimEnd(',').Split(',');
                    try
                    {
                        for (int i = 0; i < _list.Length; i++)
                        {
                            string[] _temp = _list[i].Split(':');
                            string name = _temp[0];
                            int num = Convert.ToInt32(_temp[1]);
                            var optmodel = new Model.T_Dev_Operate();
                            optmodel.F_DeviceName = name;
                            optmodel.F_NeedNum = num;
                            optmodel.F_WoId = n;
                            optmodel.F_State = 0;
                            optbll.Add(optmodel);
                        }
                    }
                    catch { return Error("设备配送信息格式错误！"); }
                    return Success("操作成功！");
                }
                else return Error("添加失败");
            }
            return Error("无操作权限");
        }


        /// <summary>
        /// 需求科添加工单
        /// </summary>
        /// <param name="type"></param>
        /// <param name="devmessages"></param>
        /// <param name="remark"></param>
        /// <param name="callid"></param>
        /// <returns></returns>
        public ActionResult AddByProposer(int type, string devmessages, string remark, string callid = "")
        {
            if (User != null)
            {
                #region 添加条件
                if (type <= 0)
                    return Error("工单类型错误！");
                if (devmessages == "")
                    return Error("请输入设备名称和数量！");
                #endregion

                var model = new Model.T_Dev_WorkOrder();
                model.F_Type = type;//工单类型  1设备租赁
                model.F_Source = "PC端";
                model.F_CallID = callid;
                model.F_State = (int)EnumDevWorkOrderState.assign;//已转派
                model.F_WorkOrderCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");//工单编号
                model.F_ProposerDeptId = User.F_DeptId;
                model.F_ProposerCode = User.F_UserCode;//申请人
                model.F_ProposerName = User.F_UserName;
                model.F_ProposerPhon = User.F_Telephone;
                model.F_Remark = remark;
                model.F_CreateTime = DateTime.Now;
                model.F_CreateCode = User.F_UserCode;
                model.F_CreateCode = User.F_UserName;
                model.F_CarryContents = devmessages.TrimEnd(',');//设备信息
                model.F_IsDel = 0;
                int n = orderbll.Add(model);
                if (n > 0)
                {
                    int clbm = 2002;//设备科
                    var wmodel = orderbll.GetModel(n);
                   logbll.AddLog(n, 0, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + "创建新工单", (int)EnumDevItemType.deal, (int)EnumDevItemOpt.create, "", 0, User.F_UserCode);
                    string[] _list = devmessages.TrimEnd(',').Split(',');
                    for (int i = 0; i < _list.Length; i++)
                    {
                        string[] _temp = _list[i].Split(':');
                        string name = _temp[0];
                        int num = Convert.ToInt32(_temp[1]);
                        var optmodel = new Model.T_Dev_Operate();
                        optmodel.F_DeviceName = name;
                        optmodel.F_NeedNum = num;
                        optmodel.F_WoId = n;
                        optmodel.F_State = 0;
                        optbll.Add(optmodel);
                    }
                    return Success("操作成功！");
                }
                else return Error("添加失败");
            }
            return Error("无操作权限");
        }
        /// <summary>
        /// 修改工单
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="deptid"></param>
        /// <param name="usercode"></param>
        /// <param name="username"></param>
        /// <param name="phon"></param>
        /// <param name="devmessages"></param>
        /// <param name="remark"></param>
        /// <param name="callid"></param>
        /// <returns></returns>
        public ActionResult UpdateWorkOrder(int woid, int hosid, int deptid, string usercode, string username, string phon, string devmessages, string remark, string callid = "")
        {
            if (User != null)
            {
                #region 添加条件
                if (woid <= 0)
                    return Error("工单编号不正确！");
                var wmodel = orderbll.GetModel(woid);
                if (wmodel == null)
                    return Error("不存在此工单信息！");
                if (hosid <= 0)
                    return Error("请选择院区！");
                if (deptid == 0)
                    return Error("请选择科室！");
                if (usercode == "")
                    return Error("请选择申请人！");
                if (phon == "")
                    return Error("请输入联系电话！");
                if (devmessages == "")
                    return Error("请输入设备名称和数量！");
                if (wmodel.F_State == (int)EnumDevWorkOrderState.checkout)
                    return Error("设备已出库不能修改！");
                #endregion
                wmodel.F_ProposerDeptId = deptid;
                wmodel.F_ProposerCode = usercode;//申请人
                wmodel.F_ProposerName = username;
                wmodel.F_ProposerPhon = phon;
                wmodel.F_Remark = remark;
                wmodel.F_CallID = callid;
                wmodel.F_CreateCode = User.F_UserCode;
                wmodel.F_CreateCode = User.F_UserName;
                wmodel.F_CarryContents = devmessages.TrimEnd(',');//设备信息
                wmodel.F_ControlManCode = User.F_UserCode;
                wmodel.F_ControlManName = User.F_UserName;//调度人
                wmodel.F_HosId = hosid;
                bool n = orderbll.Update(wmodel);
                if (n)
                {
                    int clbm = 2002;//设备科
                  logbll.AddLog(woid, (int)wmodel.F_State, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + "修改工单", (int)EnumDevItemType.update, (int)EnumDevItemOpt.update, "", 0, User.F_UserCode);
                    //AssignWO(User1, wmodel, "", "", clbm);
                    #region 删除之前记录
                    string sql = $"F_WoId=" + woid;
                    var optlist = optbll.GetModelList(sql);
                    if (optlist.Count > 0)
                    {
                        foreach (var item in optlist)
                        {
                            if (optbll.Delete(item.F_Id))
                                continue;
                        }
                    }
                    #endregion
                    string[] _list = devmessages.TrimEnd(',').Split(',');
                    for (int i = 0; i < _list.Length; i++)
                    {
                        string[] _temp = _list[i].Split(':');
                        string name = _temp[0];
                        int num = Convert.ToInt32(_temp[1]);
                        var optmodel = new Model.T_Dev_Operate();
                        optmodel.F_DeviceName = name;
                        optmodel.F_NeedNum = num;
                        optmodel.F_WoId = woid;
                        optmodel.F_State = 0;
                        optbll.Add(optmodel);
                    }
                    return Success("操作成功！");
                }
                else return Error("修改失败");
            }
            return Error("无操作权限");
        }


        /// <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_Dev_WorkOrderItem itemModel = new Model.T_Dev_WorkOrderItem();
        //    #region 
        //    //处理当前工单的记录之前的记录过期
        //    var itemlast = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and F_ItemType=" + itemtype + " and  F_WoID=" + woid + "");
        //    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>
        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_Dev_WorkOrderItem itemModel = new Model.T_Dev_WorkOrderItem();
            #region 
            //处理当前工单的记录之前的记录过期
            var itemlast = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and F_ItemType=" + itemtype + " and  F_WoID=" + woid + " ");
            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>
        private bool AssignWO(Model.T_Sys_UserAccount nowUser, Model.T_Dev_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)EnumDevItemOpt.assign;
                int wostate = (int)EnumDevWorkOrderState.assign;//已指派待出库
                //string deptname = "";
                //var deptmodel = deptbll.GetModel(nowUser.F_DeptId);
                //if (deptmodel != null)
                //{
                //    deptname = deptmodel.F_DeptName + "-";
                //}
                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.Dev, content, (int)EnumDevItemType.deal, optbut, touser, clbm, nowUser.F_UserCode);
                #endregion
                if (itemid > 0)
                {
                    wokerModel.F_State = wostate;
                if (!string.IsNullOrEmpty(cont)) 
                    orderbll.Update(wokerModel);
                    return true;
                }
                else return false;

        }


        /// <summary>
        /// 转派工单-设备配送
        /// </summary>
        private bool AssignTrans(Model.T_Sys_UserAccount nowUser, Model.T_Dev_WorkOrder wokerModel, string cont, string usercode, string username, string userphon, string usertalkie)
        {
            string touser = ""; string tousername = "";
            int clbm = 0;
            #region 获取接收人
            if (usercode != "")
            {
                Model.T_Sys_UserAccount 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;
            //if (type == 1)
            //{
            //    optbut = (int)EnumDevItemOpt.assign;
            //    wostate = (int)EnumDevWorkOrderState.work;//已指派待转运
            //    content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt + "工单给 " + tousername + optcont;
            //    itemid = AddLogTrans(wokerModel.F_Id, wostate, content, (int)EnumDevItemType.deal, optbut, touser, username, userphon, usertalkie,  clbm, nowUser);
            //}
            //else if (type == 2)
            //{
            //    optbut = (int)EnumDevItemOpt.assign;
            //    wostate = (int)EnumDevWorkOrderState.work;//已指派待转运
            //    content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt + "工单给 " + tousername + optcont;
            //    itemid = AddLogTrans(wokerModel.F_Id, wostate, content, (int)EnumDevItemType.deal, optbut, touser, username, userphon, usertalkie, clbm, nowUser);
            //    if (wokerModel.F_WoID > 0)
            //    {
            //      AddLogTrans(wokerModel.F_WoID, wostate, content, (int)EnumDevItemType.deal, optbut, touser, username, userphon, usertalkie, clbm, nowUser);
            //    }
            //}
            optbut = (int)EnumDevItemOpt.assign;
            wostate = (int)EnumDevWorkOrderState.work;//已指派待转运
            content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt + "工单给 " + tousername + optcont;
            itemid = AddLogTrans(wokerModel.F_Id, wostate, content, (int)EnumDevItemType.deal, optbut, touser, username, userphon, usertalkie, clbm, nowUser);
            if (wokerModel.F_WoID > 0)
            {
                AddLogTrans(wokerModel.F_WoID, (int)EnumDevWorkOrderState.receive, content, (int)EnumDevItemType.deal, optbut, touser, username, userphon, usertalkie, clbm, nowUser);
                //var omodel = orderbll.GetModel(wokerModel.F_WoID);
                //omodel.F_State= (int)EnumDevWorkOrderState.work;//已指派待转运
            }
            #endregion
            if (itemid > 0)
            {
                wokerModel.F_State = wostate;
                //if (!string.IsNullOrEmpty(cont))
                 orderbll.Update(wokerModel);
                return true;
            }
            else return false;

        }


        /// <summary>
        /// 出库
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="devstate"></param>
        /// <param name="devmessage"></param>
        /// <param name="handler"></param>
        /// <param name="phon"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public ActionResult DevCheckout(int woid,int devstate, string devmessage, string remark)
        {
            //设备管理科操作
            if (User != null)
            {
                string DevoutCounts = "";
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (devstate == 0)
                        return Error("请选择设备出库状态！");
                    if (devmessage == "")
                        return Error("出库设备名称和数量不能为空！");
                    string[] _list = devmessage.TrimEnd(',').Split(',');
                    try
                    {
                        for (int i = 0; i < _list.Length; i++)
                        {

                            string[] _temp = _list[i].Split('|');
                            string name = _temp[0];
                            int num = Convert.ToInt32(_temp[1]);
                            string code = _temp[2];
                            if (name != "" && num > 0)
                            {
                                string sql = $"F_DeviceName='" + name + "' and F_WoId=" + woid;
                                var optmodel = optbll.GetModelList(sql).FirstOrDefault();
                                optmodel.F_GiveNum = num;
                                optmodel.F_DeviceCode = code;
                                optmodel.F_OptPerson = User.F_UserCode;
                                optmodel.F_OptPersonName = User.F_UserName;
                                optmodel.F_OptPhon = User.F_Telephone;
                                optmodel.F_OutTime = DateTime.Now;
                                optmodel.F_Remark = remark;
                                if (num < optmodel.F_NeedNum)
                                    optmodel.F_State = 2;
                                else if (num == optmodel.F_NeedNum)
                                    optmodel.F_State = 1;
                                else return Error("出库设备数量大于需求数量！");
                                var n = optbll.Update(optmodel);
                                if (n)
                                {
                                    DevoutCounts += name + ":" + num + ",";
                                    continue;
                                }
                            }
                            else return Error("设备信息不正确！");
                        }
                    }
                    catch
                    {
                        return Error("设备出库信息格式错误!");
                    }
                    var optlist = optbll.GetModelList("F_WoId=" + woid);

                    //if (optlist.Count > 0)
                    //{
                    //    int NeedAmount = Convert.ToInt32(optlist.Sum(t => t.F_NeedNum));
                    //    int GiveAmount = Convert.ToInt32(optlist.Sum(t => t.F_GiveNum));
                    //    if (NeedAmount <= GiveAmount)
                    //        state = 1;//全部出库
                    //    else state = 2;//部分出库
                    //}
                    omodel.F_DevOutState = devstate;
                    omodel.F_HandleByCode = User.F_UserCode;//经办人
                    omodel.F_HandleByName = User.F_UserName;
                    omodel.F_HandleTime = DateTime.Now;//出库时间
                    omodel.F_State = (int)EnumDevWorkOrderState.checkout;//已出库待指派
                    omodel.F_HandleContents = DevoutCounts;
                    if (orderbll.Update(omodel))
                    {
                        #region 写入工单操作记录
                       logbll.AddLog(woid, (int)EnumDevWorkOrderState.checkout, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + "反馈设备已出库", (int)EnumDevItemType.deal, (int)EnumDevItemOpt.checkout, "", 0, User.F_UserCode);
                        #endregion
                        return Success("出库成功！");
                    }
                    else return Error("出库失败！");

                }
                return Error("出库失败,不存在此工单编号！");
            }
            return Error("无操作权限!");
        }
        /// <summary>
        /// 转派到设备科
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult AssignToDev(int woid)
        {
            if (User != null)
            {
                int clbm = 2002;//设备科
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    bool n = AssignWO(User, omodel, "", "", clbm);
                    if (n)
                    {
                        omodel.F_ControlManCode = User.F_UserCode;
                        omodel.F_ControlManName = User.F_UserName;
                        orderbll.Update(omodel);
                        return Success("转派成功！");
                    }
                    return Error("转派失败！");
                }
                return Error("转派失败,不存在此工单编号！");
            }
            return Error("无操作权限!");
        }

        /// <summary>
        /// 转派-设备运转
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="usercode"></param>
        /// <param name="username"></param>
        /// <param name="phon"></param>
        /// <param name="talkie"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public ActionResult AssignTransGo(int woid, string usercode, string username, string phon, string talkie, string remark)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                if (usercode == "")
                    return Error("配送员工号不能为空！");
                if (phon == "")
                    return Error("手机号不能为空！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    bool n = AssignTrans(User, omodel, remark, usercode, username, phon, talkie);
                    if (n)
                    {
                        return Success("转派成功！");
                    }
                    return Error("转派失败！");
                }
                return Error("转派失败,不存在此工单编号！");
            }
            else return Error("请重新登录！");
        }


        /// <summary>
        /// 转派-设备返还
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="usercode"></param>
        /// <param name="username"></param>
        /// <param name="phon"></param>
        /// <param name="talkie"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public ActionResult AssignTransOut(int woid, string usercode, string username, string phon, string talkie, string remark)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                if (usercode == "")
                    return Error("配送员工号不能为空！");
                if (phon == "")
                    return Error("手机号不能为空！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    bool n = AssignTrans(User, omodel, remark, usercode, username, phon, talkie);
                    if (n)
                    {
                        omodel.F_ControlManCode = User.F_UserCode;//调度人
                        omodel.F_ControlManName = User.F_UserName;
                        orderbll.Update(omodel);
                        return Success("转派成功！");
                    }
                    return Error("转派失败！");
                }
                return Error("转派失败,不存在此工单编号！");
            }
            else return Error("请重新登录！");
        }

        /// <summary>
        /// 处理-接收运转
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult SureTransGo(int woid)
        {
            if (User != null)
            {
                int state = 0;
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    var optitem = itembll.GetModelList($"F_WoID={woid} and F_IsUsed=0 order by F_ID desc ").FirstOrDefault();
                    if (optitem != null && optitem.F_NextUser != User.F_UserCode)
                        return Error("非接单人员不可操作工单！");
                    if (DealWO(User, omodel, "", "", 1))
                        return Success("操作成功，请尽快处理工单！");
                    else return Error("工单操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            else return Error("请重新登录！");
        }

        /// <summary>
        /// 处理-返还运转.
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult SureTransOut(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    var optitem = itembll.GetModelList($"F_WoID={woid} and F_IsUsed=0 order by F_ID desc ").FirstOrDefault();
                    if (optitem != null && optitem.F_NextUser != User.F_UserCode)
                        return Error("非接单人员不可操作工单！");
                    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,"","", 7))
                        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)
                {
                    if (DealWO(User, omodel, "", "", 3))
                        return Success("操作成功！");
                    else return Error("操作失败！");
                }
                return Error("操作失败,不存在此工单编号！");
            }
            else return Error("请重新登录！");
        }

        /// <summary>
        /// 返还设备
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult RebackWo(int woid)
        {
            if (User != null)
            {
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                Model.T_Dev_WorkOrder dmodel = new Model.T_Dev_WorkOrder();
                #region 返还发起新工单
                dmodel.F_Type = omodel.F_Type;//工单类型  1设备租赁
                dmodel.F_Source = "PC端";
                dmodel.F_CallID = omodel.F_CallID;
                dmodel.F_State = (int)EnumDevWorkOrderState.checkout;//已返还待指派
                dmodel.F_WorkOrderCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");//工单编号
                dmodel.F_ProposerDeptId = omodel.F_ProposerDeptId;
                dmodel.F_ProposerCode = omodel.F_ProposerCode;//申请人
                dmodel.F_ProposerName = omodel.F_ProposerName;
                dmodel.F_ProposerPhon = omodel.F_ProposerPhon;
                dmodel.F_DealType = "转派";
                dmodel.F_Remark = omodel.F_Remark;
                dmodel.F_CreateTime = DateTime.Now;
                dmodel.F_CreateCode = User.F_UserCode;
                dmodel.F_CreateName = User.F_UserName;
                dmodel.F_CarryContents = omodel.F_CarryContents;//设备信息
                dmodel.F_IsDel = 0;
                dmodel.F_DevOutState = omodel.F_DevOutState;
                dmodel.F_DevOutNum = omodel.F_DevOutNum;
                dmodel.F_WoFlag = 1;
                dmodel.F_WoID = omodel.F_Id;
                int n = orderbll.Add(dmodel);
                if (n > 0)
                {
                   logbll.AddLog(n, (int)EnumDevWorkOrderState.checkout, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + "创建返还工单", (int)EnumDevItemType.deal, (int)EnumDevItemOpt.create, "", 0, User.F_UserCode);
                  logbll.AddLog(woid, (int)EnumDevWorkOrderState.checkout, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + "创建返还工单", (int)EnumDevItemType.deal, (int)EnumDevItemOpt.create, "", 0, User.F_UserCode);
                    return Success("操作成功！");
                }
                omodel.F_WoID = n;//关联工单id
                orderbll.Update(omodel);
                #endregion
                //if (omodel != null)
                //{
                //    if (DealWO(ua, omodel, "", "", 4))
                //        return Success("操作成功！");
                //    else return Error("操作失败！");
                //}
                return Error("操作失败,不存在此工单编号！");
            }
            else 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_Dev_WorkOrder model, string cont, string F_Files = "", int type = 1)
        {
            #region 工单处理

            var opt = "";
            int optbut = 0;
            int wostate = 0;
            var fmodel = new Model.T_Dev_WorkOrder();
            if (type == 1)
            {
                opt = "运转设备";
                optbut = (int)EnumDevItemOpt.work;//运转
                wostate = (int)EnumDevWorkOrderState.transport;//接收转运中
                if (!string.IsNullOrEmpty(model.F_TransGoPerson))
                {
                    model.F_TransGoPerson += ',' + nowUser.F_UserName + "(" + nowUser.F_UserCode + ")";
                }
                else
                {
                    model.F_TransGoPerson = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")";
                }
            }
            if (type == 2)
            {
                fmodel = orderbll.GetModel(model.F_WoID);
                opt = "返还运转设备";
                optbut = (int)EnumDevItemOpt.work;
                wostate = (int)EnumDevWorkOrderState.transport;//返还转运中
                model.F_TransOutPerson = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")";
                if (!string.IsNullOrEmpty(fmodel.F_TransOutPerson))
                {
                    fmodel.F_TransOutPerson+= ',' + nowUser.F_UserName + "(" + nowUser.F_UserCode + ")";
                }
                else
                {
                    fmodel.F_TransOutPerson = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")";
                }
            }
            if (type == 3)
            {
                opt = "接收设备";
                optbut = (int)EnumDevItemOpt.receive;
                wostate = (int)EnumDevWorkOrderState.receive;//已接收待返还
                model.ReceiveTime = DateTime.Now;
            }
            if (type == 4)
            {
                opt = "返还设备";
                optbut = (int)EnumDevItemOpt.reback;
                wostate = (int)EnumDevWorkOrderState.checkout;//已返还待指派
            }
            if (type == 5)
            {
                opt = "异常结束工单";
                optbut = (int)EnumDevItemOpt.abreback;
                wostate = (int)EnumDevWorkOrderState.abreback;//异常结束
            }
            if (type == 6)
            {
                opt = "完结工单";
                optbut = (int)EnumDevItemOpt.end;
                wostate = (int)EnumDevWorkOrderState.finish;//已完结
            }
            if (type == 7)
            {
                opt = "运转完成工单";
                optbut = (int)EnumDevItemOpt.over;
                wostate = (int)EnumDevWorkOrderState.transover;//转运完成
            }
            //#region 读取当前登录人部门
            //string deptname = "";
            //var deptmodel = deptbll.GetModel(nowUser.F_DeptId);
            //if (deptmodel != null)
            //{
            //    deptname = deptmodel.F_DeptName + "-";
            //}
            //#endregion
            var optcont = "";
            if (!string.IsNullOrEmpty(cont))
                optcont = "，说明：" + cont;
            var content = nowUser.F_UserName + "(" + nowUser.F_UserCode + ")" + opt + optcont;
            var itemid =logbll.AddLog(model.F_Id, wostate, (int)EnumWorkOrder.Dev, content, (int)EnumDevItemType.deal, optbut, "", 0, nowUser.F_UserCode);
            if (type == 2 && model.F_WoID>0)
            {
               logbll.AddLog(model.F_WoID, (int)EnumDevWorkOrderState.receive, (int)EnumWorkOrder.Dev, content, (int)EnumDevItemType.deal, optbut, "", 0, nowUser.F_UserCode);
                orderbll.Update(fmodel);
            }
            if (itemid > 0)
            {
                #region 处理工单            
                model.F_State = wostate;
                if (type == 6)
                {
                 model.F_FinishTime = DateTime.Now;
                }
                orderbll.Update(model);
                #endregion
                return true;
            }
            else
                return false;
            #endregion
        }


        /// <summary>
        ///设备入库
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult DevStorage(int woid,string devmessage, string remark)
        {
            //设备管理科操作
            if (User != null)
            {
                int state = 0;
                int workstate = 0;
                string opt = "";
                var optcont = "";
                int totalnum = 0;
                string storContents = "";
                if (woid == 0)
                    return Error("工单编号错误！");
                var omodel = orderbll.GetModel(woid);
                if (omodel != null)
                {
                    if (devmessage == "")
                        return Error("入库设备名称和数量不能为空！");
                    string[] _list = devmessage.TrimEnd(',').Split(',');
                    for (int i = 0; i < _list.Length; i++)
                    {

                        string[] _temp = _list[i].Split('|');
                        string name = _temp[0];
                        int num = Convert.ToInt32(_temp[1]);
                        string code = _temp[2];
                        if (name != "" && num > 0)
                        {
                            string sql = $"F_DeviceName='" + name + "' and F_WoId=" + omodel.F_WoID;
                            var optmodel = optbll.GetModelList(sql).FirstOrDefault();
                            if (optmodel == null)
                                return Error("设备信息不正确！");
                            int? givenum = optmodel.F_GiveNum;
                            if (num > givenum)
                                return Error("入库数量不能大于出库数量！");
                            optmodel.F_RemainderNum += num;
                            int? getnum = optmodel.F_RemainderNum;
                            //optmodel.F_DeviceCode = code;
                            //optmodel.F_OptPerson = ua.F_UserCode;
                            //optmodel.F_OptPersonName = ua.F_UserName;
                            //optmodel.F_OptPhon = ua.F_Telephone;
                            //optmodel.F_OutTime = DateTime.Now;
                            //optmodel.F_Remark = remark;
                            if (getnum < givenum)
                                optmodel.F_State = 4;
                            else if (getnum == givenum)
                                optmodel.F_State = 3;
                            var n = optbll.Update(optmodel);
                            if (n)
                            {
                                if (!string.IsNullOrEmpty(storContents))
                                {
                                    storContents += $"," + name + num + "个";
                                }
                                else
                                {
                                    storContents = $"入库 " + name + num + "个";
                                }
                                totalnum += num;
                                continue;
                            }
                        }
                        else return Error("设备信息不正确！");
                    }
                    var optlist = optbll.GetModelList("F_WoId=" + omodel.F_WoID);

                    if (optlist.Count > 0)
                    {
                        int ReturnAmount = Convert.ToInt32(optlist.Sum(t => t.F_RemainderNum));
                        int GiveAmount = Convert.ToInt32(optlist.Sum(t => t.F_GiveNum));
                        if (ReturnAmount == GiveAmount)
                        {
                            state = 3;//全部入库库
                            workstate = (int)EnumDevWorkOrderState.storage;//已入库待完结
                            opt = "反馈设备已全部入库";
                        }
                        else if (ReturnAmount < GiveAmount)
                        {
                            state = 4;//部分入库
                            workstate = (int)EnumDevWorkOrderState.receive;//已接收待返还
                            opt = "反馈设备已部分入库";
                        }
                    }
                    omodel.F_DevOutState = state;
                    omodel.F_HandleByCode = User.F_UserCode;//经办人
                    omodel.F_HandleByName = User.F_UserName;
                    omodel.F_HandleTime = DateTime.Now;
                    omodel.F_State = (int)EnumDevWorkOrderState.storage;
                    omodel.F_DevInNum = totalnum;
                    omodel.F_DevName = storContents;
                    omodel.F_DevStorageTime = DateTime.Now;
                    if (orderbll.Update(omodel))
                    {
                        if (!string.IsNullOrEmpty(storContents))
                            optcont = "，说明：" + storContents;
                        #region 写入工单操作记录
                       logbll.AddLog(woid, workstate, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + opt + optcont, (int)EnumDevItemType.deal, (int)EnumDevItemOpt.storage, "", 0, User.F_UserCode);
                        if (omodel.F_WoID > 0)
                        {
                            var fmodel = orderbll.GetModel(omodel.F_WoID);
                            fmodel.F_DevInNum += totalnum;
                            fmodel.F_DevName = string.IsNullOrEmpty(omodel.F_DevName) ? storContents : fmodel.F_DevName + "," + storContents;
                            fmodel.F_DevOutState = state;
                            fmodel.F_State = workstate;
                           logbll.AddLog(omodel.F_WoID, workstate, (int)EnumWorkOrder.Dev, User.F_UserName + "(" + User.F_UserCode + ")" + opt + optcont, (int)EnumDevItemType.deal, (int)EnumDevItemOpt.storage, "", 0, User.F_UserCode);
                            orderbll.Update(fmodel);
                        }
                        #endregion
                        return Success("入库成功！");
                    }
                    else return Error("入库失败！");

                }
                return Error("入库失败,不存在此工单编号！");
            }
            else return Error("请重新登录！");
        }
        /// <summary>
        /// 库存不足异常结束
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult AbnormalEnd(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("操作失败,不存在此工单编号！");
            }
            else return Error("请重新登录！");
        }
        /// <summary>
        /// 完结工单
        /// </summary>
        /// <param name="woid"></param>
        /// <returns></returns>
        public ActionResult Overorder(int woid, string sureman, string phon, string talkie)
        {
            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_ProposerDeptId != User.F_DeptId)
                    return Error("无操作权限！");
                var n = AddLogTrans(woid, (int)omodel.F_State, user.F_UserName + "(" + user.F_UserCode + ")" + "确认完结工单", (int)EnumDevItemType.deal, (int)EnumDevItemOpt.end, "", "", phon, talkie, 0, User);
                //if (DealWO(ua, omodel, "", "", 6))
                if (n > 0)
                {
                    omodel.F_FinishTime = DateTime.Now;
                    omodel.F_State = (int)EnumDevWorkOrderState.finish;
                    omodel.F_FinishMan = sureman;
                    omodel.F_FinishManName = user.F_UserName;
                    omodel.F_FinishManPhon = phon;
                    omodel.F_FinishManTalkie = talkie;
                    orderbll.Update(omodel);
                    #region 原工单状态更新
                    if (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)EnumDevWorkOrderState.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("操作失败,不存在此工单编号！");
        }


        /// <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("无操作权限！");
        }


    }
}