using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CallCenter.Utility;
using CallCenter.Utility.log;
using CallCenterApi.Interface.Controllers.Base;

namespace CallCenterApi.Interface.Controllers.workorder
{
    public class AYWorkOrderController : BaseController
    {
        /// <summary>
        /// 状态枚举
        /// </summary>
        enum CodeStatus
        {
            SUCCESS = 200,
            安全验证未通过 = 400,
            无法预测的程序错误 = 501,
            参数格式错误 = 100,
            必填字段为空 = 101,
            字符串超出长度 = 102,
            数据库错误 = 103,
            执行超时 = 104,
            参数逻辑错误 = 105,
            ID不存在 = 106,
            无可上传文件 = 107,
            执行失败 = 108,
            用户已禁用 = 120,

            异常结果类型 = 500

        }
        BLL.T_Bus_WorkOrder_GovernmentServiceWO_Map womapbll = new BLL.T_Bus_WorkOrder_GovernmentServiceWO_Map();
        private BLL.T_Sys_DictionaryValue divBLL = new BLL.T_Sys_DictionaryValue();
        private BLL.T_Sys_Area areaBLL = new BLL.T_Sys_Area();
        private AjaxResult ajresult = new AjaxResult();
        /// <summary>
        /// 添加工单接口（投诉/咨询）
        /// 商丘市政务服务网和12345系统对接接口
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        public ActionResult AddWorkOrder()
        {

            Model.T_Bus_WorkOrder_GovernmentServiceWO_Map womapmodel = new Model.T_Bus_WorkOrder_GovernmentServiceWO_Map();
            string message = string.Empty;
            string port_password = RequestString.GetFormString("port_password");   //加密信息(安全验证用)
            string uuid = RequestString.GetFormString("uuid");          //客户唯一标识（安全验证用）
            int source = RequestString.GetInt("source", 7);             //信息来源
            string cusname = RequestString.GetFormString("cusname");    //投诉人姓名
            string cussex = RequestString.GetFormString("cussex");      //投诉人性别
            string cusphone = RequestString.GetFormString("cusphone");  //投诉人电话
            string cusaddress = RequestString.GetFormString("cusaddress");    //投诉人详细地址
            string email = RequestString.GetFormString("email");        //邮箱
            string zipcode = RequestString.GetFormString("zipcode");    //邮编
            string conname = RequestString.GetFormString("conname");    //联系人姓名
            string conphone = RequestString.GetFormString("conphone");  //客户电话
            string title = RequestString.GetFormString("title");        //标题
            string content = RequestString.GetFormString("content");    //内容
            int sourcearea = RequestString.GetInt("sourcearea", 0);      //事发地域id
            string sourceaddress = RequestString.GetFormString("sourceaddress");    //事发详细地址
            string keys = RequestString.GetFormString("keys");          //关键词id(多个用英文逗号, 隔开)
            string splituser = RequestString.GetFormString("splituser");    //分管领导id(多个用英文逗号, 隔开)
            int type = RequestString.GetInt("type", 0);              //类型
            int bigtype = RequestString.GetInt("bigtype", 0);        //大类别
            int smalltype = RequestString.GetInt("smalltype", 0);    //小类别
            int isprotect = RequestString.GetInt("isprotect", 0);    //0普通1保密
            int level = RequestString.GetInt("level", 0);//1普通2紧急3特急
            string files = RequestString.GetFormString("files");


            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            if (string.IsNullOrEmpty(cusphone))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "投诉人电话不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + cusphone, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
            if (string.IsNullOrEmpty(cusname))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "投诉人姓名不能为空";
                return Process();
            }
            if (string.IsNullOrEmpty(title))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "标题不能为空";
                return Process();
            }
            WorkOrderController wo = new WorkOrderController();
            if (!womapbll.ExistsByGSWorkOrderId(uuid))  //uuid不存在时,防止重复添加
            {
                string WorkOrderId = wo.AddWorkOrderBySource(source, cusname, cussex, cusphone, cusaddress, email, zipcode, conname, conphone, title, content, sourcearea, sourceaddress, keys, splituser, type, bigtype, smalltype, isprotect, level, files);
                if (string.IsNullOrEmpty(WorkOrderId))
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }

                womapmodel.F_WorkOrderId = WorkOrderId;
                womapmodel.F_GSWorkOrderId = uuid;
                womapmodel.F_AddTime = DateTime.Now;
                int n = womapbll.Add(womapmodel);
                if (n <= 0)
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }

                ajresult.state = (int)CodeStatus.SUCCESS;
                ajresult.message = "操作成功";
                ajresult.data = WorkOrderId;  //订单号
                return Process();
            }
            else
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }
        }
        /// <summary>
        /// 获取来源
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSource(string uuid, string port_password, string timesamp)
        {
            #region 参数验证
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            if (string.IsNullOrEmpty(timesamp))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "时间戳不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + timesamp, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }

            if (womapbll.ExistsByGSWorkOrderId(uuid))  //uuid是否已存在,防止重复添加
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }
            #endregion
            var divlist = divBLL.GetModelList(" F_PrentId=36 and F_State=0 ","  F_ValueId ");

            var divnewList = divlist.Select(x => { return new { id = x.F_ValueId, value = x.F_Value }; });

            ajresult.state = (int)CodeStatus.SUCCESS;
            ajresult.message = "工单来源获取成功";
            ajresult.data = divnewList;
            return Process();

        }
        /// <summary>
        /// 获取关键词
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetKeys(string uuid, string port_password, string timesamp)
        {
            #region 参数验证
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            if (string.IsNullOrEmpty(timesamp))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "时间戳不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + timesamp, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }

            if (womapbll.ExistsByGSWorkOrderId(uuid))  //uuid是否已存在,防止重复添加
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }
            #endregion
            var divlist = divBLL.GetModelList(" F_PrentId=38 and F_State=0 ","  F_ValueId ");

            var divnewList = divlist.Select(x => { return new { id = x.F_ValueId, value = x.F_Value }; });

            ajresult.state = (int)CodeStatus.SUCCESS;
            ajresult.message = "关键词获取成功";
            ajresult.data = divnewList;
            return Process();

        }

        /// <summary>
        /// 获取事件区域
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetAreas(string uuid, string port_password, string timesamp)
        {

            #region 参数验证
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }

            if (string.IsNullOrEmpty(timesamp))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "时间戳不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + timesamp, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
            if (womapbll.ExistsByGSWorkOrderId(uuid))  //uuid是否已存在,防止重复添加
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }
            #endregion
            var arealist = areaBLL.GetModelList(" isnull(F_PrentId,0)=0 and F_State=0 "," F_AreaId ");

            var areanewList = arealist.Select(x => { return new { id = x.F_AreaId, value = x.F_AreaName }; });

            ajresult.state = (int)CodeStatus.SUCCESS;
            ajresult.message = "地区获取成功";
            ajresult.data = areanewList;



            return Process();
        }
        /// <summary>
        /// 获取工单类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetType(string uuid, string port_password, string timesamp)
        {
            #region 参数验证
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            if (string.IsNullOrEmpty(timesamp))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "时间戳不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + timesamp, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }

            if (womapbll.ExistsByGSWorkOrderId(uuid))  //uuid是否已存在,防止重复添加
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }
            #endregion
            var divlist = divBLL.GetModelList(" F_PrentId=37 and F_State=0 ","  F_ValueId ");

            var divnewList = divlist.Select(x => { return new { id = x.F_ValueId, value = x.F_Value }; });

            ajresult.state = (int)CodeStatus.SUCCESS;
            ajresult.message = "工单类型获取成功";
            ajresult.data = divnewList;
            return Process();

        }
        /// <summary>
        /// 上传附件接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadUrl(string uuid, string port_password, string timesamp, string dataurl)
        {

            #region 参数验证
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }

            if (string.IsNullOrEmpty(timesamp))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "时间戳不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + timesamp, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
            if (womapbll.ExistsByGSWorkOrderId(uuid))  //uuid是否已存在,防止重复添加
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }
            #endregion
            var resid = ""; int mapid = 0;

            string path = "/Upload/Gridding/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";
            var url = dataurl.Split('|');
            FileUp fileup = new FileUp();
            if (url.Count() <= 0)
            {
                ajresult.state = (int)CodeStatus.无可上传文件;
                ajresult.message = "无可上传文件";
                return Process();
            }
            foreach (var item in url)
            {
                var filename = fileup.GetFileName(item);
                var fileext = fileup.GetExt(item);
                ImageUpload iu = new ImageUpload();
                iu.SavePath = path;
                var re = iu.downloadurl(item, filename);

                if (re == "")
                {
                    #region 附件保存
                    Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
                    model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
                    model_T_Sys_Accessories.F_FileName = filename;//附件名称
                    model_T_Sys_Accessories.F_FileType = fileext;//附件类型
                    model_T_Sys_Accessories.F_FileUrl = path + filename;//附件地址
                    model_T_Sys_Accessories.F_Size = 0;//iu.FileSize;
                                                       //model_T_Sys_Accessories.F_UserCode = wxuser.F_Id.ToString();//上传人  
                    var assid = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
                    model_T_Sys_Accessories.F_FileId = assid;
                    #endregion
                    if (assid > 0)
                    {
                        resid = resid + assid.ToString() + ",";
                    }
                    else
                        continue;
                }
            }
            resid = resid.Trim(',');
            #region 接收信息保存
            #endregion

            ajresult.state = (int)CodeStatus.SUCCESS;
            ajresult.message = "操作成功";
            ajresult.data = resid;  //反馈ID


            return Process();
        }
        /// <summary>
        /// 成功的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual ActionResult Process()
        {
            string methodName = new System.Diagnostics.StackTrace(true).GetFrame(1).GetMethod().Name;   //事件源
            LogInterface.WriteLog(this.GetType().ToString() + " -- " + methodName + "\r\n" + ajresult.state + "|" + ajresult.data + ajresult.message); //日志
            var jsonMsg = new AjaxResult { state = ajresult.state, message = ajresult.message, data = ajresult.data }.ToJson();
            return Content(jsonMsg);
        }
    }
}