using BPA.SAAS.Manage.Application.Device.Dtos.Device;
using BPA.SAAS.Manage.Application.Device.Interface;
using BPA.SAAS.Manage.Core.Base;
using BPA.SAAS.Manage.Core.Device;
using BPA.SAAS.Manage.Core.Product;
using LogicExtensions;
using Microsoft.AspNetCore.Routing.Template;
using NPOI.POIFS.Storage;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.SAAS.Manage.Application.Device.Services
{
    public class TechnologyService : ITechnologyService, ITransient
    {
        private readonly ISqlSugarClient _db;
        public TechnologyService(ISqlSugarClient db) 
        {
            _db=db;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageUtil> GetTechnologyExportRecodePage(TechnologyQueryInputDto inputDto)
        {
            var total = new RefAsync<int>();
            var data = await _db.Queryable<BPA_TechnologyExportRecode,BPA_ProductVesion, BPA_Product>((a, b,c) => new JoinQueryInfos(
                     JoinType.Left, a.DeviceVersionId == b.Id,
                      JoinType.Left, a.ProductId == c.Id
                 ))
                  .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Vesion), (a, b, c) => b.Vesion == inputDto.Vesion)
                  .WhereIF(!string.IsNullOrWhiteSpace(inputDto.ProductName), (a, b, c) => inputDto.ProductName.Contains(c.Name))
                .Select((a, b,c) => new ProductVesionView
                 {
                     Id =a.Id,
                     Vesion = b.Vesion,
                     ProductName = c.Name,
                     CreateAt = a.CreateAt,
                     DeviceVersionId = b.Id,
                    TemplatePath=a.TemplatePath
                })
               
                 .OrderBy(a=>a.CreateAt, OrderByType.Desc)
                 .Mapper(x =>
                 {
                     //var TechnologyInfo1 = _db.Queryable<BPA_Technology>().Where(p => p.DeviceVersionId == x.DeviceVersionId).Select(f => new TechnologyInfo() { Id = f.Id, Name = f.Name, DeviceVersionId = f.DeviceVersionId }).ToList();
                     var TechnologyInfo = _db.Queryable<BPA_Technology>().Where(p => p.DeviceVersionId == x.DeviceVersionId).Select(f => new TechnologyInfo() { Id = f.Id, Name = f.Name, DeviceVersionId = f.DeviceVersionId,CreateAt=f.CreateAt }).Mapper(g =>
                     {
                         g.TechnologyActionInfo= _db.Queryable<BPA_TechnologyAction>().Where(d=>d.TechnologyId==g.Id).ToList();
                     }).ToList();
                     x.TechnologyInfo = TechnologyInfo;
                 })
                 .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);

            return new PageUtil()
            {
                Data = data,
                Total = total

            };
        }
        /// <summary>
        /// 根据设备id查询设备工艺信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public async Task<List<TechnologyView>> GetTechnologyList(string deviceId)
        {
            var device= _db.Queryable<BPA_DeviceInfo>().Where(x=>x.Id== deviceId).First();
            if (device == null) return new List<TechnologyView>();
            var data = await _db.Queryable<BPA_Technology>().Where(x=>x.DeviceVersionId== device.ProductVersionId).Select(a => new TechnologyView
            {
                Id = a.Id,
                Name = a.Name,
                DeviceVersionId = a.DeviceVersionId,
                CreateAt = a.CreateAt,
                 }).OrderBy(a => a.CreateAt, OrderByType.Desc)
                 .Mapper(x =>
                 {
                     //var TechnologyInfo1 = _db.Queryable<BPA_Technology>().Where(p => p.DeviceVersionId == x.DeviceVersionId).Select(f => new TechnologyInfo() { Id = f.Id, Name = f.Name, DeviceVersionId = f.DeviceVersionId }).ToList();
                     var TechnologyInfo = _db.Queryable<BPA_TechnologyAction>().Where(d => d.TechnologyId == x.Id).ToList();
                     x.TechnologyActionInfo = TechnologyInfo;
                 })
                 .ToListAsync();

            return data;
        }
        public async Task<List<TechnologyView>> GetTechnologyList_alm(string deviceId)
        {
            var device = _db.Queryable<BPA_DeviceInfo>().Where(x => x.Id == deviceId).First();
            var data = await _db.Queryable<BPA_Technology>().Where(x => x.DeviceVersionId == device.ProductVersionId).Select(a => new TechnologyView
            {
                Id = a.Id,
                Name = a.Name,
                DeviceVersionId = a.DeviceVersionId,
                CreateAt = a.CreateAt,
            }).OrderBy(a => a.CreateAt, OrderByType.Desc)
                 .Mapper(x =>
                 {
                     //var TechnologyInfo1 = _db.Queryable<BPA_Technology>().Where(p => p.DeviceVersionId == x.DeviceVersionId).Select(f => new TechnologyInfo() { Id = f.Id, Name = f.Name, DeviceVersionId = f.DeviceVersionId }).ToList();
                     var TechnologyInfo = _db.Queryable<BPA_TechnologyAction>().Where(d => d.TechnologyId == x.Id).ToList();
                     x.TechnologyActionInfo = TechnologyInfo;
                 })
                 .ToListAsync();

            return data;
        }
        public async Task<bool> DeTechnologyExportRecode(string inputList)
        {
            try
            {
                _db.Ado.BeginTran();
                var datas = await _db.Queryable<BPA_TechnologyExportRecode>().Where(x => x.Id == inputList).FirstAsync();
                var Technology = _db.Queryable<BPA_Technology>().Where(p => p.DeviceVersionId == datas.DeviceVersionId).ToList();
                var TechnologyIds = Technology.Select(x => x.Id).ToArray();
                var TechnologyAction = _db.Queryable<BPA_TechnologyAction>().Where(p => TechnologyIds.Contains(p.TechnologyId)).ToList();
                _db.Deleteable(TechnologyAction).ExecuteCommand();
                _db.Deleteable(Technology).ExecuteCommand();
                _db.Deleteable(datas).ExecuteCommand();
                _db.Ado.CommitTran();
                return true;
            }
            catch (Exception)
            {
                _db.Ado.RollbackTran();
                throw Oops.Oh("删除失败");
            }
           
        }
    }
}
