using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class ProductService : Repository<Models.Product>
    {
        public List<Product> GetListNav()
        {
            return base.AsSugarClient().Queryable<Product>()
                .Includes(m => m.AttachmentList.Where(c => c.TBName == "product").OrderBy(x => x.Type).ToList())
                .Includes(m => m.BatchHistoryList.OrderByDescending(x => x.Id).ToList())
                .Includes(m => m.ProductProcessList)
                .Includes(m => m.ProductReviseProcessList)
                .Includes(m => m.ProductAssistProcessList)
                .Includes(m => m.QualifiedCriterionList)
                .Includes(m => m.ClassesInfo)
                .Includes(m => m.StepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())//查询2级(等于EF ThenInclude)
                .Includes(m => m.ReviseStepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .Includes(m => m.AssistStepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .ToList();
        }
        public Product GetModelNav(string code)
        {
            return base.AsSugarClient().Queryable<Product>()
                .Includes(m => m.AttachmentList.Where(c => c.TBName == "product").OrderBy(x => x.Type).ToList())
                .Includes(m => m.BatchHistoryList.OrderByDescending(x => x.Id).ToList())
                .Includes(m => m.ProductProcessList)
                .Includes(m => m.ProductReviseProcessList)
                .Includes(m => m.ProductAssistProcessList)
                .Includes(m => m.QualifiedCriterionList)
                .Includes(m => m.ClassesInfo)
                .Includes(m => m.StepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .Includes(m => m.ReviseStepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .Includes(m => m.AssistStepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .First(m => m.Code == code);
        }
        public Product GetModelNav(int id)
        {
            return base.AsSugarClient().Queryable<Product>()
                .Includes(m => m.AttachmentList.Where(c => c.TBName == "product").OrderBy(x => x.Type).ToList())
                .Includes(m => m.BatchHistoryList.OrderByDescending(x => x.Id).ToList())
                .Includes(m => m.ProductProcessList)
                .Includes(m => m.ProductReviseProcessList)
                .Includes(m => m.ProductAssistProcessList)
                .Includes(m => m.QualifiedCriterionList)
                .Includes(m => m.ClassesInfo)
                .Includes(m => m.StepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .Includes(m => m.ReviseStepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .Includes(m => m.AssistStepInfo, info => info.ProcessList.OrderBy(x => x.Order).ToList())
                .First(m => m.Id == id);
        }
        public bool InsertNav(Models.Product model)
        {
            return base.AsSugarClient().InsertNav(model)
                .Include(a => a.BatchHistoryList)
                .Include(m => m.QualifiedCriterionList)
                //.Include(a => a.ProductProcessList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
                //.Include(a => a.ProductReviseProcessList) //第2个导航 
                //.Include(a => a.ProductAssistProcessList) //第2个导航
                .ExecuteCommand();
        }
        public bool UpdateNav(Models.Product model)
        {
            return base.AsSugarClient().UpdateNav(model)
                .Include(m => m.AttachmentList)
                .Include(m => m.BatchHistoryList) 
                .Include(a => a.ProductProcessList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
                .Include(a => a.ProductReviseProcessList) //第2个导航
                .Include(a => a.ProductAssistProcessList) //第2个导航
                .Include(m => m.QualifiedCriterionList)
                .ExecuteCommand();
        }
        public bool DelNav(Models.Product model)
        {
            return base.AsSugarClient().DeleteNav(model)
                .Include(m => m.AttachmentList.Where(c => c.TBName == "product").ToList())
                .Include(a => a.BatchHistoryList)
                .Include(a => a.ProductProcessList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
                .Include(a => a.ProductReviseProcessList) //第2个导航
                .Include(a => a.ProductAssistProcessList) //第2个导航
                .Include(m => m.QualifiedCriterionList)
                .Include(m => m.OrderList).ThenInclude(m =>m.OrderHistoryList)
                .ExecuteCommand();
        }
        public bool DelProductProcess(int type,int pid)
        { 
            var db = base.Change<ProductProcess>();//切换仓储（新功能）
            return db.Delete(m =>m.Pid == pid);
        }
        public bool DelProductReviseProcess(int type, int pid)
        {
            var db = base.Change<ProductReviseProcess>();//切换仓储（新功能）
            return db.Delete(m => m.Pid == pid);
        }
        /// <summary>
        /// 获取流程工序 0-主控台 1-修复台
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public List<Step> GetStepList(int tag)
        {
            var db = base.Change<Step>();//切换仓储（新功能）
            return db.AsSugarClient().Queryable<Step>()
                .Includes(m => m.ProcessList.OrderBy(s => s.Order).ToList())
                .Where(m => m.Tag == tag)
                .ToList();
        }

        //分页
        //public List<Models.User> GetOrderPage(Expression<Func<Models.User, bool>> where, int pagesize, int pageindex)
        //{
        //    return base.GetPageList(where, new SqlSugar.PageModel() { PageIndex = pageindex, PageSize = pagesize }); //使用自已的仓储方法
        //}

        //调用仓储扩展方法
        //public List<Models.User> GetOrderByJson(string Json)
        //{
        //    return base.CommQuery(Json);
        //}
    }
}
