1
yusijie
2022-12-13 4b547a5e8d2eca13da5e957b7a8c3feeb3febab8
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using System.ComponentModel;
using Kingdee.BOS.ServiceHelper;
using System.Data;
using Newtonsoft.Json.Linq;
using ZD.Cloud.WebApi;
using Newtonsoft.Json;
using Kingdee.BOS.Core.DynamicForm;
using System.Linq;
using RestSharp;
using zymes_Project.Utility;
 
namespace zymes_Project.OperationService
{
    [Description("[生产汇报单-审核]服务插件]")]
    [HotUpdate]
    public class zymes_RealessPrdMoReport : AbstractOperationServicePlugIn
    {
        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        public override void OnPrepareOperationServiceOption(OnPrepareOperationServiceEventArgs e)
        {
            base.OnPrepareOperationServiceOption(e);
 
            //为了在BeginOperationTransaction里检查数据抛出异常时
            //只回滚当前单据的事务,这里设置为不支持批量事务,这样BOS会
            //循环为每一张单据创建事务调用操作
            e.SupportTransaction = true;
            e.SurportBatchTransaction = false;
        }
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("FEntity");
            e.FieldKeys.Add("FEntity_FEntryID");
        }
        //public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
        //{
        //    base.BeginOperationTransaction(e);
        //    //foreach (var item in e.DataEntitys)
        //    //{
        //    //    DynamicObjectCollection entryRows = item["FEntity"] as DynamicObjectCollection;
        //    //}
        //    Entity entity = this.BusinessInfo.GetEntity("FEntity");//获取明细信息
        //    foreach (var billObj in e.DataEntitys)
        //    {
        //        //DynamicObjectCollection entryRows = entity.DynamicProperty.GetValue(billObj.DataEntity)
        //        //        as DynamicObjectCollection;
        //        DynamicObjectCollection entryRows = billObj["FEntity"] as DynamicObjectCollection;
        //        foreach (var entryRow in entryRows)
        //        {
        //            var fentryId = entryRow["Id"].ToString();
        //            //生成生产汇报单
        //            string sql = $"/*dialect*/ exec proc_jit_DayPlanAduitAddNemMoOrder {fentryId}";
        //            DataTable dt = DBServiceHelper.ExecuteDataSet(Context, sql).Tables[0];
        //            if (dt.Rows[0][0].ToString().Contains("成功"))
        //                continue;
        //            foreach (DataRow dr in dt.Rows)
        //            {
        //                JObject model = new JObject();
        //                model.Add("FBILLTYPEID", new JObject() { ["Fnumber"] = dr["FBillTypeNumber"].ToString() }); //单据类型
        //                model.Add("FCreatorId", new JObject() { ["FUserID"] = Context.UserId }); //创建人
        //                model.Add("FModifierId", new JObject() { ["FUserID"] = Context.UserId }); //修改人
        //                model.Add("FDate", dr["FDate"].ToString());
        //                model.Add("FBILLNO", dr["FSpreadBillNo"].ToString());
        //                model.Add("FPrdOrgId", new JObject() { ["Fnumber"] = dr["FPrdOrgNumber"].ToString() });
        //                model.Add("FWorkShopID0", new JObject() { ["Fnumber"] = dr["FWorkShopNumber"].ToString() });
        //                model.Add("FOwnerTypeId", dr["FOwnerTypeId"].ToString());
        //                model.Add("FIsRework", dr["FIsRework"].ToString());
        //                model.Add("FBusinessType", dr["FBusinessType"].ToString());
        //                model.Add("FTrustteed", dr["FTrustteed"].ToString());
        //                model.Add("FIsEntrust", dr["FIsEntrust"].ToString());
        //                model.Add("FPPBOMType", dr["FPPBOMType"].ToString());
        //                model.Add("FIssueMtrl", dr["FIssueMtrl"].ToString());
        //                JArray Fentity = new JArray();
        //                JObject FentityModel = new JObject();
        //                FentityModel.Add("FProductType", dr["FProductType"].ToString());
        //                FentityModel.Add("FMaterialId", new JObject() { ["Fnumber"] = dr["FMATERIALNUMBER"].ToString() });
        //                FentityModel.Add("FWorkShopID", new JObject() { ["Fnumber"] = dr["FWorkShopNumber"].ToString() });
        //                FentityModel.Add("FUnitId", new JObject() { ["Fnumber"] = dr["FUnitNumber"].ToString() });
        //                FentityModel.Add("FQty", dr["FQty"].ToString());
        //                FentityModel.Add("FYieldQty", dr["FYieldQty"].ToString());
        //                FentityModel.Add("FPlanStartDate", dr["FPlanStartDate"].ToString());
        //                FentityModel.Add("FPlanFinishDate", dr["FPlanFinishDate"].ToString());
        //                FentityModel.Add("FRequestOrgId", new JObject() { ["Fnumber"] = dr["FRequestOrgNumber"].ToString() });
        //                FentityModel.Add("FBomId", new JObject() { ["Fnumber"] = dr["FBomNumber"].ToString() });
        //                FentityModel.Add("FISBACKFLUSH", dr["FISBACKFLUSH"].ToString());
        //                FentityModel.Add("FStockInOrgId", new JObject() { ["Fnumber"] = dr["FPrdOrgNumber"].ToString() });
        //                FentityModel.Add("FBaseYieldQty", dr["FBaseYieldQty"].ToString());
        //                FentityModel.Add("FReqType", dr["FReqType"].ToString());
        //                FentityModel.Add("FInStockOwnerTypeId", dr["FInStockOwnerTypeId"].ToString());
        //                FentityModel.Add("FBaseStockInLimitH", dr["FBaseStockInLimitH"].ToString());
        //                FentityModel.Add("FInStockOwnerId", new JObject() { ["Fnumber"] = dr["FPrdOrgNumber"].ToString() });
        //                FentityModel.Add("FCheckProduct", dr["FCheckProduct"].ToString());
        //                FentityModel.Add("FBaseStockInLimitL", dr["FBaseStockInLimitL"].ToString());
        //                FentityModel.Add("FBaseUnitQty", dr["FBaseUnitQty"].ToString());
        //                FentityModel.Add("FRoutingId", new JObject() { ["Fnumber"] = dr["FRoutingNumber"].ToString() });
        //                FentityModel.Add("FBaseUnitId", new JObject() { ["Fnumber"] = dr["FUnitNumber"].ToString() });
        //                FentityModel.Add("FStockId", new JObject() { ["Fnumber"] = dr["FStockNumber"].ToString() });
        //                FentityModel.Add("FStockInLimitH", dr["FStockInLimitH"].ToString());
        //                FentityModel.Add("FStockInLimitL", dr["FStockInLimitL"].ToString());
        //                FentityModel.Add("FCostRate", dr["FCostRate"].ToString());
        //                FentityModel.Add("FCreateType", dr["FCreateType"].ToString());
        //                FentityModel.Add("FYieldRate", dr["FYieldRate"].ToString());
        //                FentityModel.Add("FGroup", dr["FGroup"].ToString());
        //                FentityModel.Add("FNoStockInQty", dr["FNoStockInQty"].ToString());
        //                FentityModel.Add("FBaseNoStockInQty", dr["FBaseNoStockInQty"].ToString());
        //                FentityModel.Add("FRowId", dr["FRowId"].ToString());
        //                FentityModel.Add("FPickMtrlStatus", dr["FPickMtrlStatus"].ToString());
        //                FentityModel.Add("FMOChangeFlag", dr["FMOChangeFlag"].ToString());
        //                FentityModel.Add("FDAYPLANBILLNO", dr["FDAYPLANBILLNO"].ToString());
        //                FentityModel.Add("FDayPlanFseq", dr["FDayPlanFseq"].ToString());
        //                FentityModel.Add("FDayPlanFentryId", dr["FDayPlanFentryId"].ToString());
        //                FentityModel.Add("FDayPlanFid", dr["FDayPlanFid"].ToString());
        //                FentityModel.Add("FDayPlanRemainQty", 0);
        //                FentityModel.Add("FDayPlanQty", dr["FQty"].ToString());
        //                FentityModel.Add("F_TEZV_Base", new JObject() { ["Fnumber"] = dr["FPREBDONENUMBER"].ToString() });//包装标识
        //                FentityModel.Add("F_bsv_Text1", dr["F_bsv_Text1"].ToString());//源单客户代码
        //                FentityModel.Add("F_bsv_Date", dr["F_bsv_Date"].ToString());//要求交期
        //                FentityModel.Add("F_bsv_KH", new JObject() { ["Fnumber"] = dr["FCUSTOMERNUMBER"].ToString() });//客户
        //                //////////////生产汇报单源单信息////////////////////
        //                //FSRCBILLTYPE,FSRCBILLID,FSRCBILLNO,FSRCBILLENTRYID,FSRCBILLENTRYSEQ,FSALEORDERID,FSALEORDERNO,FSALEORDERENTRYID,FSALEORDERENTRYSEQ
        //                FentityModel.Add("FSRCBILLTYPE", dr["FSRCBILLTYPE"].ToString());
        //                FentityModel.Add("FSRCBILLID", dr["FSRCBILLID"].ToString());
        //                FentityModel.Add("FSRCBILLNO", dr["FSRCBILLNO"].ToString());
        //                FentityModel.Add("FSRCBILLENTRYID", dr["FSRCBILLENTRYID"].ToString());
        //                FentityModel.Add("FSRCBILLENTRYSEQ", dr["FSRCBILLENTRYSEQ"].ToString());
        //                FentityModel.Add("FSALEORDERID", dr["FSALEORDERID"].ToString());
        //                FentityModel.Add("FSALEORDERNO", dr["FSALEORDERNO"].ToString());
        //                FentityModel.Add("FSALEORDERENTRYID", dr["FSALEORDERENTRYID"].ToString());
        //                FentityModel.Add("FSALEORDERENTRYSEQ", dr["FSALEORDERENTRYSEQ"].ToString());
        //                FentityModel.Add("FReqSrc", dr["FReqSrc"].ToString());//需求来源 1销售订单
        //                if (dr["FSRCBILLTYPE"].ToString() == "PLN_PLANORDER")
        //                {
        //                    JArray _Fentity2 = new JArray();
        //                    JObject _FentityModel2 = new JObject();
        //                    _FentityModel2.Add("FTREEENTITY_Link_FFlowId", "");//业务流程图:FTREEENTITY_Link_FFlowId
        //                    _FentityModel2.Add("FTREEENTITY_Link_FFlowLineId", "0");//推进路线:FTREEENTITY_Link_FFlowLineId
        //                    _FentityModel2.Add("FTREEENTITY_Link_FRuleId", "PlanOrder_MO");//转换规则:FTREEENTITY_Link_FRuleId
        //                    _FentityModel2.Add("FTREEENTITY_Link_FSTableId", "0");//源单表内码:FTREEENTITY_Link_FSTableId
        //                    _FentityModel2.Add("FTREEENTITY_Link_FSTableName", "T_PLN_PLANORDER");//源单表:FTREEENTITY_Link_FSTableName
        //                    _FentityModel2.Add("FTREEENTITY_Link_FSBillId", dr["FSBILLID"].ToString());//源单内码:FTREEENTITY_Link_FSBillId
        //                    _FentityModel2.Add("FTREEENTITY_Link_FSId", dr["FSID"].ToString()); //源单分录内码:FTREEENTITY_Link_FSId
        //                    _FentityModel2.Add("FTREEENTITY_Link_FBaseUnitQtyOld", dr["FQty"].ToString());//原始携带量:FTREEENTITY_Link_FBaseUnitQtyOld
        //                    _FentityModel2.Add("FTREEENTITY_Link_FBaseUnitQty", dr["FQty"].ToString());//修改携带量:FTREEENTITY_Link_FBaseUnitQty
        //                    _Fentity2.Add(_FentityModel2);
        //                    FentityModel.Add("FTREEENTITY_Link", _Fentity2);
        //                }
        //                //////////////////////////////////////////////////
        //                Fentity.Add(FentityModel);
        //                model.Add("FTreeEntity", Fentity);
        //                JObject jsonRoot = new JObject()
        //                {
        //                    ["Creator"] = "",
        //                    ["NeedUpDateFields"] = new JArray(),
        //                    ["NeedReturnFields"] = new JArray(),
        //                    ["IsDeleteEntry"] = "false",
        //                    ["SubSystemId"] = "",
        //                    ["IsVerifyBaseDataField"] = "false",
        //                    ["Model"] = model
        //                };
        //                var _result = InvokeHelper.Save("PRD_MO", jsonRoot.ToString());
        //                var _saveObj = JObject.Parse(_result);
        //                var saveIsSuc = _saveObj["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper();
        //                if (saveIsSuc != "TRUE")
        //                {
        //                    e.CancelOperation = true;
        //                    dictionary.Add(dr["FDAYPLANBILLNO"].ToString(), "生产汇报单生成失败," + _saveObj["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToString());
        //                    LogService.Write("生产汇报单生成失败json" + jsonRoot.ToString());
        //                    //this.View.ShowErrMessage("生产汇报单生成失败," + _saveObj["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToString());
        //                    continue;
        //                }
        //            }
        //        }
        //    }
        //}
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            Entity entity = this.BusinessInfo.GetEntity("FEntity");//获取明细信息
            foreach (var billObj in e.DataEntitys)
            {
                var sql = $"exec zymes_RealessPrdMoReport {billObj["Id"]} ";
                DataSet ds = DBServiceHelper.ExecuteDataSet(Context, sql);
                DataTable maindt = ds.Tables[1];
                DataTable subdt = ds.Tables[0];
                if (maindt.Rows.Count == 0 || subdt.Rows.Count == 0)
                {
                    LogService.Write("生产汇报单同步异常:" + sql);
                    continue;
                }
                JArray array = new JArray();//明细表
                foreach (DataRow dr in subdt.Rows)
                {
                    JObject fentrymodel = new JObject();
                    foreach (DataColumn column in subdt.Columns)
                    {
                        fentrymodel.Add(column.ColumnName, dr[column.ColumnName].ToString());
                    }
                    array.Add(fentrymodel);
                }
                JObject model = new JObject();//主表
                DataRow maindr = maindt.Rows[0];
                foreach (DataColumn column in maindt.Columns)
                {
                    model.Add(column.ColumnName, maindr[column.ColumnName].ToString());
                }
                model.Add("HENTRY", array);
                JObject jsonRoot = new JObject()
                {
                    ["model"] = model
                };
                //LogService.Write("生产汇报单同步示例" + JsonConvert.SerializeObject(jsonRoot));
                //string clientUrl = Util.GetConfigKey(AppDomain.CurrentDomain.BaseDirectory + "/Config/kdapi.config", "ClientUrl");//K/3 Cloud 业务站点地址
                var client = new RestSharp.RestClient("http://61.130.182.102:18181/simoerAPI/");
                //新增
                var requestPost = new RestRequest("Sc_ICMOReportBill/Sc_ICMOReportBillSaveApi", Method.POST);
                requestPost.AddParameter("application/json", JsonConvert.SerializeObject(jsonRoot), ParameterType.RequestBody);
                IRestResponse responsePost = client.Execute(requestPost);
                var contentPost = responsePost.Content;
                if (!contentPost.Contains("成功"))
                {
                    LogService.Write("生产汇报单同步异常,POST方式获取结果," + billObj["Id"] + "," + contentPost);
                    LogService.Write("生产汇报单同步异常,POST," +   "Sc_ICMOReportBill/Sc_ICMOReportBillSaveApi");
                    LogService.Write("生产汇报单同步异常,JsonConvert.SerializeObject(jsonRoot)," + JsonConvert.SerializeObject(jsonRoot));
                }
            };
            foreach (var item in dictionary)
            {
                ResultMessage.dataError(this.OperationResult.OperateResult, item.Key, item.Value);
            }
        }
        public static class ResultMessage
        {/// <summary>
         /// 修改提示信息
         /// </summary>
         /// <param name="operateResult"></param>
         /// <param name="billno"></param>
            public static void dataError(OperateResultCollection operateResult, string billno, string message)
            {
                OperateResult operate = operateResult.Where(n => n.Number == billno).First();
                operate.Message = message;
                operate.SuccessStatus = false;
                operate.MessageType = MessageType.FatalError;
            }
        }
    }
}