using Model.APIItem.HJGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.API.HJGL
{
    public class APIWelderService
    {
        public static WelderItem GetWelderByIdCard(string idCard, string projectId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                WelderItem item = new WelderItem();
                var welder = db.BS_Welder.Where(u => u.IdentityCard == idCard && u.ProjectId == projectId).FirstOrDefault();
                if (welder != null)
                {
                   
                    item.WED_ID = welder.WED_ID;
                    item.WED_Code = welder.WED_Code;
                    item.WED_Name = welder.WED_Name;
                    item.WED_Unit = welder.WED_Unit;
                    item.TeamGroupId = welder.TeamGroupId;
                    item.WED_Sex = welder.WED_Sex;
                    if (welder.WED_Birthday.HasValue)
                        item.WED_Birthday = welder.WED_Birthday.Value.ToString("yyyy-MM-dd");
                    if (welder.LimitDate.HasValue)
                        item.LimitDate = welder.LimitDate.Value.ToString("yyyy-MM-dd");
                    item.WED_WorkCode = welder.WED_WorkCode;
                    item.WED_Class = welder.WED_Class;
                    if (welder.WED_IfOnGuard.HasValue)
                        item.WED_IfOnGuard = welder.WED_IfOnGuard.Value.ToString();
                    item.ProjectId = welder.ProjectId;
                    item.IdentityCard = welder.IdentityCard;
                    item.WederType = welder.WederType;


                    item.AttachUrl = welder.AttachUrl;
                    if (!string.IsNullOrEmpty(welder.WED_Unit))
                    {
                        var unit= db.Base_Unit.FirstOrDefault(x => x.UnitId == welder.WED_Unit);
                        if (unit != null)
                        {
                            item.UnitName = unit.UnitName;
                        }
                    }
                }
                return item;
            }



        }

        public static List<WeldingMethodItem> GetWelderMethod(string welderId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var items = from x in db.BS_WeldMethodItem where x.WED_ID == welderId select x.WME_ID;
                var methods = from m in db.Base_WeldingMethod
                              where items.Count() == 0 || items.Contains(m.WeldingMethodId)
                              select new WeldingMethodItem
                              {
                                  WeldingMethodId = m.WeldingMethodId,
                                  WeldingMethodCode = m.WeldingMethodCode,
                                  WeldingMethodName = m.WeldingMethodName,
                                  ConsumablesType = m.ConsumablesType,
                                  Remark = m.Remark
                              };

                return methods.ToList();
            }
        }

        public static List<BSSteelItem> GetWelderBSSteel(string welderId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var itemsSteel = from x in db.BS_WelderItem where x.WED_ID == welderId select x.STE_ID;
                var steel = from x in db.BS_Steel
                            where itemsSteel.Count() == 0 || itemsSteel.Contains(x.STE_ID) select new BSSteelItem
                {
                    STE_ID = x.STE_ID,
                    STE_Code = x.STE_Code,
                    STE_Name = x.STE_Name,
                    STE_SteelType = x.STE_SteelType,
                    STE_Remark = x.STE_Remark,
                    MaterialType = x.MaterialType,
                    MaterialGroup = x.MaterialGroup
                };

                return steel.ToList();
            }
        }

        public static List<ConsumablesItem> GetConsumables()
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var steel = from x in db.Base_Consumables                          
                            select new ConsumablesItem
                            {
                                ConsumablesId = x.ConsumablesId,
                                ConsumablesCode = x.ConsumablesCode,
                                ConsumablesName = x.ConsumablesName,
                                ConsumablesType = x.ConsumablesType,
                                SteelType = x.SteelType,
                                SteelFormat = x.SteelFormat,
                                Standard = x.Standard,
                                Remark = x.Remark

                            };

                return steel.ToList();
            }
        }
    }
}