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 AchievementsController : BaseController
    {
        private BLL.T_Sys_Achievements Achievements = new BLL.T_Sys_Achievements();
        private BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();

        //[Authority]
        /// <summary>
        /// 添加/编辑字典值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddAchievements (Model .T_Sys_Achievements  model)
        {
            if (model .F_ID >0)
            {
                Model.T_Sys_Achievements t_Sys_Achievements=
                   Achievements.GetModel(model.F_ID);
                if (t_Sys_Achievements == null)
                    return Error("修改失败，该记录不存在");
                t_Sys_Achievements.F_Type = model.F_Type;
                t_Sys_Achievements.F_WorkOrderId = model.F_WorkOrderId;
                t_Sys_Achievements.F_Deptid = model.F_Deptid;
                t_Sys_Achievements.F_DeptName = model.F_DeptName;
                t_Sys_Achievements.F_Evaluation = model.F_Evaluation;
                t_Sys_Achievements.F_Bonusitems = model.F_Bonusitems;
                t_Sys_Achievements.F_Score = model.F_Score;
                t_Sys_Achievements.F_Remarks = model.F_Remarks;
                t_Sys_Achievements.F_UpdateUser = User .F_UserCode ;
                t_Sys_Achievements.F_UpdateTime = DateTime .Now ;
                if (Achievements.Update(t_Sys_Achievements))
                    return Success("编辑成功");
                else
                    return Error ("编辑失败");
            }
            else
            {
                Model.T_Sys_Achievements t_Sys_Achievements =
                 new Model.T_Sys_Achievements();
                t_Sys_Achievements.F_Type = model.F_Type;
                t_Sys_Achievements.F_WorkOrderId = model.F_WorkOrderId;
                t_Sys_Achievements.F_Deptid = model.F_Deptid;
                t_Sys_Achievements.F_DeptName = model.F_DeptName;
                t_Sys_Achievements.F_Evaluation = model.F_Evaluation;
                t_Sys_Achievements.F_Bonusitems = model.F_Bonusitems;
                t_Sys_Achievements.F_Score = model.F_Score;
                t_Sys_Achievements.F_Remarks = model.F_Remarks;
                t_Sys_Achievements.F_CreatUser  = User.F_UserCode;
                t_Sys_Achievements.F_CreatTime  = DateTime.Now;
                t_Sys_Achievements.F_IsDelete = 0;
                if (Achievements.Add (t_Sys_Achievements)>0)
                    return Success("添加成功");
                else
                    return Error("添加失败");
            }
        }
        //[Authority]
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDicValue(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("获取参数失败");
            var idStr = string.Join(",", ids);
            if (new BLL.T_Sys_Achievements().DeleteList(idStr))
                return Success("删除成功");
            else
                return Error("删除失败");
        }
        public ActionResult GetList()
        {
            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 ";
            int type = RequestString.GetInt("type", 0);
           int deptid = RequestString.GetInt("deptid", 0);
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("strendtime"));
            if (starttime.Trim() != "" && starttime != "undefined")
            {
                if (strendtime.Trim() != "" && strendtime != "undefined")
                {
                    sql += " and F_CreatTime  between   '" + starttime +
                             "' AND  '" + strendtime + "'";
                }
                else
                {
                    sql += " and F_CreatTime>='" + starttime + "' ";
                }
            }
            #region sql 语句相关处理

            sql += "and F_Type=" + type;
            if (deptid != 0)
            {
                sql += " and  F_Deptid ='"+deptid +"'";
            }
            #endregion
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_Achievements a WITH(NOLOCK)",
                "F_ID",
                "*",
                sql,
                "ORDER BY F_CreatTime DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());
        }
        /// <summary>
        /// 加载字典值
        /// </summary>
        /// <returns></returns>
        public ActionResult GetModel(int id)
        {

            Model.T_Sys_Achievements valueModel = Achievements.GetModel(id);
            if (valueModel != null)
            {
                return Success("加载成功", valueModel);
            }

            else
                return Error("加载失败");

        }


    }
}