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 StepService : Repository<Models.Step>
    {
        /// <summary>
        /// 0-主控台 1-修复台
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public List<Step> GetListNav(int tag)
        {
            return base.AsSugarClient().Queryable<Step>()
                .Includes(m => m.ProcessList.OrderBy(s=>s.Order).ToList()) 
                .Where(m => m.Tag == tag)
                .ToList();
        }
        public bool InsertNav(Models.Step model)
        {
            return base.AsSugarClient().InsertNav(model)
                .Include(a => a.ProcessList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
                //.Include(a => a.ProcessList2) //第2个导航
                .ExecuteCommand();
        }
        public bool UpdateNav(Models.Step model)
        {
            return base.AsSugarClient().UpdateNav(model)
                .Include(a => a.ProcessList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
                                            //.Include(a => a.ProcessList2) //第2个导航
                .ExecuteCommand();
        }
        public bool DelNav(Models.Step model)
        {
            return base.AsSugarClient().DeleteNav(model)
                .Include(a => a.ProcessList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
                                            //.Include(a => a.ProcessList2) //第2个导航
                .ExecuteCommand();
        } 
        //获取所有流程内工序
        public List<StepProcess> GetStepProcess(int pid)
        {
            var db = base.Change<StepProcess>();//切换仓储（新功能）
            return db.GetList().Where(m => m.Pid == pid).OrderBy(m=>m.Order).ToList();
        }
        /// <summary>
        /// 流程是否有产品正使用
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public bool InUse(int pid)
        {
            var db = base.Change<Product>();//切换仓储（新功能）
            return db.GetList().Where(m => m.StepId == pid || m.ReviseStepId == pid || m.AssistStepId == pid).Count()>0;
        }

        //分页
        //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);
        //}
    }
}
