using CallCenter.Utility;
using CallCenterApi.DB;
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
{
    /// <summary>
    /// 工单修改日志管理
    /// </summary>
    public class WorkOrderLogsController : BaseController
    {
        /// <summary>
        /// 获取工单修改日志列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            string sql = " and IsDelete=0";
            DataTable dt = new DataTable();
            //模糊搜索
            string orderid = RequestString.GetQueryString("orderid");
            string keyword = RequestString.GetQueryString("keyword");
            string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            //之前接待描述，修改的接待描述，工单编号
            if (!string.IsNullOrEmpty(orderid))
            {
                sql += " and WorkOrderID = '" + orderid + "'  ";
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                sql += " and (BeforeContent like '%" + keyword + "%' or UpdateContent like '%" + keyword + "%') ";
            }
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,AddTime,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,AddTime,'" + strendtime + "')>=0   ";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                " T_Wo_WorkOrder_Logs (NOLOCK) ",
                "T_Wo_WorkOrder_Logs.Id",
                "*",
                sql,
                "ORDER BY T_Wo_WorkOrder_Logs.Id desc",
                pagesize,
                pageindex,
                true,
                out recordCount);
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }
                string sql = "update T_Wo_WorkOrder_Logs set IsDelete=1 where Id in (" + idd.TrimEnd(',') + ")";
                if (!string.IsNullOrEmpty(idd.Trim()))
                {
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        return Success("设置成功");
                    }
                    else
                    {
                        return Error("设置失败");
                    }
                }
            }
            return Error("获取参数失败");
        }
    }

}