jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using HH.WMS.CoreServer.DAL;
using HH.WMS.Entitys;
using HH.WMS.Entitys.Algorithm;
using HH.WMS.Entitys.Basic;
using HH.WMS.Entitys.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WMS.CoreServer.BLL
{
    public class OutDataBLL : BaseBLL
    {
        public DataTable GetOutMstData(string tableName)
        {
            return CreateDAL<OutDataDAL>().GetOutMstData(tableName);
        }
 
        public DataTable GetOutDtlData(string collectionName, string cCode)
        {
            return CreateDAL<OutDataDAL>().GetOutDtlData(collectionName, cCode);
        }
 
        public bool AddRdrecord(DataTable RdrecordMstEntityTable, string dtlTableName, string opType)
        {
            //主表集合
            List<TN_WM_OUT_MSTEntity> outMstEntityList = new List<TN_WM_OUT_MSTEntity>();
 
            string stockStr = WebApiManager.HttpWMS_Get("api/Common/NoTokenStockList");
            OperateResult or = JsonConvert.DeserializeObject<OperateResult>(stockStr);
            List<TN_AB_STOCKEntity> stockList = JsonConvert.DeserializeObject<List<TN_AB_STOCKEntity>>(or.Data.ToString());
 
            for (int i = 0; i < RdrecordMstEntityTable.Rows.Count; i++)
            {
                #region 拼接出库单主表
                string postDataOpNo = "{\"appCode\":\"WMS\",\"ruleName\":\"" + Constants.Rule_OutOrder + "\",\"orgId\":\"\",\"orgFlag\":\"0\"}";
                string opNo = WebApiManager.HttpAutoBom_Post("Api/BillRule/GenBillNo", postDataOpNo);
 
                if (opNo == "")
                {
                    Log.Info("生成出库单号失败!", "AutoBom接口GenBillNo未调通!");
                    return false;
                }
                TN_WM_OUT_MSTEntity outMstEntity = new TN_WM_OUT_MSTEntity();
                outMstEntity.CN_GUID = System.Guid.NewGuid().ToString().ToUpper();
                outMstEntity.CN_S_OP_NO = opNo;
                //outMstEntity.CN_T_OPERATE = Convert.ToDateTime(RdrecordMstEntityTable.Rows[i]["dDate"].ToString());
                if (dtlTableName == "Rdrecords32")
                {
                    outMstEntity.CN_S_CUSTOMER = RdrecordMstEntityTable.Rows[i]["cCusCode"].ToString();
                    outMstEntity.CN_S_CUSTOMER_NAME = RdrecordMstEntityTable.Rows[i]["cCusName"].ToString();
                    outMstEntity.CN_S_DETAIL_ADDRESS = RdrecordMstEntityTable.Rows[i]["cShipAddress"].ToString();
                }
                else
                {
                    outMstEntity.CN_S_CUSTOMER = "";
                    outMstEntity.CN_S_CUSTOMER_NAME = "";
                }
                outMstEntity.CN_S_STOCK_CODE = stockList[0].CN_S_STOCK_CODE; // RdrecordMstEntityTable.Rows[i]["cWhCode"].ToString();
                outMstEntity.CN_S_FROM_NO = RdrecordMstEntityTable.Rows[i]["MainId"].ToString();
                outMstEntity.CN_S_AUDITOR = RdrecordMstEntityTable.Rows[i]["cChecker_Erp"].ToString();
                outMstEntity.CN_T_AUDIT_TIME = string.IsNullOrEmpty(RdrecordMstEntityTable.Rows[i]["dCheckDate_Erp"].ToString()) ? DateTime.Now : Convert.ToDateTime(RdrecordMstEntityTable.Rows[i]["dCheckDate_Erp"].ToString());
                outMstEntity.CN_T_CREATE = Convert.ToDateTime(RdrecordMstEntityTable.Rows[i]["dCreateDate"].ToString());
 
                //未传递     默认值
                outMstEntity.CN_T_OP_DATE = DateTime.Now;
                outMstEntity.CN_S_STATE = Constants.State_Audit;
                outMstEntity.CN_S_OWNER = Constants.DefaultU8Owner;
                outMstEntity.CN_S_OP_TYPE = opType;
                outMstEntity.CN_S_OP_FROM = "U8";
                outMstEntity.CN_S_FROM_NO = RdrecordMstEntityTable.Rows[i]["cCode"].ToString();
                outMstEntity.CN_S_AUDIT_REMARK = "";
                outMstEntity.CN_S_OPERATOR = "汉和信息";
                outMstEntity.CN_S_CREATOR = "hh";
                outMstEntity.CN_S_CREATOR_BY = "汉和信息";
                outMstEntity.CN_S_MODIFY = "hh";
                outMstEntity.CN_S_MODIFY_BY = "汉和信息";
                outMstEntity.CN_T_MODIFY = DateTime.Now;
                outMstEntity.CN_S_NOTE = "U8对接导入";
                #endregion
 
                #region 拼接出库单子表
                //子表集合
                List<TN_WM_OUT_DTLEntity> outDtlEntityList = new List<TN_WM_OUT_DTLEntity>();
 
                DataTable RdrecordDtlEntityTable = BLLCreator.Create<OutDataBLL>().GetOutDtlData(dtlTableName, outMstEntity.CN_S_FROM_NO);
                for (int j = 0; j < RdrecordDtlEntityTable.Rows.Count; j++)
                {
                    AutoBomItemEntity itemenity = BLLCreator.Create<MongoBLL>().GetItemEntity(RdrecordDtlEntityTable.Rows[j]["cInvCode"].ToString()); ;
                    if (itemenity == null)
                    {
                        Log.Info("拼接子表失败!", "物料编码 " + RdrecordDtlEntityTable.Rows[j]["cInvCode"].ToString() + " 不存在或者未定版!");
                        return false;
                    }
 
                    TN_WM_OUT_DTLEntity dtlEntity = new TN_WM_OUT_DTLEntity();
                    dtlEntity.CN_GUID = System.Guid.NewGuid().ToString().ToUpper();
                    dtlEntity.CN_T_PRODUCTION = Convert.ToDateTime(RdrecordDtlEntityTable.Rows[j]["dMadeDate"].ToString());
                    dtlEntity.CN_T_MODIFY = DateTime.Now;
                    dtlEntity.CN_T_CREATE = DateTime.Now;
                    dtlEntity.CN_S_STATE = Constants.State_Audit;
                    dtlEntity.CN_S_SERIAL_NO = "";
                    dtlEntity.CN_S_PRODUCTION_BATCH = RdrecordDtlEntityTable.Rows[j]["cBatch"].ToString();
                    dtlEntity.CN_S_OP_NO = outMstEntity.CN_S_OP_NO;
                    dtlEntity.CN_S_MSTGUID = outMstEntity.CN_GUID;
                    dtlEntity.CN_S_MODEL = itemenity.CN_S_MODEL;
                    dtlEntity.CN_S_ITEM_NAME = itemenity.CN_S_ITEM_NAME;
                    dtlEntity.CN_S_ITEM_CODE = itemenity.CN_S_ITEM_CODE;
                    dtlEntity.CN_N_ROW_NO = Convert.ToInt32(RdrecordDtlEntityTable.Rows[j]["iRowNo"].ToString());
 
 
                    string unit = RdrecordDtlEntityTable.Rows[j]["cComUnitName"].ToString();
                    decimal qty = RdrecordDtlEntityTable.Rows[j]["iQuantity"].ToString() == null ? 0 : Convert.ToDecimal(RdrecordDtlEntityTable.Rows[j]["iQuantity"].ToString());
 
                    if (unit != itemenity.CN_S_MEASURE_UNIT)
                    {
                        if (string.IsNullOrEmpty(itemenity.CN_S_CONERSION))
                        {
                            Log.Info("拼接子表失败!", "请维护物料 " + itemenity.CN_S_ITEM_CODE + " 的换算比例!");
                            return false;
                        }
 
                        unit = itemenity.CN_S_MEASURE_UNIT;
                        decimal jishu = Convert.ToDecimal(itemenity.CN_S_CONERSION.Substring(0, itemenity.CN_S_CONERSION.IndexOf("-")));
                        if (jishu == 0) jishu = 1;
                        dtlEntity.CN_F_QUANTITY = Math.Floor(qty / jishu);
                    }
                    else
                    {
                        dtlEntity.CN_F_QUANTITY = qty;
                    }
 
                    dtlEntity.CN_S_MEASURE_UNIT = unit;
                    dtlEntity.CN_S_ITEM_STATE = Constants.ItemState_WaitCheck;
 
                    dtlEntity.CN_S_CREATOR = "hh";
                    dtlEntity.CN_S_CREATOR_BY = "汉和信息";
                    dtlEntity.CN_S_MODIFY = "hh";
                    dtlEntity.CN_S_MODIFY_BY = "汉和信息";
                    dtlEntity.CN_T_MODIFY = DateTime.Now;
                    dtlEntity.CN_S_FIGURE_NO = "";
 
                    dtlEntity.CN_S_NOTE = "U8对接导入";
 
                    outDtlEntityList.Add(dtlEntity);
                }
                outMstEntity.OutDtlList = outDtlEntityList;
 
                outMstEntityList.Add(outMstEntity);
 
                #endregion
            }
 
            string resultStrMst = WebApiManager.HttpWMS_Post("Api/Out/AddU8OutData", JsonConvert.SerializeObject(outMstEntityList));
            if (resultStrMst == "")
            {
                Log.Info("保存主子表调通!", "Api/Out/AddU8OutData");
                return false;
            }
            OperateResult orMst = JsonConvert.DeserializeObject<OperateResult>(resultStrMst);
            if (!orMst.Success)
            {
                Log.Info("保存主子表接口调用失败!", orMst.Msg);
                return false;
            }
            return orMst.Success;
        }
 
        public SqlExecuteResult UpdateRdrecordCheckState(DataTable RdrecordMstEntityTable, string collectionName)
        {
            return CreateDAL<OutDataDAL>().UpdateRdrecordCheckState(RdrecordMstEntityTable, collectionName);
        }
 
        public bool UpdateRdrecord(DataTable RdrecordMstEntityTable, string collectionName)
        {
            List<U8_TransVouchEntity> transVouchList = new List<U8_TransVouchEntity>();
 
            for (int i = 0; i < RdrecordMstEntityTable.Rows.Count; i++)
            {
                string cCode = RdrecordMstEntityTable.Rows[i]["cCode"].ToString();
                DataTable RdrecordDtlEntityTable = BLLCreator.Create<OutDataBLL>().GetOutDtlData(collectionName, cCode);
 
                for (int j = 0; j < RdrecordDtlEntityTable.Rows.Count; j++)
                {
                    U8_TransVouchEntity transVouchEntity = new U8_TransVouchEntity();
 
                    string itemCode = RdrecordDtlEntityTable.Rows[i]["cInvCode"].ToString();
                    string productBatch = RdrecordDtlEntityTable.Rows[i]["cBatch"].ToString();
 
                    transVouchEntity.CN_S_ITEM_CODE = itemCode;
                    transVouchEntity.CN_S_PRODUCTION_BATCH = productBatch;
                    transVouchEntity.CN_S_BATCH_NO = productBatch;
                    transVouchEntity.CN_S_ITEM_STATE = Constants.ItemState_Qualified;
 
                    transVouchList.Add(transVouchEntity);
                }
            }
 
            string result = WebApiManager.HttpWMS_Post("Api/In/UpdateTransVouchItemState", JsonConvert.SerializeObject(transVouchList));
            OperateResult opResult = JsonConvert.DeserializeObject<OperateResult>(result);
            return opResult.Success;
        }
 
        public SqlExecuteResult UpdateRdrecordCheckFlag(string opNo, string tableName)
        {
            return CreateDAL<OutDataDAL>().UpdateRdrecordCheckFlag(opNo, tableName);
        }
    }
}