
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.App_Start;
using CallCenterApi.Interface.Controllers.Base;

namespace CallCenterApi.Interface.Controllers.County
{
    [OutActionFilter]
    public class CountyBusinessController : BaseController
    {
        // GET: CountyBusiness

        /// <summary>
        /// 添加工单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AddWorkOrder()
        {

            int source = RequestString.GetInt("source", 8);

            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");
            string files = RequestString.GetFormString("files");
            int sourcearea = RequestString.GetInt("sourcearea", 0);
            string sourceaddress = RequestString.GetFormString("sourceaddress");

            //int isresult = RequestString.GetInt("isresult", 0);
            //string result = RequestString.GetFormString("result");

            //string keys = RequestString.GetFormString("keys");
            //string splituser = RequestString.GetFormString("splituser");

            //int type = RequestString.GetInt("type", 0);
            //int bigtype = RequestString.GetInt("bigtype", 0);
            //int smalltype = RequestString.GetInt("smalltype", 0);

            int isprotect = RequestString.GetInt("isprotect", 0);
            int level = RequestString.GetInt("level", 0);
            //int issubmit = RequestString.GetInt("issubmit", 0);

            //string callid = RequestString.GetFormString("callid");

            string workorderid = string.Empty;

            using (TransactionScope trans = new TransactionScope())
            {
                #region 保存工单信息
                Model.T_Bus_WorkOrder modelT_Bus_WorkOrder = new Model.T_Bus_WorkOrder();
                modelT_Bus_WorkOrder.F_WorkOrderId = new BLL.T_Bus_WorkOrder().GetNewWorkOrderID(source.ToString().PadLeft(4, '0'));

                modelT_Bus_WorkOrder.F_InfoSource = source;//信息来源

                //modelT_Bus_WorkOrder.F_InfoType = type;//信息类别
                //modelT_Bus_WorkOrder.F_InfoConBigType = bigtype;//内容大类
                //modelT_Bus_WorkOrder.F_InfoConSmallType = smalltype;//内容小类
                modelT_Bus_WorkOrder.F_SourceArea = sourcearea;//反映地域
                modelT_Bus_WorkOrder.F_SourceAddress = sourceaddress;//事发地址

                modelT_Bus_WorkOrder.F_CusName = cusname;//投诉人姓名
                modelT_Bus_WorkOrder.F_CusSex = cussex;//性别
                modelT_Bus_WorkOrder.F_CusPhone = cusphone;//来电号码
                modelT_Bus_WorkOrder.F_CusAddress = cusaddress;//地址
                modelT_Bus_WorkOrder.F_ZipCode = zipcode;//邮编
                modelT_Bus_WorkOrder.F_ConName = conname;//联系人姓名
                modelT_Bus_WorkOrder.F_ConPhone = conphone;//联系人电话
                modelT_Bus_WorkOrder.F_Email = email;//E-mail

                modelT_Bus_WorkOrder.F_ComTitle = title;//标题
                modelT_Bus_WorkOrder.F_ComContent = content;//情况摘要（投诉内容）
                modelT_Bus_WorkOrder.F_File = files;//附件
                modelT_Bus_WorkOrder.F_IsProtect = isprotect;//保密方式0否1是

                //modelT_Bus_WorkOrder.F_Key = keys;
                //modelT_Bus_WorkOrder.F_SplitUser = splituser;
                modelT_Bus_WorkOrder.F_Level = level;

                //modelT_Bus_WorkOrder.F_CreateUser = userModel.F_UserCode;//登记人工号
                modelT_Bus_WorkOrder.F_CreateTime = DateTime.Now;//登记时间
                modelT_Bus_WorkOrder.F_WorkState = 0;//工单状态0登记中
                modelT_Bus_WorkOrder.F_IsClosed = 0;//工单是否关闭
                modelT_Bus_WorkOrder.F_IsOverdue = 0;//是否逾期(0:否，1：是)
                modelT_Bus_WorkOrder.F_IsDelete = 1;//是否删除(0:否，1：是)
                modelT_Bus_WorkOrder.F_IsEnabled = 0;//是否处理

                new BLL.T_Bus_WorkOrder().Add(modelT_Bus_WorkOrder);
                workorderid = modelT_Bus_WorkOrder.F_WorkOrderId;
                #endregion

                #region 插入操作记录
                Model.T_Bus_Operation oper = new Model.T_Bus_Operation();
                oper.F_WorkOrderId = modelT_Bus_WorkOrder.F_WorkOrderId;
                oper.F_State = modelT_Bus_WorkOrder.F_WorkState;

                string userinfo = "";
                var sourceinfo = new BLL.T_Sys_DictionaryValue().GetModel(source);
                if (sourceinfo != null) { userinfo = "由 " + sourceinfo.F_Value; }
                if (modelT_Bus_WorkOrder.F_WorkState == 0)
                {
                    oper.F_Message = userinfo + " 登记了工单,工单编号：" + modelT_Bus_WorkOrder.F_WorkOrderId;
                }

                //oper.F_CreateUser = userModel.F_UserCode;
                oper.F_CreateTime = DateTime.Now;
                oper.F_IsDelete = 0;

                new BLL.T_Bus_Operation().Add(oper);
                #endregion

                trans.Complete();
            }
            return Success("操作成功");
        } 

        /// <summary>
        /// 绩效考核
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult GetAreaAssessment(DateTime? start, DateTime? end)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }

            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_AreaAssessmentReport", paras, "AreaAssessmentReport");

            return Success("成功", obj.Tables[0]);

        }

        /// <summary>
        /// 县区受理
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult GetAreaAccept(DateTime? date)
        {
            if (date == null)
            {
                date = DateTime.Now;
            }

            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@date", date.Value.ToString("yyyy-MM"));
            var obj = DbHelperSQL.RunProcedure("P_AreaAcceptReport", paras, "AreaAcceptReport");

            return Success("成功", obj.Tables[0]);
        }

        /// <summary>
        /// 县区办理
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult GetAreaDeal(DateTime? date)
        {
            if (date == null)
            {
                date = DateTime.Now;
            }

            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@date", date.Value.ToString("yyyy-MM"));
            var obj = DbHelperSQL.RunProcedure("P_AreaDealReport", paras, "AreaDealReport");

            return Success("成功", obj.Tables[0]);

        }

        /// <summary>
        /// 县区各类型统计
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult GetAreaCount(DateTime? start, DateTime? end, int type)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }


            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            paras.Add("@type", type.ToString());
            var obj = DbHelperSQL.RunProcedure("P_AreaCountReport", paras, "AreaCountReport");

            return Success("成功", obj.Tables[0]);

        }

    }
}