using RMYY_CallCenter_Api.Bll;
using RMYY_CallCenter_Api.Model;
using RMYY_CallCenter_Api.Models.Enum;
using RMYY_CallCenter_Api.Models.Input;
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers
{
    public class WorepairController : BaseController
    {
        private Bll.T_Sys_UserAccount userbll = new Bll.T_Sys_UserAccount();
        /// <summary>
        /// 报修工单
        /// </summary>
        private Bll.T_WowoRepair_Basebll wrbBll = new Bll.T_WowoRepair_Basebll();
        /// <summary>
        /// 工单类别
        /// </summary>
        private T_Wowo_wocodetypebll typebll = new T_Wowo_wocodetypebll();
        /// <summary>
        /// 院区
        /// </summary>
        private T_Wowo_repairyqbll yqbll = new T_Wowo_repairyqbll();
        /// <summary>
        /// 工单类型
        /// </summary>
        private Bll.T_Sys_GongDan gongdanBll = new Bll.T_Sys_GongDan();
        /// <summary>
        /// 陪检、血站取血工单
        /// </summary>
        private Bll.T_Acc_WorkOrder accorderbll = new Bll.T_Acc_WorkOrder();
        /// <summary>
        /// 租赁工单
        /// </summary>
        private Bll.T_Dev_WorkOrder devorderbll = new Bll.T_Dev_WorkOrder();
        /// <summary>
        /// 配送工单
        /// </summary>
        private Bll.T_Dis_WorkOrder disorderbll = new Bll.T_Dis_WorkOrder();
        /// <summary>
        /// 病床转移
        /// </summary>
        private Bll.T_Hos_WorkOrder orderbll = new Bll.T_Hos_WorkOrder();
        /// <summary>
        /// 物资转移
        /// </summary>
        private Bll.T_Mat_WorkOrder matitembll = new Bll.T_Mat_WorkOrder();
        /// <summary>
        /// 车辆调度
        /// </summary>
        private Bll.T_Car_WorkOrder carorderbll = new Bll.T_Car_WorkOrder();
        /// <summary>
        /// 咨询工单
        /// </summary>
        private Bll.T_Con_WorkOrder conorderbll = new Bll.T_Con_WorkOrder();
        #region 报修工单模块
        #region 添加
        /// <summary>
        /// 添加报修工单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddRepairbase(T_WowoRepair_Baseinput input)
        {
            ////查询默认为故障报修的类型id
            //var wocotypelist = gongdanBll.GetModelList(" F_Name='故障报修'");
            //var modeltype = 0;
            //foreach (var item in wocotypelist)
            //{
            //    modeltype = item.F_GDId;
            //}

            T_WowoRepair_Base model = new T_WowoRepair_Base();
            /// <summary>
            /// 工单类别
            /// </summary>
            model.T_Wowocodetype = input.T_Wowocodetype;
            ////工单类型默认故障报修
            //model.T_Wotype = modeltype;
            ///工单编号
            model.T_workercode = DateTime.Now.ToString("yyyyMMddHHmmssfff");//工单编号
            //工单来源
            model.T_Wowocodecnnal = input.T_Wowocodecnnal;
            /// <summary>
            /// 用来判断是否有协助人
            /// </summary>
            model.T_Wofrindpid = -1;
            /// <summary>
            /// 工单状态
            /// </summary>
            model.T_Wowocodestate = 0;
            /// <summary>
            /// 报修内容
            /// </summary>
            model.T_Wowocodecenter = input.T_Wowocodecenter;
            /// <summary>
            /// 创建工单人信息
            /// </summary>
            model.T_Wocreaterepairman = input.T_Wocreaterepairman;
            /// <summary>
            /// 报修人信息
            /// </summary>
            model.T_Worepairman = input.T_Worepairman;
            /// <summary>
            /// 报修人电话
            /// </summary>
            model.T_Worepairmanphone = input.T_Worepairmanphone;
            /// <summary>
            /// 报修时间
            /// </summary>
            model.T_Worepairdate = DateTime.Now;
            /// <summary>
            ///报修院区编号
            /// </summary>
            model.T_Worepairyqid = input.T_Worepairyqid;
            /// <summary>
            /// 报修科室编号
            /// </summary>
            model.T_Worepairdeptid = input.T_Worepairdeptid;
            /// <summary>
            /// 报修地点
            /// </summary>
            model.T_Worepairaddress = input.T_Worepairaddress;
            /// <summary>
            /// 图片表id
            /// </summary>
            model.T_Woimageid = input.T_Woimageid;
            ///// <summary>
            ///// 接单人
            ///// </summary>
            //model.T_Wotousercode = input.T_Wotousercode;
            ///// <summary>
            ///// 接单（派单）时间
            ///// </summary>
            //model.T_Wotousertime = null;
            ///// <summary>
            ///// 处理人
            ///// </summary>
            //model.T_Wodealrepairman = input.T_Wodealrepairman;
            ///// <summary>
            ///// 处理时间
            ///// </summary>
            //model.dealtime = input.dealtime;
            var id = wrbBll.Addrepair(model);
            if (id > 0)
            {
                T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                itemmodel.T_Wowocodeid = id;
                itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 创建报修工单";
                var siccount = AddRepairitem(itemmodel);
                if (siccount > 0)
                {
                    return Success("添加成功！");
                }
                else
                {
                    return Success("添加操作记录失败，请重试");
                }
            }
            else
            {
                return Success("添加失败，请重试");
            }
        }
        /// <summary>
        /// 添加派单记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddRepairsend(T_Wowo_repair_send input)
        {
            T_Wowo_repair_send model = new T_Wowo_repair_send();
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {
                #region 派单赋值
                /// <summary>
                /// 工单编号
                /// </summary>
                model.T_Wowocodeid = input.T_Wowocodeid;
                /// <summary>
                /// 派单人信息
                /// </summary>
                model.T_Wocreaterepairman = input.T_Wocreaterepairman;
                /// <summary>
                /// 维修人
                /// </summary>
                model.T_Wousername = input.T_Wousername;
                /// <summary>
                /// 联系方式
                /// </summary>
                model.T_Wouserphone = input.T_Wouserphone;
                /// <summary>
                /// 维修内容
                /// </summary>
                model.T_Woreparicenter = ModuleInfo.T_Wowocodecenter;
                /// <summary>
                /// 创建时间
                /// </summary>
                model.T_Wocreatetime = DateTime.Now;

                #endregion

                ModuleInfo.T_Wowocodestate = 1;
                ModuleInfo.T_Wofrindpid = 0;
                ModuleInfo.T_Wotousercode = input.T_Wousername;//接单人
                ModuleInfo.T_Wotousertime = DateTime.Now;//派单时间
                wrbBll.Updaterepair(ModuleInfo);//修改一条记录

            }

            if (wrbBll.Addrepairsend(model) > 0)
            {
                T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 将工单转派给： " + input.T_Wousername;
                var siccount = AddRepairitem(itemmodel);
                if (siccount > 0)
                {
                    return Success("添加成功！");
                }
                else
                {
                    return Success("添加操作记录失败，请重试");
                }
            }
            else
            {
                return Success("添加失败，请重试");
            }
        }
        #region 内部协助 不需要添加新工单
        public ActionResult AddRepairsendN(T_Wowo_repair_send input)
        {
            T_Wowo_repair_send model = new T_Wowo_repair_send();
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            ModuleInfo.T_Wotousercode +=" 添加内协助人"+ input.T_Wousername;//接单人

            if (wrbBll.Updaterepair(ModuleInfo))
            {
                T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 内部协助将工单转派给： " + input.T_Wousername;
                var siccount = AddRepairitem(itemmodel);
                if (siccount > 0)
                {
                    return Success("添加成功！");
                }
                else
                {
                    return Success("添加操作记录失败，请重试");
                }
            }
            else
            {
                return Success("添加失败，请重试");
            }
        }

        #endregion
        #region 外部协作 需要添加新工单
        public ActionResult AddRepairsendW(T_Wowo_repair_send input)
        {
            
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {

                ModuleInfo.T_Wowocodestate = 0;
                ModuleInfo.T_Wofrindpid = input.T_Wowocodeid;
                ModuleInfo.T_Wotousercode = "";//接单人
                ModuleInfo.T_Wotousertime = null;//派单时间
                ModuleInfo.T_Wodealrepairman = "";// 处理人
                ModuleInfo.T_Wodealtime = null;// 处理时间
                //wrbBll.Addrepair(ModuleInfo);//添加一条记录

                if (wrbBll.Addrepair(ModuleInfo) > 0)
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 添加了外部协助 ";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("添加成功！");
                    }
                    else
                    {
                        return Success("添加操作记录失败，请重试");
                    }
                }
                else
                {
                    return Success("添加失败，请重试");
                }
            }
            else
            {
                return Success("所选工单不存在");
            }

        }

        #endregion

        /// <summary>
        /// 添加流程记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddRepairitem(T_Wowo_repair_items input)
        {
            T_Wowo_repair_items model = new T_Wowo_repair_items();
            model.T_Wowocodeid = input.T_Wowocodeid; //工单编号
            model.T_Woplaycenter = input.T_Woplaycenter;//操作内容
            model.T_Wocreatetime = DateTime.Now;//创建时间
            return wrbBll.Addrepairitems(model);

        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddRepairimag(T_Wowo_repair_img input)
        {
            string path = HttpContext.Request.MapPath("~/");//获取你的根目录
            var files = HttpContext.Request.Files["file"];
            //存到数据库的路径
            var sqlserurl = "img/" + files.FileName;
            //先判断文件夹是否存在
            var fullpath = path + "img";
            if (!Directory.Exists(fullpath))
            {
                Directory.CreateDirectory(fullpath);
            }
            //然后再保存
            files.SaveAs(path + sqlserurl);

            T_Wowo_repair_img model = new T_Wowo_repair_img();
            model.T_Wofilename = files.FileName;
            model.T_Wofileurl = sqlserurl;

            var reaust = wrbBll.Addrepairimg(model);
            if (reaust > 0)
            {

                return Success("添加成功！", reaust);
            }
            else
            {
                return Success("添加失败，请重试");
            }

        }
        #endregion
        #region 处理工单
        public ActionResult Dealwocode(T_WowoRepair_Baseinput input)
        {
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {
                ModuleInfo.T_Wowocodestate = 2;
                ///// <summary>
                ///// 处理人
                ///// </summary>
                ModuleInfo.T_Wodealrepairman = input.T_Wodealrepairman; 
                ///// <summary>
                ///// 处理备注
                ///// </summary>
                ModuleInfo.T_Wodealdesmo = input.T_Wodealdesmo;
                ///// <summary>
                ///// 处理时间
                ///// </summary>
                ModuleInfo.T_Wodealtime = DateTime.Now;
                if (wrbBll.Updaterepair(ModuleInfo))
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 处理了工单";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("处理成功");
                    }
                    else
                    {
                        return Success("创建流程记录失败,请重试！");
                    }
                }
                else
                {
                    return Success("处理失败,请重试！");
                }
            }
            else
            {
                return Success("对象获取失败");
            }
        }

        #endregion
        #region 退回工单
        public ActionResult Thwocode(T_WowoRepair_Baseinput input)
        {
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {
                ModuleInfo.T_Wowocodestate = 0;
                ModuleInfo.T_Wotousercode = "";//接单人
                ModuleInfo.T_Wotousertime = null;//派单时间
                ModuleInfo.T_Wothbecause = input.T_Wothbecause;
                if (wrbBll.Updaterepair(ModuleInfo))
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 退回了工单";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("退回成功");
                    }
                    else
                    {
                        return Success("创建流程记录失败,请重试！");
                    }
                }
                else
                {
                    return Success("退回失败,请重试！");
                }
            }
            else
            {
                return Success("对象获取失败");
            }
        }

        #endregion
        #region 异常结束
        public ActionResult Ycjswocode(T_WowoRepair_Baseinput input)
        {
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {
                ModuleInfo.T_Wowocodestate = 4;
                ///// <summary>
                ///// 处理人
                ///// </summary>
                ModuleInfo.T_Wodealrepairman = input.T_Wodealrepairman;
                ///// <summary>
                ///// 处理时间
                ///// </summary>
                ModuleInfo.T_Wodealtime = DateTime.Now;
                if (wrbBll.Updaterepair(ModuleInfo))
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 异常结束了工单";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("异常结束成功");
                    }
                    else
                    {
                        return Success("创建流程记录失败,请重试！");
                    }
                }
                else
                {
                    return Success("异常结束失败,请重试！");
                }
            }
            else
            {
                return Success("对象获取失败");
            }
        }

        #endregion
        #region 返修工单
        public ActionResult FxRepairbase(T_WowoRepair_Baseinput input)
        {
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {
                #region 添加
                //T_WowoRepair_Base model = new T_WowoRepair_Base();
                ///// <summary>
                ///// 工单类型
                ///// </summary>
                //model.T_Wowocodetype = ModuleInfo.T_Wowocodetype;
                ///// <summary>
                ///// 工单状态
                ///// </summary>
                //model.T_Wowocodestate = 1;
                ///// <summary>
                ///// 报修内容
                ///// </summary>
                //model.T_Wowocodecenter = ModuleInfo.T_Wowocodecenter;
                ///// <summary>
                ///// 创建工单人信息
                ///// </summary>
                //model.T_Wocreaterepairman = ModuleInfo.T_Wocreaterepairman;
                ///// <summary>
                ///// 报修人信息
                ///// </summary>
                //model.T_Worepairman = ModuleInfo.T_Worepairman;
                ///// <summary>
                ///// 报修人电话
                ///// </summary>
                //model.T_Worepairmanphone = ModuleInfo.T_Worepairmanphone;
                ///// <summary>
                ///// 报修时间
                ///// </summary>
                //model.T_Worepairdate = DateTime.Now;
                ///// <summary>
                /////报修院区编号
                ///// </summary>
                //model.T_Worepairyqid = ModuleInfo.T_Worepairyqid;
                ///// <summary>
                ///// 报修科室编号
                ///// </summary>
                //model.T_Worepairdeptid = ModuleInfo.T_Worepairdeptid;
                ///// <summary>
                ///// 报修地点
                ///// </summary>
                //model.T_Worepairaddress = ModuleInfo.T_Worepairaddress;
                ///// <summary>
                ///// 图片表id
                ///// </summary>
                //model.T_Woimageid = ModuleInfo.T_Woimageid;
                ///// <summary>
                ///// 接单人
                ///// </summary>
                //model.T_Wotousercode = ModuleInfo.T_Wotousercode;
                ///// <summary>
                ///// 接单（派单）时间
                ///// </summary>
                //model.T_Wotousertime = DateTime.Now;
                /////// <summary>
                /////// 处理人
                /////// </summary>
                ////model.T_Wodealrepairman = input.T_Wodealrepairman;
                /////// <summary>
                /////// 处理时间
                /////// </summary>
                ////model.dealtime = input.dealtime;
                //var id = wrbBll.Addrepair(model);
                //if (id > 0)
                //{
                //    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                //    itemmodel.T_Wowocodeid = id;
                //    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 创建返修工单";
                //    var siccount = AddRepairitem(itemmodel);
                //    if (siccount > 0)
                //    {
                //        return Success("返修成功！");
                //    }
                //    else
                //    {
                //        return Success("返修操作记录失败，请重试");
                //    }
                //}
                //else
                //{
                //    return Success("返修失败，请重试");
                //}

                #endregion
                #region 修改
                /// <summary>
                /// 工单状态
                /// </summary>
                ModuleInfo.T_Wowocodestate = 1;
                /// <summary>
                /// 报修时间
                /// </summary>
                ModuleInfo.T_Worepairdate = DateTime.Now;
                /// <summary>
                /// 接单（派单）时间
                /// </summary>
                ModuleInfo.T_Wotousertime = DateTime.Now;
                ///// <summary>
                ///// 处理人
                ///// </summary>
                ModuleInfo.T_Wodealrepairman = "";
                if (wrbBll.Updaterepair(ModuleInfo))
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = ModuleInfo.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 返修工单";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("返修成功！");
                    }
                    else
                    {
                        return Success("返修操作记录失败，请重试");
                    }
                }
                else
                {
                    return Success("返修失败，请重试");
                }

                #endregion
            }
            else
            {
                return Success("该工单不存在");
            }
        }
        #endregion
        #region 评价
        public ActionResult RepairbaseEavu(T_Wowo_repair_evaluate intput)
        {
            T_Wowo_repair_evaluate model = new T_Wowo_repair_evaluate();
            model.T_Wowocodeid = intput.T_Wowocodeid;//工单编号
            model.T_Woscore = intput.T_Woscore;//评分
            model.T_Woevaluate = intput.T_Woevaluate;//评价内容
            model.T_Wolabel = intput.T_Wolabel;//评价标签
            model.T_Wocreateuser = intput.T_Wocreateuser;//评价人
            model.T_Wocreatetime = DateTime.Now;
            if (wrbBll.Addrepairevaluate(model) > 0)
            {
                var ModuleInfo = wrbBll.GetModel(intput.T_Wowocodeid);
                ModuleInfo.T_Wowocodestate = 3;//已完结
                if (wrbBll.Updaterepair(ModuleInfo))
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = intput.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = intput.T_Wocreateuser + " 评价了工单";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("评价成功");
                    }
                    else
                    {
                        return Success("创建流程记录失败,请重试！");
                    }
                }
                else
                {
                    return Success("评价失败,请重试！");
                }
            }
            return Success("成功");
        }

        #endregion
        #region 修改
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult EditGongDan(T_WowoRepair_Baseinput input)
        {
            var ModuleInfo = wrbBll.GetModel(input.T_Wowocodeid);
            if (ModuleInfo != null)
            {
                ModuleInfo.T_Wowocodetype = input.T_Wowocodetype;
                /// <summary>
                /// 工单状态
                /// </summary>
                ModuleInfo.T_Wowocodestate = input.T_Wowocodestate;
                /// <summary>
                /// 报修内容
                /// </summary>
                ModuleInfo.T_Wowocodecenter = input.T_Wowocodecenter;
                /// <summary>
                /// 创建工单人信息
                /// </summary>
                ModuleInfo.T_Wocreaterepairman = input.T_Wocreaterepairman;
                /// <summary>
                /// 报修人信息
                /// </summary>
                ModuleInfo.T_Worepairman = input.T_Worepairman;
                /// <summary>
                /// 报修人电话
                /// </summary>
                ModuleInfo.T_Worepairmanphone = input.T_Worepairmanphone;
                /// <summary>
                /// 报修时间
                /// </summary>
                if (input.T_Worepairdate != "")
                {
                    ModuleInfo.T_Worepairdate = Convert.ToDateTime(input.T_Worepairdate);
                }
                /// <summary>
                ///报修院区编号
                /// </summary>
                ModuleInfo.T_Worepairyqid = input.T_Worepairyqid;
                /// <summary>
                /// 报修科室编号
                /// </summary>
                ModuleInfo.T_Worepairdeptid = input.T_Worepairdeptid;
                /// <summary>
                /// 报修地点
                /// </summary>
                ModuleInfo.T_Worepairaddress = input.T_Worepairaddress;
                /// <summary>
                /// 图片表id
                /// </summary>
                ModuleInfo.T_Woimageid = input.T_Woimageid;
                /// <summary>
                /// 接单人
                /// </summary>
                ModuleInfo.T_Wotousercode = input.T_Wotousercode;
                /// <summary>
                /// 接单（派单）时间
                /// </summary>
                if (input.T_Wotousertime != "")
                {
                    ModuleInfo.T_Wotousertime = Convert.ToDateTime(input.T_Wotousertime);
                }
                /// <summary>
                /// 处理人
                /// </summary>
                ModuleInfo.T_Wodealrepairman = input.T_Wodealrepairman;
                /// <summary>
                /// 处理时间
                /// </summary>
                if (input.T_Wodealtime != "")
                {
                    ModuleInfo.T_Wodealtime = Convert.ToDateTime(input.T_Wodealtime);
                }

                if (wrbBll.Updaterepair(ModuleInfo))
                {
                    T_Wowo_repair_items itemmodel = new T_Wowo_repair_items();
                    itemmodel.T_Wowocodeid = input.T_Wowocodeid;
                    itemmodel.T_Woplaycenter = input.T_Wocreaterepairman + " 修改了工单";
                    var siccount = AddRepairitem(itemmodel);
                    if (siccount > 0)
                    {
                        return Success("修改成功");
                    }
                    else
                    {
                        return Success("创建流程记录失败,请重试！");
                    }
                }
                else
                {
                    return Success("修改失败,请重试！");
                }
            }
            else
            {
                return Success("对象获取失败");
            }
        }
        #endregion
        #region 删除
        public ActionResult Deleterepair(int T_Wowocodeid)
        {
            if (wrbBll.Deleterepair(T_Wowocodeid))
            {
                return Success("删除成功");
            }
            else
            {
                return Success("删除失败,请重试！");
            }
        }

        #endregion
        #region 查询
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="mId"></param>
        /// <returns></returns>
        public ActionResult GetModule(int T_Wowocodeid)
        {
            var T_WowoRepair_Base_ModuleInfo = wrbBll.GetModel(T_Wowocodeid);
            var imgid = "";
            if (T_WowoRepair_Base_ModuleInfo != null)
            {
                imgid = T_WowoRepair_Base_ModuleInfo.T_Woimageid;
            }
            List<T_Wowo_repair_img> imgmodel = new List<T_Wowo_repair_img>();
            if (!string.IsNullOrEmpty(imgid))
            {
                  imgmodel = wrbBll.Getrepairimg(imgid);
            }
           
            var obj = new
            {
                models = T_WowoRepair_Base_ModuleInfo,
                imgmodel = imgmodel
            };
            if (T_WowoRepair_Base_ModuleInfo != null)
            {
                return Success("加载成功", obj);
            }
            else
            {
                return Success("加载失败");
            }
        }
        /// <summary>
        /// 分页获取数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetListRepairlist(string T_Worepairman,string T_Worepairmanphone,int state,string stime,string etime,string createusercode,string tousercode ,int T_Wowocodetype=0,int T_Worepairdeptid=0,   int pageindex=1, int pagesise=10)
        {
            
            DataTable date = new DataTable();
            string where = "";
            int recordCount = 0;
            if (!string.IsNullOrEmpty(T_Worepairman))
            {
                where += " and T_Worepairman like '%" + T_Worepairman + "%'";
            }
            if (!string.IsNullOrEmpty(T_Worepairmanphone))
            {
                where += " and T_Worepairmanphone ='" + T_Worepairmanphone+"'";
            }
            if (T_Wowocodetype!=0)
            {
                where += " and T_Wowocodetype =" + T_Wowocodetype;
            }
            if (T_Worepairdeptid != 0)
            {
                where += " and T_Worepairdeptid =" + T_Worepairdeptid;
            }
            if (state>=0)
            {
                if (state == 6)
                {
                    where += " and T_Wofrindpid !=0 and T_Wofrindpid !=-1";
                }
                else
                {
                    where += " and T_Wowocodestate =" + state;
                }
                
            }
            if (!string.IsNullOrWhiteSpace(createusercode))
            {
                where += " and (T_Worepairman='" + createusercode + "' or T_Wocreaterepairman ='" + createusercode + "' )";
            }
            if (!string.IsNullOrWhiteSpace(tousercode))
            {
                where += " and T_Wotousercode like '%" + tousercode + "%'";
            }
            if (!string.IsNullOrEmpty(stime)&& !string.IsNullOrEmpty(etime))
            {
                stime += " 00:00:00";
                etime += " 23:59:59";
                where += " and T_Worepairdate >='" +Convert.ToDateTime(stime)+ "' and T_Worepairdate<= '"+Convert.ToDateTime(etime)+"'";
            }
           // var list = wrbBll.GetListByPage(where, "", pageindex, pagesise);
            //return Success("成功",list);
            string tablename = "";
            tablename += "(select a.*,b.T_Woname,c.F_DeptName,d.F_UserName createrepairmanusername,e.F_UserName repairmanusername,g.F_UserName dealrepairmanusername,h.F_Name T_Wowocodetypename,i.T_Woscore T_Woscore from T_WowoRepair_Base a ";
            tablename += " left join T_Wowo_repairyq b on a.T_Worepairyqid=b.T_Woid";
            tablename += " left join T_Sys_Department c on a.T_Worepairdeptid=c.F_DeptId";
            tablename += " left join T_Sys_UserAccount d on a.T_Wocreaterepairman=d.F_UserCode";
            tablename += " left join T_Sys_UserAccount e on a.T_Worepairman=e.F_UserCode";
            //tablename += " left join T_Sys_UserAccount f on a.T_Wotousercode=f.F_UserCode";
            tablename += " left join T_Sys_UserAccount g on a.T_Wodealrepairman=g.F_UserCode";
            tablename += " left join T_Sys_GongDan h on a.T_Wowocodetype=h.F_GDId";
            tablename += " left join T_Wowo_repair_evaluate i on a.T_Wowocodeid=i.T_Wowocodeid) T_WowoRepair_Base ";
            date = Bll.PagerBll.GetListPager(tablename,"T_Wowocodeid","*",where,"order by T_Wowocodeid desc",pagesise,pageindex,true,out recordCount);
            List<T_WowoRepair_Base> carlist =  wrbBll.DataTableToList(date);
           
            List<T_WowoRepair_Base> wolist = new List<T_WowoRepair_Base>();
            if (carlist.Count > 0)
            {
                foreach (var item in carlist)
                {
                    T_WowoRepair_Base modelinfo = new T_WowoRepair_Base();
                    modelinfo = item;
                    var dealtime = item.T_Wodealtime;
                    var tousertime = item.T_Wotousertime;
                    if (dealtime != null && tousertime != null)
                    {
                        TimeSpan ts = Convert.ToDateTime(dealtime).Subtract(Convert.ToDateTime(tousertime));
                        double hours = ts.TotalHours;//24.0
                        modelinfo.timethlong = hours.ToString();
                    }
                    wolist.Add(modelinfo);
                }

                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = wolist,
                    total = recordCount
                };
                return Content(obj.ToJson());
            }
            else
                return Success("暂无记录");
        }
        /// <summary>
        /// 获取报表所需信息
        /// </summary>
        /// <param name="T_Worepairman"></param>
        /// <param name="T_Worepairmanphone"></param>
        /// <param name="state"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="T_Wowocodetype"></param>
        /// <param name="T_Worepairdeptid"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesise"></param>
        /// <returns></returns>
        public ActionResult GetListRepairlistall(string T_Worepairman, string T_Worepairmanphone, int state, string stime, string etime, int T_Wowocodetype = 0, int T_Worepairdeptid = 0)
        {
            DataTable date = new DataTable();
            string where = "";
            if (!string.IsNullOrEmpty(T_Worepairman))
            {
                where += " and T_Worepairman like '%" + T_Worepairman + "%'";
            }
            if (!string.IsNullOrEmpty(T_Worepairmanphone))
            {
                where += " and T_Worepairmanphone ='" + T_Worepairmanphone + "'";
            }
            if (T_Wowocodetype != 0)
            {
                where += " and T_Wowocodetype =" + T_Wowocodetype;
            }
            if (T_Worepairdeptid != 0)
            {
                where += " and T_Worepairdeptid =" + T_Worepairdeptid;
            }
            if (state >= 0)
            {
                where += " and T_Wowocodestate =" + state;
            }

            if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
            {
                stime += " 00:00:00";
                etime += " 23:59:59";
                where += " and T_Worepairdate >='" + Convert.ToDateTime(stime) + "' and T_Worepairdate<= '" + Convert.ToDateTime(etime) + "'";
            }
             var list = wrbBll.GetListRepairlistall(where);
            if (list.Count > 0)
            {
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = list
                };
                return Content(obj.ToJson());
            }
            else
                return Success("暂无记录");
        }
       /// <summary>
        /// 获取工单流程
        /// </summary>
        /// <returns></returns>
        public ActionResult Getworderitems(int T_Wowocodeid)
        {
            var list = wrbBll.Getworderitems(T_Wowocodeid);
            return Success("成功",list);
        }
        /// <summary>
        /// 获取派单记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Getwordersend(int T_Wowocodeid)
        {
            var list = wrbBll.Getwordersend(T_Wowocodeid);
            return Success("成功",list);
        }
        /// <summary>
        /// 获取评价信息
        /// </summary>
        /// <param name="T_Wowocodeid"></param>
        /// <returns></returns>
        public ActionResult Getworderevaluate(int T_Wowocodeid)
        {
            var list = wrbBll.Getworderevaluate(T_Wowocodeid);
            return Success("成功",list);
        }
        #endregion
        #region 综合维运工作质量评价表
        public ActionResult GetZhwyworktable(string stime ,string etime ,string key, int deptid = 0)
        {
            if (!string.IsNullOrEmpty(stime))
            {
                stime += " 00:00:00";
            }
            if (!string.IsNullOrEmpty(etime))
            {
                etime += " 23:59:59";
            }
            var listtable = wrbBll.GetZhwyworktable(stime,etime,deptid,key);
            return Success("成功", listtable);
        }

        #endregion
        /// <summary>
        /// 获取首页信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOneIndex(string stime,string etime,int isweek=0)
        {
            
            #region 查找所有待处理工单数量
            var dclacccount = wrbBll.getcount("T_Acc_WorkOrder", " F_State=" + (int)EnumAccWorkOrderState.receive,"","");//T_Acc_WorkOrder 陪检、血站取血工单 2
            var dcldevcount = wrbBll.getcount("T_Dev_WorkOrder", " F_State= " + (int)EnumDevWorkOrderState.work, "", "");//T_Dev_WorkOrder 租赁工单 3
            var dcldiscount = wrbBll.getcount("T_Dis_WorkOrder", " F_State= " + (int)EnumDisWorkOrderState.receive, "", "");//T_Dis_WorkOrder 配送工单 2
            var dclhoscount = wrbBll.getcount("T_Hos_WorkOrder", " F_State= " + (int)EnumHosWorkOrderState.receive, "", "");//T_Hos_WorkOrder 病床转移 4
            var dclmatcount = wrbBll.getcount("T_Mat_WorkOrder", " F_State= " + (int)EnumMatWorkOrderState.receive, "", "");//T_Mat_WorkOrder 物资转移 2
            var dclcarcount = wrbBll.getcount("T_Car_WorkOrder", " F_State=2 ", "", ""); //T_Car_WorkOrder 车辆调度工单
            var dclwowcount = wrbBll.getcount("T_WowoRepair_Base", " T_Wowocodestate=1", "", "");//T_WowoRepair_Base 报修工单
            var dclallcount = dclacccount + dcldevcount + dcldiscount + dclhoscount + dclmatcount + dclcarcount + dclwowcount;//所有待处理工单数量 
            #endregion
            #region 代派工工单数量
            var dpgacccount = wrbBll.getcount("T_Acc_WorkOrder", " F_State=0 ","","");//T_Acc_WorkOrder 陪检、血站取血工单
            var dpgdevcount = wrbBll.getcount("T_Dev_WorkOrder", " F_State=0 ","","");//T_Dev_WorkOrder 租赁工单
            var dpgdiscount = wrbBll.getcount("T_Dis_WorkOrder", " F_State=0 ","","");//T_Dis_WorkOrder 配送工单
            var dpghoscount = wrbBll.getcount("T_Hos_WorkOrder", " F_State=0 ","","");//T_Hos_WorkOrder 病床转移
            var dpgmatcount = wrbBll.getcount("T_Mat_WorkOrder", " F_State=0 ","","");//T_Mat_WorkOrder 物资转移
            var dpgcarcount = wrbBll.getcount("T_Car_WorkOrder", " F_State=0 ","",""); //T_Car_WorkOrder 车辆调度工单
            var dpgwowcount = wrbBll.getcount("T_WowoRepair_Base", " T_Wowocodestate=0","","");//T_WowoRepair_Base 报修工单
            var dpgallcount = dpgacccount + dpgdevcount + dpgdiscount + dpghoscount + dpgmatcount + dpgcarcount + dpgwowcount;//所有待派工工单数量 
            #endregion
            #region 待完工工单数量
            var dwgacccount = wrbBll.getcount("T_Acc_WorkOrder", " F_State=6 " ,"","");//T_Acc_WorkOrder 陪检、血站取血工单
            var dwgdevcount = wrbBll.getcount("T_Dev_WorkOrder", " F_State=8 " ,"","");//T_Dev_WorkOrder 租赁工单
            var dwgdiscount = wrbBll.getcount("T_Dis_WorkOrder", " F_State=10 ","","");//T_Dis_WorkOrder 配送工单
            var dwghoscount = wrbBll.getcount("T_Hos_WorkOrder", " F_State=10 ","","");//T_Hos_WorkOrder 病床转移
            var dwgmatcount = wrbBll.getcount("T_Mat_WorkOrder", " F_State=6 " ,"","");//T_Mat_WorkOrder 物资转移
            var dwgcarcount = wrbBll.getcount("T_Car_WorkOrder", " F_State=10 ","",""); //T_Car_WorkOrder 车辆调度工单
            var dwgwowcount = wrbBll.getcount("T_WowoRepair_Base", " T_Wowocodestate=2","","");//T_WowoRepair_Base 报修工单
            var dwgallcount = dwgacccount + dwgdevcount + dwgdiscount + dwghoscount + dwgmatcount + dwgcarcount + dwgwowcount;//所有待完工工单数量 
            #endregion
            #region 待评价工单数量
            var dpjacccount = wrbBll.getcount("T_Acc_WorkOrder", " F_State=" + (int)EnumAccWorkOrderState.end,"","");//T_Acc_WorkOrder 陪检、血站取血工单 6  
            var dpjdevcount = wrbBll.getcount("T_Dev_WorkOrder", " F_State="+(int)EnumDevWorkOrderState.storage, "","");//T_Dev_WorkOrder 租赁工单 8  
            var dpjdiscount = wrbBll.getcount("T_Dis_WorkOrder", " F_State=10 ","","");//T_Dis_WorkOrder 配送工单
            var dpjhoscount = wrbBll.getcount("T_Hos_WorkOrder", " F_State=10 ","","");//T_Hos_WorkOrder 病床转移
            var dpjmatcount = wrbBll.getcount("T_Mat_WorkOrder", " F_State=6 " ,"","");//T_Mat_WorkOrder 物资转移
            var dpjcarcount = wrbBll.getcount("T_Car_WorkOrder", " F_State=10 ","",""); //T_Car_WorkOrder 车辆调度工单
            var dpjwowcount = wrbBll.getcount("T_WowoRepair_Base", " T_Wowocodestate=2","","");//T_WowoRepair_Base 报修工单
            var dpjallcount = dpjacccount + dpjdevcount + dpjdiscount + dpjhoscount + dpjmatcount + dpjcarcount + dpjwowcount;//所有待评价工单数量 
            #endregion
            ///完成工单统计 日 周 月 起止日期
            #region 定义起止时间
            if (string.IsNullOrEmpty(stime) || string.IsNullOrEmpty(stime))
            {
                stime = stime + " 00:00:00";
                etime =etime + " 23:59:59";
            }
            else
            {
                if (isweek == 0)//默认天
                {
                    stime = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
                    etime = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
                }
                else if (isweek == 1)//周
                {
                    stime = DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd") + " 00:00:00";  //本周周一
                    etime = DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).AddDays(6).ToString("yyyy-MM-dd") + " 23:59:59";  //本周周日
                }
                else//月
                {
                    stime = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd") + " 00:00:00";  //本月月初
                    etime = DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddDays(-1).AddDays(6).ToString("yyyy-MM-dd") + " 23:59:59";  //本月月末
                }
            }
           
            #endregion
            #region T_Con_WorkOrder  工单类型：1健康咨询，2投诉建议，3外聘专家咨询，4家庭预约上门,5静脉置管维护
            ///健康咨询完成工单数量 1
            var jkzxfinishcount = wrbBll.getcount("T_Con_WorkOrder", " F_type1=1 and F_State=10  ", stime, etime);
            ///外聘专家咨询 3
            var wpzjzxcount = wrbBll.getcount("T_Con_WorkOrder", " F_type1=3  and F_State=10 ", stime, etime);
            ///家庭预约上门 4 
            var jtyysmcount = wrbBll.getcount("T_Con_WorkOrder", " F_type1=4  and F_State=10 ", stime, etime);
            ///静脉置管维护 5
            var jmzgwhcount = wrbBll.getcount("T_Con_WorkOrder", " F_type1=5 and F_State=10 ", stime, etime);
            #endregion
            #region 综合调度  5种相加
            /////T_Acc_WorkOrder 陪检、血站取血工单
            var zhacccount = wrbBll.getcount("T_Acc_WorkOrder", "  F_State=10 ", stime, etime);
            /////T_Dev_WorkOrder 租赁工单 
            var zhdevcount = wrbBll.getcount("T_Dev_WorkOrder", "  F_State=10 ", stime, etime);
            /////T_Dis_WorkOrder 配送工单 
            var zhdiscount = wrbBll.getcount("T_Dis_WorkOrder", "  F_State=10 ", stime, etime);
            /////T_Hos_WorkOrder 病床转移 
            var zhhoscount = wrbBll.getcount("T_Hos_WorkOrder", "  F_State=12 ", stime, etime);
            /////T_Mat_WorkOrder 物资转移 
            var zhmatcount = wrbBll.getcount("T_Mat_WorkOrder", "  F_State=12 ", stime, etime);
            //综合调度数量
            var zhallcount = zhacccount + zhdevcount + zhdiscount + zhhoscount + zhmatcount;
            #endregion
            ///故障报修
            var wobxcount = wrbBll.getcount("T_WowoRepair_Base", " T_Wowocodestate=2", stime, etime);
            ///设备租赁
            var sbzldevcount = wrbBll.getcount("T_Dev_WorkOrder", "  F_State=10 ", stime, etime);
            ///车辆调度
            var clddcarcount = wrbBll.getcount("T_Car_WorkOrder", " F_State=10 ", stime, etime);
            var obj = new
            {
                dclallcount = dclallcount,//待处理工单数
                dpgallcount = dpgallcount,//代派工工单数
                dwgallcount= dwgallcount,//待完工工单数
                dpjallcount= dpjallcount,//待评价工单数
                jkzxfinishcount= jkzxfinishcount,//健康咨询工单数
                wpzjzxcount= wpzjzxcount,//外聘专家咨询工单数
                jtyysmcount=jtyysmcount,//家庭预约上门工单数
                jmzgwhcount= jmzgwhcount,//静脉置管维护
                zhallcount= zhallcount,//综合调度数量
                wobxcount= wobxcount,//故障报修工单数量
                sbzldevcount= sbzldevcount,//设备租赁工单数量
                clddcarcount= clddcarcount//车辆调度工单数量


                  
            };

            return Success("成功",obj);
        }
        #endregion
        #region 工单类型
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult Addwotype(T_Wowo_wocodetype input)
        {
            T_Wowo_wocodetype model = new T_Wowo_wocodetype();
            model.T_Woname = input.T_Woname;
            if (typebll.Addrepairitems(model) > 0)
            {
                return Success("添加成功！");
            }
            else
            {
                return Success("添加失败，请重试");
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult edittype(T_Wowo_wocodetype input)
        {
            var ModuleInfo = typebll.GetModel(input.T_Woid);
            if (ModuleInfo != null)
            {
                ModuleInfo.T_Woname = input.T_Woname;
              
                if (typebll.Updaterepair(ModuleInfo))
                {
                   
                        return Success("修改成功");
                   
                }
                else
                {
                    return Success("修改失败,请重试！");
                }
            }
            else
            {
                return Success("对象获取失败");
            }
        }
        #region 删除
        public ActionResult Deletertype(int id)
        {
            if (typebll.Deleterepair(id))
            {
                return Success("删除成功");
            }
            else
            {
                return Success("删除失败,请重试！");
            }
        }

        #endregion
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="mId"></param>
        /// <returns></returns>
        public ActionResult GettypeModule(int id)
        {
            var T_Wowo_wocodetypeModuleInfo = typebll.GetModel(id);
            if (T_Wowo_wocodetypeModuleInfo != null)
            {
                return Success("加载成功", T_Wowo_wocodetypeModuleInfo);
            }
            else
            {
                return Success("加载失败");
            }
        }
        /// <summary>
        /// 获取数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetListtypelistall()
        {
            var list = typebll.GetListall();
            return Success("成功");
        }
        /// <summary>
        /// 分页获取数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetListtypeRepairlist(int pageindex, int pagesise)
        {
            var list = typebll.GetListByPage("", "", 1, 10);
            return Success("成功");
        }
        #endregion
        #region 院区
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult Addwotyq(T_Wowo_repairyq input)
        {
            T_Wowo_repairyq model = new T_Wowo_repairyq();
            model.T_Woname = input.T_Woname;
            if (yqbll.Addrepairitems(model) > 0)
            {
                return Success("添加成功！");
            }
            else
            {
                return Success("添加失败，请重试");
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult edittyq(T_Wowo_repairyq input)
        {
            var ModuleInfo = yqbll.GetModel(input.T_Woid);
            if (ModuleInfo != null)
            {
                ModuleInfo.T_Woname = input.T_Woname;

                if (yqbll.Updaterepair(ModuleInfo))
                {

                    return Success("修改成功");

                }
                else
                {
                    return Success("修改失败,请重试！");
                }
            }
            else
            {
                return Success("对象获取失败");
            }
        }
        #region 删除
        public ActionResult Deleteryq(int id)
        {
            if (yqbll.Deleterepair(id))
            {
                return Success("删除成功");
            }
            else
            {
                return Success("删除失败,请重试！");
            }
        }

        #endregion
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="mId"></param>
        /// <returns></returns>
        public ActionResult GetyqModule(int id)
        {
            var T_Wowo_wocodetypeModuleInfo = yqbll.GetModel(id);
            if (T_Wowo_wocodetypeModuleInfo != null)
            {
                return Success("加载成功", T_Wowo_wocodetypeModuleInfo);
            }
            else
            {
                return Success("加载失败");
            }
        }
        /// <summary>
        /// 获取数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetListyqlistall()
        {
            var list = yqbll.GetListall();
           
            return Success("成功",list);
        }
        /// <summary>
        /// 分页获取数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetListyqRepairlist(int pageindex, int pagesise)
        {
            DataTable date = new DataTable();
            string where = "";
            int recordCount = 0;
            //var list = yqbll.GetListByPage("", "", pageindex, pagesise);
            //return Success("成功",list);
            date = Bll.PagerBll.GetListPager
                               ("T_Wowo_repairyq",
                                 "T_Woid",
                                 "*",
                                where,
                                 "order by T_Woid desc",
                                 pagesise,
                                 pageindex,
                                 true,
                                 out recordCount

                               );
            List<T_Wowo_repairyq> carlist = yqbll.DataTableToList(date);
            if (carlist.Count > 0)
            {
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = carlist,
                    total = recordCount
                };
                return Content(obj.ToJson());
            }
            else
                return Success("暂无记录");
        }

        #endregion
    }
}