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

namespace CallCenterApi.Interface.Controllers.callout
{
    public class CallOutController : BaseController
    {
        private BLL.T_Wo_WorkOrder bllwo = new BLL.T_Wo_WorkOrder();
        private BLL.T_Que_Question_Answer bllQue = new BLL.T_Que_Question_Answer();
        private BLL.T_Que_Question_Map bllQueMap = new BLL.T_Que_Question_Map();

        #region 问卷回答 - new
        /// <summary>
        /// 添加 问卷回答 - 物业
        /// </summary>
        /// <param name="workorderid">工单号</param>
        /// <param name="ans">提交题目内容</param>
        /// <param name="hjjgid">呼叫结果id(字典)</param>
        /// <returns></returns>
        public ActionResult AddAnswers(string workorderid, string[] ans, int hjjgid)
        {
            string usercode = CurrentUser.UserData.F_UserCode;
            var nn = 0;
            if (string.IsNullOrEmpty(workorderid))
                return Error("参数错误");
            var model = bllwo.GetModel(workorderid);
            if (model != null)
            {
                #region 保存答案
                var modelQueMap = new Model.T_Que_Question_Map();
                if (ans != null)
                {
                    //先删除 后添加
                    bllQueMap.Delete(workorderid);                    
                    modelQueMap.WorkOrderId = workorderid;
                    modelQueMap.CreateBy = usercode;
                    #region 循环保存
                    foreach (var item in ans)
                    {
                        var ii = item.Split('_');
                        modelQueMap.QuestionId = int.Parse(ii[0]);
                        if (ii[1].IndexOf('|') > 0)
                        {
                            //格式：2_3|5|2|一般
                            //格式：Id_IId|总分|得分|评价项名称
                            var iii = ii[1].Trim('|').Split('|');
                            modelQueMap.QuestionIId = int.Parse(iii[0]);
                            modelQueMap.TotalScore = int.Parse(iii[1]);
                            modelQueMap.Score = int.Parse(iii[2]);
                            modelQueMap.AnswersTitle = iii[3];
                            modelQueMap.CreateTime = DateTime.Now;
                            if (bllQueMap.Add(modelQueMap) > 0)
                                nn++;
                        }
                        else
                        {
                           //
                        }
                    }
                    #endregion
                }
                #endregion
                #region 更新工单表回访信息
                //model.IsReturnBak = 1;
                //model.IsReturn = hjjgid;
                //model.ResponUser = usercode;  //回访人code
                //model.ReturnBakTime = DateTime.Now;
                bllwo.Update(model);
                #endregion
            }
            if (ans != null)
            {
                if (nn == ans.Length)
                {
                    return Success("问卷答案提交成功！工单编码：" + workorderid + ",操作人：" + CurrentUser.UserData.F_UserCode);
                }
                else
                {
                    bllQueMap.Delete(workorderid);
                    return Error("问卷答案提交失败！工单编码:" + workorderid + ",操作人：" + CurrentUser.UserData.F_UserCode);
                }
            }
            else
            {
                return Error("没有问卷答案提交");
            }

        }
        
        #endregion
    }
}