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

namespace CallCenterApi.Interface.Controllers.workorder
{
    public class WOVisitRecordsController : BaseController
    {
        /// <summary>
        /// 获取回访记录
        /// </summary>
        /// <param name="woid">工单ID</param>
        /// <param name="rid">回访结果ID</param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetList(string woid,string rid)
        {
            string sql = "  ";
            DataTable dt = new DataTable();
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            
            if (woid != null && woid.Trim() != "")
            {
                sql += " and F_WorkOrderId=" + woid;
            }
            if (rid != null && rid.Trim() != "")
            {
                sql += " and F_VRId=" + rid;
            }

            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Bus_VisitResultRecords",
                "F_VRRId",
                "*",
                sql,
                "ORDER BY F_VRRId DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);
            List<Model.T_Bus_VisitResultRecords> rlist = new BLL.T_Bus_VisitResultRecords().DataTableToList(dt);
            List<Model.T_Call_CallRecords> clist = new BLL.T_Call_CallRecords().GetModelList("");
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = rlist.Select(r=>  {
                    Model.T_Call_CallRecords cmodel = new Model.T_Call_CallRecords();
                    string path = "";
                    if (r.F_CallRecordId != null)
                        cmodel = clist.Where(c => c.CallRecordsId == r.F_CallRecordId.Value).FirstOrDefault();
                    if (cmodel != null)
                    {
                        path = cmodel.FilePath;
                    }
                    return new
                    {
                        F_AssignedId = r.F_AssignedId,
                        F_CallRecordId = r.F_CallRecordId,
                        F_CreateTime = r.F_CreateTime,
                        F_CreateUser = r.F_CreateUser,
                        F_DeleteTime = r.F_DeleteTime,
                        F_DeleteUser = r.F_DeleteUser,
                        F_File = r.F_File,
                        F_IsDelete = r.F_IsDelete,
                        F_IsSatisfie = r.F_IsSatisfie,
                        F_LimitTime = r.F_LimitTime,
                        F_Remark = r.F_Remark,
                        F_Result = r.F_Result,
                        F_State = r.F_State,
                        F_Type = r.F_Type,
                        F_VisitUser = r.F_VisitUser,
                        F_VRId = r.F_VRId,
                        F_WorkOrderId = r.F_WorkOrderId,
                        F_VRRID = r.F_VRRId,
                        F_ReUser = r.F_ReUser,
                        F_ReTime = r.F_ReTime,
                        F_FilePath = path,
                    };
                }),
                total = recordCount
            };
            return Content(obj.ToJson());
        }
    }
}