1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using iWareCommon.Common.Service;
using iWareCommon.Utils;
using iWareDataCore.BASE.Dao;
using iWareDataCore.BASE.Entity;
using iWareDataCore.ORM;
using iWareDataCore.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareDataCore.BASE.Service
{
    public class PlaceMaterialViewService : CommonService<PlaceMaterialViewEntity, BASEPlaceMaterialView, DbModelCore>
    {
 
        private static object Lock = new object();
 
        private PlaceMaterialViewService() : base(PlaceMaterialViewDao.GetInstance()) { }
 
        private static PlaceMaterialViewService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色服务的单例实体</returns>
        public static PlaceMaterialViewService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new PlaceMaterialViewService();
                    }
                }
            }
            return Instance;
        }
 
 
 
 
        /// <summary>
        /// 获取各种状态的库位
        /// </summary>
        /// <returns></returns>
        public List<PlaceStatusNumEntity> GetPlaceStatusNum(out string msg)
        {
            msg = "";
            List<PlaceStatusNumEntity> psnlst = new List<PlaceStatusNumEntity>();
            try
            {
                using (DbModelCore mcore = new DbModelCore())
                {
                    string sql = @"select A.EmptyPlace,B.FullPlace,C.LockPlace from 
                              (select 1 as id,COUNT(id) as EmptyPlace from BASEPlace where typeid=4 and status=0 )A left join
                              (select 1 as id,COUNT(id) as FullPlace from BASEPlace where typeid=4 and status=2)B on A.id=B.id left join
                              (select 1 as id,COUNT(id) as LockPlace from BASEPlace where typeid=4 and islock=1)C on A.id=C.id";
                    psnlst = mcore.Database.SqlQuery<PlaceStatusNumEntity>(sql).ToList();
                }
                return psnlst;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "GetPlaceStatusNum", ex.Message);
                return psnlst;
            }
        }
 
        /// <summary>
        /// 给调度界面的库位预览及库位信息显示提供的查询结果(也包含placetypeid=17的,17的是无用库位,再次查询出来主要方便前端构图使用,对实际业务并没有意义)
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<PlaceMaterialViewEntity> GetPlacePreview(out string msg)
        {
            msg = "";
            List<PlaceMaterialViewEntity> psnlst = new List<PlaceMaterialViewEntity>();
            try
            {
                using (DbModelCore mcore = new DbModelCore())
                {
                    string sql = @"SELECT A.id, ISNULL(C.createtime,'1990-01-01') as createtime, ISNULL(C.updatetime,'1990-01-01') as updatetime, A.code AS placecode, A.status,
A.islock, A.isexecute, B.name as placetypename, B.remark as placetyperemark, D.name, D.code AS materialcode, D.description, D.typeremark, D.typename, 
ISNULL(D.status,0) AS materialstatus, A.id as placeid, ISNULL(C.materialid,0) as materialid, D.remark, A.layer, A.col, A.row, A.typeid as placetypeid,
D.thick, D.wide, D.length,D.IssueProjectNo,D.ProcurementProjectNo,D.SerialNo,D.ClassificationSociety,D.CuttingType,D.Version
FROM   dbo.BASEPlace AS A LEFT OUTER JOIN
       dbo.BASEPlaceType AS B ON A.typeid = B.id LEFT OUTER JOIN
       dbo.BASEPlaceMaterial as C on A.id=C.placeid  LEFT OUTER JOIN
       dbo.BASEMaterialView AS D ON C.materialid= D.id where A.typeid=4 or A.typeid=17 order by layer desc,col asc ";
                    psnlst = mcore.Database.SqlQuery<PlaceMaterialViewEntity>(sql).ToList();
                }
                return psnlst;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "GetPlacePreview", ex.Message);
                return psnlst;
            }
        }
 
        /// <summary>
        /// 根据物料信息找取指定的库位物料信息
        /// </summary>
        /// <param name="materialcode"></param>
        /// <returns></returns>
        public string GetPlaceMaterialViews(string materialcode)
        {
            try
            {
                using (DbModelCore mcore = new DbModelCore())
                {
 
                    return mcore.BASEPlaceMaterialViews.FirstOrDefault(x => x.materialcode == materialcode).placecode;
                }
 
            }
            catch (Exception ex)
            {
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "GetPlaceMaterialViews", ex.Message);
                return "";
            }
        }
 
 
        /// <summary>
        /// 根据物料信息列表找取指定的库位物料信息 【Editby shaocx,2025-09-29】
        /// </summary>
        /// <param name="materialcode"></param>
        /// <returns></returns>
        public List<int> GetPlaceMaterialViewsByCodeList(List<string> materialcodeList, out string msg)
        {
            msg = "";
            try
            {
                using (DbModelCore mcore = new DbModelCore())
                {
 
                    var list = mcore.BASEPlaceMaterialViews.Where(x => materialcodeList.Contains(x.materialcode)).ToList();
                    foreach (var item in list)
                    {
                        if (!materialcodeList.Contains(item.materialcode))
                        {
                            msg = $"唯一编码{item.materialcode}没有库存";
                            return null;
                        }
                    }
                    return list.Select(x => x.id).ToList();
                }
 
            }
            catch (Exception ex)
            {
                msg = "异常:" + ex.ToString();
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "GetPlaceMaterialViewsByCodeList", ex.Message);
                return null;
            }
        }
 
    }
}