using CallCenter.Utility;
using CallCenterApi.Common;
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.customer
{
    public class CustomerNewController : BaseController
    {
        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList(int isdc = 0)
        {
            string sql = " and F_IsDelete=0";
            DataTable dt = new DataTable();

          
            string keyWord = HttpUtility.UrlDecode(RequestString.GetQueryString("keyWord"));
       
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            string name = RequestString.GetQueryString("name");
            string mobile = RequestString.GetQueryString("mobile");
            if (!string .IsNullOrEmpty (name))
            {
                sql += "and customer_name='"+ name + "'";
            }
            if (!string.IsNullOrEmpty(mobile))
            {
                sql += "and customer_mobile='" + mobile + "'";
            }
            if (keyWord.Trim() != "" && keyWord != "undefined")
            {
                sql += " and (customer_no like '%" + keyWord + "%' or customer_name like '%" + keyWord + "%' or identify_no like '%" + keyWord + "%' or customer_phone like '%" + keyWord + "%' or customer_mobile like '%" + keyWord + "%' or customer_number like '%" + keyWord + "%'or spouse_name like '%" + keyWord + "%'or account_address like '%" + keyWord + "%') ";
            }


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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            

            
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_Customer a WITH(NOLOCK) ",
                "F_ID",
                "*",
                sql,
                "ORDER BY F_ID 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 GetCustomer()
        {
            int cid = Utils.StrToInt(RequestString.GetQueryString("cid"), 0);
            if (cid != 0)
            {
                Model.T_Sys_Customer  User = new BLL.T_Sys_Customer().GetModel(cid);
                if (User != null)
                {
                    return Success("获取成功", User);
                }
                else
                {
                    return Error("获取失败");
                }
            }
            else
            {
                return Error("参数传输失败");
            }

        }
        /// <summary>
        /// 验证客户电话是否唯一
        /// </summary>
        private bool getunphone(string ukey)
        {
            var sql = " F_IsDelete=0 ";
            sql += "and (customer_ukey='" + ukey + "')";
            var count = new BLL.T_Sys_Customer().GetModelList(sql).Count();
            return count > 0;
        }
        /// <summary>
        /// 添加/修改客户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AddCustomer(Model .T_Sys_Customer input)
        {
           
            #region 验证
            if (string.IsNullOrEmpty(input.customer_name ))
            {
                return Error("请输入客户名称");
            }
            if (getunphone(input .customer_ukey))
                return Error("已存在该唯一号");
            #endregion
            input.F_IsDelete = 0;
            input.F_CreateTime = DateTime.Now;
            int n = new BLL .T_Sys_Customer().Add(input);
            if (n > 0)
            {
                return Success("新增成功！", n);
            }
            else
            {
                return Error("新增失败！");
            }
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelCustomer(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }

                string sql = "update T_Sys_Customer set F_IsDelete=1 where F_CustomerId in(" + idd.TrimEnd(',') + ")";
                if (!string.IsNullOrEmpty(idd.Trim()))
                {
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        return Success("设置成功");
                    }
                    else
                    {
                        return Error("设置失败");
                    }
                }
                else
                {
                    return Error("请选择用户");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }
    }
}