using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;
using Learun.Util.Operat;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{
    /// <summary>
    /// 开关厂家
    /// </summary>
    public class RefCBController : MvcControllerBase
    {

        private ec_RefCBService serv = new ec_RefCBService();
        #region 网页
        /// <summary>
        /// 加载对应的Index.cshtml
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 表单页。如new update时，编辑时在form.js里去调用getformdata去查询
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }
        #endregion

        /// <summary>
        /// 获取列表分页数据
        /// <param name="pagination">分页参数</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string queryJson)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            paginationobj.sidx = "CreateTime";
            paginationobj.sord = "DESC";
            var data = serv.GetList(queryJson, paginationobj);
            var jsonData = new
            {
                rows = data,
                total = paginationobj.total,
                page = paginationobj.page,
                records = paginationobj.records
            };
            return Success(jsonData);
        }

        /// <summary>
        /// 获取表单数据。'/ZZDT_EC/RefCB/Form?keyValue=' + keyValue
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetFormData(string keyValue)
        {
            var data = SqlSugarHelper.Db.Queryable<ec_RefCircuitBreakerEntity>().First(x=>x.ID == keyValue);
            var jsonData = new
            {
                ec_RefCircuitBreakerEntity = data
            };
            return Success(jsonData);
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string strEntity)
        {
            var entity = strEntity.ToObject<ec_RefCircuitBreakerEntity>();
            //新建、编辑业务表时，业务表名称、业务表编号不允许重复。
            var data = serv.GetList("{}").ToList();
            data = data.FindAll(x => x.Name == entity.Name);
            if (data != null && data.Count > 0)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    data = data.FindAll(x => x.ID != keyValue);
                    if (data != null && data.Count > 0)
                    {
                        return Fail("已有同名的开关型号！");//修改时，改了名字，结果和已有的重复了
                    }
                }
                else
                {
                    return Fail("已有同名的开关型号！");
                }
            }
            serv.SaveEntity(ref keyValue, entity);
            return Success("保存成功！", "厂家开关管理", string.IsNullOrEmpty(keyValue) ? OperationType.Create : OperationType.Update, null, null);
        }

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string keyValue)
        {
            SqlSugarHelper.Db.Deleteable<ec_RefCircuitBreakerEntity>(keyValue).ExecuteCommand();
            return Success("删除成功！", "厂家开关管理", OperationType.Delete, null, null);
        }
    }
}