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

namespace CallCenterApi.Interface.Controllers.order
{
    public class ProductController : BaseController
    {
        private BLL.article articleBLL = new BLL.article();
        private BLL.article_category categoryBLL = new BLL.article_category();



        #region 商品
        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterProduct filter)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var sql = " ";
            Model.PageData<Model.article> pageModel = new Model.PageData<Model.article>();
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                 " dt_channel_article_goods as product left join dt_article_category as c on c.id=product.category_id ",
                 "product.id",
                 "product.*,c.title as categorytitle",
                 sql,
                 "ORDER BY product.id desc ",
                 filter.PageSize,
                 filter.PageIndex,
                 true,
                 out recordCount);
            List<Model.article> modelList = new BLL.article().DataTableToList(dt);
            var list = new List<ProductDto>();
            foreach (var item in modelList)
            {
                list.Add(new ProductDto
                {
                    id = item.id,
                    title = item.title,
                    description = item.zhaiyao,
                    detail = item.content,
                    goodno = item.goods_no,
                    categoryid = item.category_id,
                    department = item.department,
                    addtime = item.add_time,
                    imgurl = item.img_url,
                    sort = item.sort_id,
                    status = Enum.GetName(typeof(EnumProductStatus), item.status),
                    sellprice = item.sell_price ?? 0,
                    updatetime = item.update_time ?? DateTime.Now,
                    categorytitle = item.categorytitle

                });
            }

            var obj = new
            {
                rows = list,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取商品实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetModel(int id)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var model = articleBLL.GetModel(id);
            if (model == null)
                return Error("该商品不存在");
            var obj = new ProductDto
            {
                id = model.id,
                title = model.title,
                description = model.zhaiyao,
                detail = model.content,
                goodno = model.goods_no,
                categoryid = model.category_id,
                department = model.department,
                addtime = model.add_time,
                imgurl = model.img_url,
                sort = model.sort_id,
                status = Enum.GetName(typeof(EnumProductStatus), model.status),
                sellprice = model.sell_price ?? 0,
                updatetime = model.update_time ?? DateTime.Now,
                categorytitle = model.categorytitle

            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 增加/修改商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult CreateOrUpdate(ProductInput input)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var model = new Model.article();
            if (input.Id <= 0)
            {
                model.category_id = input.CategoryId;
                model.title = input.Name;
                model.zhaiyao = input.Description;
                model.content = input.Detail;
                model.add_time = DateTime.Now;
                model.sell_price = input.SellPrice;
                model.goods_no = input.ProductNum;
                model.img_url = input.ImageUrl;
                model.status = input.Status;
                if (articleBLL.Add(model) > 0)
                    return Success("添加成功");
                return Error("添加失败");
            }
            model = articleBLL.GetModel(input.Id);
            if (model == null)
                return Error("该商品不存在");
            model.title = input.Name;
            model.zhaiyao = input.Description;
            model.content = input.Detail;
            model.update_time = DateTime.Now;
            model.sell_price = input.SellPrice;
            model.goods_no = input.ProductNum;
            model.img_url = input.ImageUrl;
            model.status = input.Status;
            if (articleBLL.Update(model))
                return Success("修改成功");
            return Error("修改失败");
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <returns></returns>
        public ActionResult DeleteBatch(int[] ids)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var sql = "";
            if (ids.Length <= 0)
                return Error("请选择");

            sql += " id in (" + string.Join(",", ids) + ") ";
            if (articleBLL.DeleteBatch(sql))
                return Success("删除成功");
            return Error("删除失败");

        }

        #endregion

        #region 商品类别
        /// <summary>
        /// 获取类别列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetCategoryList(FilterCategory filter)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var sql = " and c.parentId>0 ";
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                 "dt_article_category as c left join dt_article_category as ct on ct.id=c.parentid ",
                 "c.id",
                 "c.*,ct.title as parenttitle",
                 sql,
                 "ORDER BY c.id desc",
                 filter.PageSize,
                 filter.PageIndex,
                 true,
                 out recordCount);
            var modelList = categoryBLL.DataTableToList(dt);
            var list = new List<CategoryDto>();
            foreach (var item in modelList)
            {
                list.Add(new CategoryDto
                {
                    id = item.id,
                    title = item.title,
                    parenttitle = item.parenttitle,
                    sort = item.sort,
                    status = Enum.GetName(typeof(EnumCategoryStatus), item.status)
                });
            }

            var obj = new
            {
                rows = list,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取类别下拉框列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCategoryListDrop()
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var list = categoryBLL.DataTableToList(categoryBLL.GetList(-1));
            return Success("获取成功", list.Select(x => new
            {
                id = x.id,
                title = x.title
            }));
        }

        /// <summary>
        /// 获取类别实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetCategoryModel(int id)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var item = categoryBLL.GetModel(id);
            return Success("获取成功", new
            {
                id = item.id,
                title = item.title,
                parenttitle = item.parenttitle,
                sort = item.sort,
                status = Enum.GetName(typeof(EnumCategoryStatus), item.status)

            });
        }

        /// <summary>
        /// 增加/修改类别
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult CreateOrUpdateCategory(CategoryInput input)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var model = new Model.article_category();
            if (input.Id <= 0)
            {
                model.title = input.Name;
                // model.content = input.Description;
                if (categoryBLL.Add(model) > 0)
                    return Success("添加成功");
                return Error("添加失败");
            }
            model = categoryBLL.GetModel(input.Id);
            if (model == null)
                return Error("该分类不存在");
            model.title = input.Name;
            //model.content = input.Description;
            if (categoryBLL.Update(model))
                return Success("修改成功");
            return Error("修改失败");
        }

        /// <summary>
        /// 删除类别
        /// </summary>
        /// <returns></returns>
        public ActionResult DeleteCategoryBatch(int[] ids)
        {
            //if (Request.IsAuthenticated)
            //    return NoToken("未知错误，请重新登录");
            var sql = "";
            if (ids.Length <= 0)
                return Error("请选择");

            sql += " id in (" + string.Join(",", ids) + ") ";
            if (categoryBLL.DeleteBatch(sql))
                return Success("删除成功");
            return Error("删除失败");

        }
        #endregion

        #region 快递
        //public ActionResult GetExpressList()
        //{

        //}
        #endregion



    }
}