using CallCenter.Utility;
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
{
    [Authority]
    public class OperationLogController : BaseController
    {

        /// <summary>
        /// 获取工单日志
        /// </summary>
        /// <param name="isdc"></param>
        /// <returns></returns>
        public ActionResult GetOperationList(int isdc = 0)
        {
            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 ";
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string usercode = HttpUtility.UrlDecode(RequestString.GetQueryString("usercode"));
            int valueID = RequestString.GetInt("valueID", 0);
            int type = RequestString.GetInt("type", -1);
          
            if (!string.IsNullOrEmpty(starttime))
            {
                sql += " and F_CreateTime>='" + starttime + "' ";
            }
            if (!string.IsNullOrEmpty(endtime))
            {
                sql += " and F_CreateTime<='" + endtime + "' ";
            }
            if (valueID>0)
            {
                sql += $" and F_ValueID = '{valueID}'";
            }

            if (type > -1)
            {
                sql += $" and F_Type = '{type}'";
            }
            if (!string.IsNullOrEmpty(usercode))
            {
                sql += $" and F_CreateUser = '{usercode}'";
            }
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                       "T_Sys_OperationLog WITH(NOLOCK)",
                       "F_ID",
                       "*,dbo.GetDictionaryName(F_ValueID) as ValueName",
                       sql,
                       "ORDER BY F_CreateTime desc ",
                       pagesize,
                       pageindex,
                       true,
                       out recordCount);
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        public static  string  AddOperationList(string content,string usercode,string ip,int type=0,int valueid=0,int id=0)
        {
            Model.T_Sys_OperationLog dModel = new Model.T_Sys_OperationLog();
            dModel.F_ValueID = valueid;
            dModel.F_Content = content;
            dModel.F_Type = type;
            dModel.F_CreateUser = usercode;
            dModel.F_IsDelete  = 0;
            dModel.F_IP = ip;
            dModel.F_TypeId = id;
            dModel.F_CreateTime = DateTime.Now;
            int b = new BLL.T_Sys_OperationLog().Add(dModel);
            if (b > 0)
            {
                return "";
            }
            else
            {
                return "添加失败";
            }
        }

    }
}