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 NoticeCriticismController : BaseController
    {
        BLL.T_Bus_NoticeCriticism bll = new BLL.T_Bus_NoticeCriticism();
        // GET: NoticeCriticism
        public ActionResult Index()
        {
            return View();
        }

        // 获取通告列表
        public ActionResult GetList(string startdate,string enddate)
        {
            DataTable dt = new DataTable();
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            string sql = " ";
            if (startdate != null && startdate.Trim() != "")
            {
                sql += " and Datediff(day,F_CreateTime,'" + startdate.Trim() + "')>=0";                
            }
            if (enddate != null && enddate.Trim() != "")
            {              
                sql += " and Datediff(day,F_CreateTime,'" + enddate.Trim() + "')<=0";
            }
            sql += " and (F_WorkOrderId is null or F_WorkOrderId='')";
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Bus_NoticeCriticism",
                "F_Id",
                "*,dbo.GetDeptNames(F_DeptIds) as NoticeDeptName",
                sql,
                "ORDER BY F_Id desc",
                pagesize,
                pageindex,
                true,
                out recordCount);
           
            var obj = new
            {
                dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        [Authority]
        //添加通报
        public ActionResult AddNotice(string deptids, string content,int noticeid,string noticeinfo)
        {
            Model.T_Bus_NoticeCriticism modelT_Bus_NoticeCriticism = new Model.T_Bus_NoticeCriticism();
            modelT_Bus_NoticeCriticism.F_DeptIds = deptids;
            modelT_Bus_NoticeCriticism.F_WorkOrderId = "";
            modelT_Bus_NoticeCriticism.F_Content = content;
            modelT_Bus_NoticeCriticism.F_State = 1;
            modelT_Bus_NoticeCriticism.F_IsDelete = 0;
            modelT_Bus_NoticeCriticism.F_CreateTime = DateTime.Now;
            modelT_Bus_NoticeCriticism.F_CreateUser = User.F_UserId.ToString();
            modelT_Bus_NoticeCriticism.F_NoticeID  = noticeid;
            modelT_Bus_NoticeCriticism.F_NoticeInfo = noticeinfo;
            int b=new BLL.T_Bus_NoticeCriticism().Add(modelT_Bus_NoticeCriticism);

            if (b > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }
        }

        //编辑通报
        public ActionResult EditNotice(string nid, string deptids, string content, int noticeid, string noticeinfo)
        {
            
            if (nid != null && nid.Trim() != "")
            {
                Model.T_Bus_NoticeCriticism dModel = bll.GetModel(int.Parse(nid.Trim()));
                if (dModel != null)
                {
                    
                    dModel.F_DeptIds = deptids;
                    //dModel.F_WorkOrderId = "";
                    dModel.F_Content = content;
                    dModel.F_State = 1;
                    dModel.F_IsDelete = 0;
                    //dModel.F_CreateTime = DateTime.Now;
                    //dModel.F_CreateUser = User.F_UserId;
                    dModel.F_NoticeID = noticeid;
                    dModel.F_NoticeInfo = noticeinfo;

                    bool b = bll.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
            }
            else
            {
                return Error("id不能为空");
            }

        }
        [Authority]
        //删除通报
        public ActionResult DelNotice(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idds = "";
                for (int i = 0; i < ids.Length; i++)
                {
                    idds += ids[i] + ",";
                }
                bool r = bll.DeleteList(idds.Trim(','));

                if (r)
                    return Success("删除成功");
                else
                {
                    return Error("删除失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //获取通报
        public ActionResult GetNotice(string nid)
        {
            Model.T_Bus_NoticeCriticism dModel = bll.GetModel(int.Parse(nid.Trim()));
            if (dModel != null)
            {
                string NoticeDeptName = "";
                if (dModel.F_DeptIds!= null && dModel.F_DeptIds.ToString() != "")
                {
                     NoticeDeptName = DbHelperSQL.GetSingle(" select dbo.GetDeptNames('" + dModel.F_DeptIds.ToString() + "')").ToString ();
                }
                var obj = new
                {
                   dModel,
                    NoticeDeptName
                };
                return Success("获取通报详情成功", obj);
            }
            else
            {
                return Error("获取通报详情失败");
            }

        }


    }
}