using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Filter;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.customer
{
    public class CustomerFieldController : BaseController
    {
        private readonly BLL.T_Cus_CustomerField customerFieldBLL = new BLL.T_Cus_CustomerField();
        private readonly BLL.T_Form_EntityAttribute entityAttrBLL = new BLL.T_Form_EntityAttribute();


        /// <summary>
        /// 右侧系统字段+已选择的自定义字段列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterCustomerField filter)
        {
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "T_Cus_CustomerField",
                "F_Id",
                "*",
                "",
                "ORDER BY F_FieldType ASC,  F_Sort ASC ",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);
            var fieldList = customerFieldBLL.DataTableToList(dt);

            var obj = new
            {
                rows = fieldList,
                total = recordCount
            };

            return Content(obj.ToJson());
        }

        /// <summary>
        /// 添加选中的自定义字段 / 修改已选中的自定义字段
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult CreateOrUpdate(CustomerFieldInput input)
        {
            Model.T_Cus_CustomerField model = new Model.T_Cus_CustomerField();
            Model.T_Form_EntityAttribute attrModel;
            if (input.Id <= 0 && input.AttributeId > 0)
            {
                attrModel = entityAttrBLL.GetModel(input.AttributeId);
                if (attrModel == null)
                    return Error("该条记录不存在");
                var sort = DbHelperSQL.GetSingle("SELECT MAX(F_Sort) from T_Cus_CustomerField") ?? 0;
                model.F_FieldId = attrModel.F_AttributeId;
                model.F_FieldType = attrModel.F_EntityId;
                model.F_Name = attrModel.F_Name;
                model.F_DBFieldName = attrModel.F_DBFieldlName;
                model.F_DBTypeName = attrModel.F_DBTypeName;
                model.F_Sort = Convert.ToInt32(sort) + 1;
                if (customerFieldBLL.Add(model) > 0)
                    return Success("添加成功");
                return Error("添加失败");
            }
            if (input.Id > 0)
            {
                model = customerFieldBLL.GetModel(input.Id);
                if (model == null)
                    return Error("该条记录不存在");
                model.F_Name = string.IsNullOrWhiteSpace(input.Name) ? model.F_Name : input.Name;
                model.F_Sort = input.Sort >= 0 ? input.Sort : model.F_Sort;
                model.F_IsNecessary = input.IsNecessary >= 0 ? input.IsNecessary : model.F_IsNecessary;
                model.F_IsExportOrInput = input.IsExportOrInput >= 0 ? input.IsExportOrInput : model.F_IsExportOrInput;
                model.F_IsShowOnList = input.IsShowOnList >= 0 ? input.IsShowOnList : model.F_IsShowOnList;
                model.F_IsShowOnScrean = input.IsShowOnScrean >= 0 ? input.IsShowOnScrean : model.F_IsShowOnScrean;
                model.F_Search = input.Search >= 0 ? input.Search : model.F_Search;


                if (customerFieldBLL.Update(model))
                    return Success("修改成功");
                return Error("修改失败");
            }
            return Error("参数错误");
        }

        /// <summary>
        /// 删除已选中的自定义字段
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                if (customerFieldBLL.DeleteBatch($" F_Id in ({string.Join(",", ids)}) "))
                    return Success("删除成功");
                return Error("删除失败");
            }
            return Error("参数错误");
        }

        /// <summary>
        /// 左侧所有自定义字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetEntity(FilterExcelTmp filter)
        {
            var sql = "  and F_DBTableName='T_Cus_CustomerExpand' and F_AttributeId not in (select F_FieldId from T_Cus_CustomerField where F_FieldType=1) ";

            if (filter.Type > 0)
            {
                sql += " and F_DBTypeName='" + filter.Type + "' ";
            }
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "T_Form_EntityAttribute",
                "F_AttributeId",
                "*",
                sql,
                "ORDER BY F_AttributeId ",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);
            var fieldList = entityAttrBLL.DataTableToList(dt);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());
        }
    }
}