/**
* 命名空间: CZFW.Category.Logic
*
* 功 能: N/A
* 类 名: DictItemLogic
*
* Ver 变更日期               负责人 变更内容
* ───────────────────────────────────
* V0.01 2016/12/20 19:05:07 曹湘 初稿
*
* Copyright (c) 2016 CHUANGZHIKEJI Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│ 此技术信息为本公司机密信息,未经本公司书面同意禁止向第三方披露. │
*│ 版权所有:创执科技(北京)有限公司                    │
*└──────────────────────────────────┘
*/

using System.Collections.Generic;
using CZFW.Framework.Interface;
using CZFW.Framework.Model.Entity;
using CZFW.Framework.Model.ViewModel;
using System.Linq;

namespace CZFW.Framework.Logic
{
    public class DictItemLogic : LogicBase<DictItemEntity>, IDictItem
    {

        public IList<DictItemModel> Get(string id)
        {
            int i = 0;
            if (int.TryParse(id, out i))
                return GetDictItemsById(i);
            else
                return GetDictItemByMark(id);
        }

        public IList<DictItemModel> GetDictItemByMark(string mark)
        {
            var dictEntity = DbContext.Set<DictEntity>().Where(x => x.Mark == mark).SingleOrDefault();
            var res = Queryable.Where(x => x.DictMark == mark).Select(x => new DictItemModel()
            {
                Id = x.Id,
                Description = x.Description,
                DictId = x.DictId,
                Name = x.Name,
                DictMark = x.DictMark,
                SortOrder = x.SortOrder ?? 0,
                Value = x.Value,
                IsDefault = dictEntity.DefaultItemId == x.Id
            }).OrderByDescending(x => x.SortOrder).ToList();
            return res;
        }

        public IList<DictItemModel> GetDictItemsById(int id)
        {
            var dictEntity = DbContext.Set<DictEntity>().Where(x => x.Id == id).SingleOrDefault();
            var res = Queryable.Where(x => x.DictId == id).Select(x => new DictItemModel()
            {
                Id = x.Id,
                Description = x.Description,
                DictId = x.DictId,
                Name = x.Name,
                DictMark = x.DictMark,
                SortOrder = x.SortOrder ?? 0,
                Value = x.Value,
                IsDefault = x.Id == dictEntity.DefaultItemId
            }).OrderByDescending(x => x.SortOrder).ToList();
            return res;
        }

        public TableModel<DictItemModel> GetDictItemsTableById(int id, int pageIndex = 1, int pageSize = 20)
        {
            var model = new TableModel<DictItemModel>();
            model.SetData(GetDictItemsById(id));
            model.Pager = new PagerModel(GetCount(x => x.DictId == id), pageIndex, pageSize);
            model.TableHeads = PropertyHelper.Get(typeof(DictItemModel));
            return model;
        }

        public IList<KeyValuePair<string, int>> GetEnumItems(string enumName)
        {
            var res = PropertyHelper.GetEnumValues(enumName);
            return res;
        }

        public DictItemModel GetModel(int dictItemId)
        {
            var res = Queryable.Where(x => x.Id == dictItemId).Select(x => new DictItemModel
            {
                Id = x.Id,
                Description = x.Description,
                DictId = x.DictId,
                DictMark = x.DictMark,
                Name = x.Name,
                Value = x.Value
            }).SingleOrDefault();
            return res;
        }

        public IList<DictItemModel> GetModelsByIds(params int[] ids)
        {
            var res = Queryable.Where(x => ids.Contains(x.Id)).Select(x => new DictItemModel
            {
                Id = x.Id,
                Description = x.Description,
                DictId = x.DictId,
                DictMark = x.DictMark,
                Name = x.Name,
                Value = x.Value
            }).ToList();
            return res;
        }
    }
}