using CounsellorBL.BLStructure; using CounsellorBL.Helper; using MonumentDefine; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OT.COM.ArsenalDB; using OT.COM.SignalerMessage; using SoldierData.EnterprizeV4; using System; using System.Collections.Generic; using System.Linq; using Util = OT.COM.LogisticsUtil.Util; namespace CounsellorBL.ORD { public class PaymentListService : SingleDataTableTemplate { public PaymentListService() { dgReadCommandPostDataHandler = readCommandPostDataHandler; dgUpdateCommandGenerator = updateCommandGenerator; dgDeleteCommandGenerator = deleteCommandGenerator; } public new CResponseMessage Read(CRequestMessage i_crmInput) => base.Read(i_crmInput); protected string readCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { string sMsg; Command cRes = null; object oResultData = null; try { do { Dictionary dicCondition = GetQueryMasterFirstWhereData(i_crmInput); // 取得condition var lsBranch = ProjectHelper.GetUserGroup(i_crmInput); QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjePurchase = lBlocks.GetInst(); qjePurchase.table = tb_ord_purchase.TABLENAME; qjePurchase.displaycols = new List() { tb_ord_purchase.CN_UID, tb_ord_purchase.CN_AMOUNT, tb_ord_purchase.CN_CREATE_DATE, tb_ord_purchase.CN_MEMBER_UID, tb_ord_purchase.CN_CONFIRMED_PAID, tb_ord_purchase.CN_PURCHASE_NUMBER }; List lsWn = new List(); if (Convert.ToInt32(dicCondition["payee"]) == (int)Enums.PayeeOption.Self) { var user = i_crmInput.customparam[BLWording.SESSION_USER] as tb_sys_session; lsWn.Add(new WhereNode(tb_ord_purchase.CN_CREATE_USER_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_purchase), user.create_user_uid)); } if (dicCondition.ContainsKey(tb_ord_purchase.CN_CONFIRMED_PAID)) { lsWn.Add(new WhereNode(tb_ord_purchase.CN_CONFIRMED_PAID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_purchase), Convert.ToInt32(dicCondition[tb_ord_purchase.CN_CONFIRMED_PAID]))); } if (dicCondition.ContainsKey(tb_ord_purchase.CN_CREATE_DATE + "_start")) { lsWn.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), dicCondition[tb_ord_purchase.CN_CREATE_DATE + "_start"])); } if (dicCondition.ContainsKey(tb_ord_purchase.CN_CREATE_DATE + "_end")) { var endDate = Convert.ToDateTime(dicCondition[tb_ord_purchase.CN_CREATE_DATE + "_end"]); lsWn.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_LT, typeof(tb_ord_purchase), endDate.AddDays(1))); } lsWn.Add(new WhereNode(tb_ord_purchase.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_purchase), (int)Enums.Flag.Enable)); qjePurchase.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lsWn.ToArray()); qjePurchase.ordercols = new List>() { new Tuple(qjePurchase, tb_ord_purchase.CN_CREATE_DATE, BLWording.ORDER_DESC) }; QueryJsonElement qjeEmp = lBlocks.GetInst(); qjeEmp.table = tb_hr_employee.TABLENAME; qjeEmp.jointype = QueryJsonElement.LEFT_JOIN; qjeEmp.jointable = qjePurchase; qjeEmp.joincols = new Dictionary() { { tb_hr_employee.CN_UID,tb_ord_purchase.CN_CREATE_USER_UID }}; qjeEmp.displaycols = new List() { tb_hr_employee.CN_NAME }; qjeEmp.aliascols = new Dictionary> { { tb_hr_employee.CN_NAME, new List() { "payee" } } }; QueryJsonElement qjeMember = lBlocks.GetInst(); qjeMember.table = tb_meb_member.TABLENAME; qjeMember.jointype = QueryJsonElement.LEFT_JOIN; qjeMember.jointable = qjePurchase; qjeMember.joincols = new Dictionary() { { tb_meb_member.CN_UID,tb_ord_purchase.CN_MEMBER_UID }}; if (dicCondition.ContainsKey("member_name")) { qjeMember.wherecols = new WhereNode(tb_meb_member.CN_NAME, WhereNode.EColumnOperation.EOT_LIKE, typeof(tb_meb_member), "%" + dicCondition["member_name"] + "%"); } qjeMember.aliascols = new Dictionary> { { tb_meb_member.CN_NAME, new List() { "member_name" } } }; QueryJsonElement qjeGroup = lBlocks.GetInst(); qjeGroup.table = tb_grp_group.TABLENAME; qjeGroup.jointype = QueryJsonElement.LEFT_JOIN; qjeGroup.jointable = qjeMember; qjeGroup.joincols = new Dictionary() { { tb_grp_group.CN_FB_GROUP_ID,tb_meb_member.CN_GROUP_ID }}; if (dicCondition.ContainsKey(tb_grp_article.CN_GROUP_UID)) { qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_group), dicCondition[tb_grp_article.CN_GROUP_UID]); } else { qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_grp_group), lsBranch.ToArray()); } qjeGroup.aliascols = new Dictionary> { { tb_grp_group.CN_NAME, new List() { "group_name" } } }; lBlocks.Add(qjePurchase); lBlocks.Add(qjeEmp); lBlocks.Add(qjeMember); lBlocks.Add(qjeGroup); sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes); if (sMsg != null) { break; } ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes); QueryDataSet qds; if (dicCondition.ContainsKey("topSearch"))// 取貨清單 > 收款清單, 快速顯示前幾筆結帳資訊 { qds = ai.RunQueryDataSet( cRes, null, 0, Convert.ToInt32(dicCondition["topPayQuantity"])// 顯示筆數 ); } else { qds = ai.RunQueryDataSet(cRes); } if (cRes.IsSuccess == false) { sMsg = cRes.LastErrorCode; break; } oResultData = new QueryResponse(qds); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(readCommandPostDataHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_oReault = oResultData; return sMsg; } protected string deleteCommandGenerator(CRequestMessage i_crmInput, JArray i_jaItems, tb_sys_session i_sSessionUser, out List o_lcResult, List i_saQryContainKeys, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { string sMsg = null; List lcCmds = new List(); try { do { foreach (JToken jtkItem in i_jaItems) { Dictionary dicItem = jtkItem.ToObject>(); sMsg = getManualLog(i_crmInput, dicItem, BLWording.LOG_ACTION_NAME_DELETESQL, out Command cLog); if (sMsg != null) { break; } if (cLog != null) { lcCmds.Add(cLog); } string sMstUID = null; if (dicItem.ContainsKey(BLWording.WHEREDATA) && dicItem[BLWording.WHEREDATA] is JObject wheredata) { Dictionary wheredataDic = wheredata.ToObject>(); if (wheredataDic.ContainsKey(tb_ord_purchase.CN_UID)) { sMstUID = wheredataDic[tb_ord_purchase.CN_UID].ToString(); string memberUID = wheredataDic[tb_ord_purchase.CN_MEMBER_UID].ToString(); // 刪除收款紀錄 lcCmds.Add(Command.SetupUpdateCmd(new tb_ord_purchase() { status_flag = (int)Enums.Flag.Disable }, new tb_ord_purchase() { uid = sMstUID })); // 查詢相關購物金 tb_meb_shopping_points_record cPoints = new tb_meb_shopping_points_record(); cPoints.SetDirty(tb_meb_shopping_points_record.CN_UID, tb_meb_shopping_points_record.CN_AMOUNT); tb_meb_shopping_points_record cConPoints = new tb_meb_shopping_points_record() { purchase_uid = sMstUID, }; Command pointsSelect = Command.SetupSelectCmd(cPoints, cConPoints); ArsenalInterface aiPoints = ArsenalDBMgr.GetInst(pointsSelect); var qdsPoints = aiPoints.RunQueryList(pointsSelect); if (qdsPoints.Count > 0) { List points_uid = new List(); foreach (var points in qdsPoints) { points_uid.Add(points.uid); } lcCmds.Add(Command.SetupDeleteCmd(new WhereNode(tb_meb_shopping_points_record.CN_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_meb_shopping_points_record), points_uid.ToArray()))); } tb_meb_member mPoints = new tb_meb_member(); mPoints.SetDirty(tb_meb_member.CN_SHOPPING_ACCOUNT); tb_meb_member mConPoints = new tb_meb_member() { uid = memberUID }; Command pointsMemberSelect = Command.SetupSelectCmd(mPoints, mConPoints); aiPoints = ArsenalDBMgr.GetInst(pointsMemberSelect); var qdsMebPoints = aiPoints.RunQueryList(pointsMemberSelect); lcCmds.Add(Command.SetupUpdateCmd(new tb_meb_member() { shopping_account = qdsMebPoints[0].shopping_account - Convert.ToInt32(qdsPoints.Sum(x => x.amount)) } , new tb_meb_member() { uid = memberUID })); // 查詢收款紀錄商品 tb_ord_purchase_detail cDetail = new tb_ord_purchase_detail(); cDetail.SetDirty(tb_ord_purchase_detail.CN_QTY, tb_ord_purchase_detail.CN_ORDER_DETAIL_UID, tb_ord_purchase_detail.CN_ARTICLE2PRODUCT_UID, tb_ord_purchase_detail.CN_BRANCH_UID); tb_ord_purchase_detail cDetailCon = new tb_ord_purchase_detail() { purchase_uid = sMstUID }; Command cDetailSelect = Command.SetupSelectCmd(cDetail, cDetailCon); ArsenalInterface ai = ArsenalDBMgr.GetInst(cDetailSelect); List qdsDetail = ai.RunQueryList(cDetailSelect); foreach (var detail in qdsDetail) { if (detail.order_detail_uid != null) { var returnQty = detail.qty; // 查詢訂單 tb_ord_order_detail cOrder = new tb_ord_order_detail(); cOrder.SetDirty(tb_ord_order_detail.CN_UID, tb_ord_order_detail.CN_TAKE_QTY); tb_ord_order_detail cCon = new tb_ord_order_detail() { uid = detail.order_detail_uid }; Command cSelect = Command.SetupSelectCmd(cOrder, cCon); ai = ArsenalDBMgr.GetInst(cSelect); List qdsOrder = ai.RunQueryList(cSelect); // tb_ord_order_detail.status改回未取貨 foreach (var order in qdsOrder) { var qty = returnQty >= order.take_qty ? order.take_qty : returnQty; tb_ord_order_detail upCon = new tb_ord_order_detail() { uid = order.uid }; tb_ord_order_detail upData = new tb_ord_order_detail() { status = (int)Enums.OrderStatus.NotTake, take_qty = order.take_qty - qty }; lcCmds.Add(Command.SetupUpdateCmd(upData, upCon)); returnQty -= qty; } } } } } } } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_crmInput); sMsg = $"{nameof(deleteCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_lcResult = lcCmds; return sMsg; } private string updateCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List o_lcCmds, List i_saQryContainKeys, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { string sMsg = null; List lCmds = new List(); try { do { foreach (JToken joData in i_jaData) { Dictionary dicData = joData.ToObject>(); JObject jwheredata = dicData[BLWording.WHEREDATA] as JObject; JArray jaData = jwheredata[tb_ord_purchase.CN_UID] as JArray; foreach (var data in jaData) { string uid = data.ToString(); // 更新purchase.confirmed_paid tb_ord_purchase upData = new tb_ord_purchase() { confirmed_paid = (int)Enums.Flag.Enable }; tb_ord_purchase upCon = new tb_ord_purchase() { uid = uid }; lCmds.Add(Command.SetupUpdateCmd(upData, upCon)); } } if (sMsg != null) { break; } } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(updateCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_lcCmds = lCmds; return sMsg; } public CResponseMessage GetPurchaseDetail(CRequestMessage i_crmInput) { string sMsg; Command cRes = null; CResponseMessage crmRes = null; try { do { Dictionary dicCondition = GetQueryMasterFirstWhereData(i_crmInput); // 取得condition QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeDetail = lBlocks.GetInst(); qjeDetail.table = tb_ord_purchase_detail.TABLENAME; qjeDetail.displaycols = new List() { tb_ord_purchase_detail.CN_ARTICLE2PRODUCT_UID, tb_ord_purchase_detail.CN_UID, tb_ord_purchase_detail.CN_SPECIFICATION }; qjeDetail.aliascols = new Dictionary> { { QueryJsonElement.SUM(tb_ord_purchase_detail.CN_QTY), new List() { tb_ord_order_detail.CN_ORDER_QTY} } }; qjeDetail.wherecols = new WhereNode(tb_ord_purchase_detail.CN_PURCHASE_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_purchase_detail), dicCondition[tb_ord_purchase.CN_UID]); QueryJsonElement qjeMain = lBlocks.GetInst(); qjeMain.table = tb_ord_purchase.TABLENAME; qjeMain.jointype = QueryJsonElement.LEFT_JOIN; qjeMain.jointable = qjeDetail; qjeMain.joincols = new Dictionary() { { tb_ord_purchase.CN_UID,tb_ord_purchase_detail.CN_PURCHASE_UID }}; qjeMain.displaycols = new List() { tb_ord_purchase.CN_MEMBER_UID }; QueryJsonElement qjeMember = lBlocks.GetInst(); qjeMember.table = tb_meb_member.TABLENAME; qjeMember.jointype = QueryJsonElement.LEFT_JOIN; qjeMember.jointable = qjeMain; qjeMember.joincols = new Dictionary() { { tb_meb_member.CN_UID,tb_ord_purchase.CN_MEMBER_UID }}; qjeMember.displaycols = new List() { tb_meb_member.CN_WPRICE_PAYMENT, }; qjeMember.aliascols = new Dictionary> { {tb_meb_member.CN_NAME , new List() { "member_name"} } }; QueryJsonElement qjeBranch = lBlocks.GetInst(); qjeBranch.table = tb_grp_branch.TABLENAME; qjeBranch.jointype = QueryJsonElement.LEFT_JOIN; qjeBranch.jointable = qjeDetail; qjeBranch.joincols = new Dictionary() { { tb_grp_branch.CN_UID,tb_ord_purchase_detail.CN_BRANCH_UID }}; qjeBranch.displaycols = new List() { tb_grp_branch.CN_BRANCH_NAME }; QueryJsonElement qjeProduct = lBlocks.GetInst(); qjeProduct.table = tb_prd_article2product.TABLENAME; qjeProduct.jointype = QueryJsonElement.LEFT_JOIN; qjeProduct.jointable = qjeDetail; qjeProduct.joincols = new Dictionary() { { tb_prd_article2product.CN_UID,tb_ord_purchase_detail.CN_ARTICLE2PRODUCT_UID }}; qjeProduct.displaycols = new List() { tb_prd_article2product.CN_SEQ, tb_prd_article2product.CN_NAME, tb_prd_article2product.CN_PRICE, tb_prd_article2product.CN_WHOLESALE_PRICE }; QueryJsonElement qjeArticle = lBlocks.GetInst(); qjeArticle.table = tb_grp_article.TABLENAME; qjeArticle.jointype = QueryJsonElement.LEFT_JOIN; qjeArticle.jointable = qjeProduct; qjeArticle.joincols = new Dictionary() { { tb_grp_article.CN_UID,tb_prd_article2product.CN_ARTICLE_UID }}; qjeArticle.displaycols = new List() { tb_grp_article.CN_DUTYFREE, }; QueryJsonElement qjeOrderDetail = lBlocks.GetInst(); qjeOrderDetail.table = tb_ord_order_detail.TABLENAME; qjeOrderDetail.jointype = QueryJsonElement.LEFT_JOIN; qjeOrderDetail.jointable = qjeDetail; qjeOrderDetail.joincols = new Dictionary() { { tb_ord_order_detail.CN_UID,tb_ord_purchase_detail.CN_ORDER_DETAIL_UID }}; QueryJsonElement qjeOrder = lBlocks.GetInst(); qjeOrder.table = tb_ord_order_master.TABLENAME; qjeOrder.jointype = QueryJsonElement.LEFT_JOIN; qjeOrder.jointable = qjeOrderDetail; qjeOrder.joincols = new Dictionary() { { tb_ord_order_master.CN_UID,tb_ord_order_detail.CN_ORDER_UID }}; qjeOrder.aliascols = new Dictionary> { { tb_ord_order_master.CN_NAME, new List() { "order_name" } }, }; qjeDetail.groupcols = new List> { Tuple.Create(qjeDetail, tb_ord_purchase_detail.CN_ARTICLE2PRODUCT_UID), Tuple.Create(qjeDetail, tb_ord_purchase_detail.CN_UID), Tuple.Create(qjeDetail, tb_ord_purchase_detail.CN_BRANCH_UID), Tuple.Create(qjeDetail, tb_ord_purchase_detail.CN_SPECIFICATION), Tuple.Create(qjeMain, tb_ord_purchase.CN_MEMBER_UID), Tuple.Create(qjeMember, tb_meb_member.CN_NAME), Tuple.Create(qjeMember, tb_meb_member.CN_WPRICE_PAYMENT), Tuple.Create(qjeBranch, tb_grp_branch.CN_BRANCH_NAME), Tuple.Create(qjeProduct, tb_prd_article2product.CN_PRICE), Tuple.Create(qjeProduct, tb_prd_article2product.CN_WHOLESALE_PRICE), Tuple.Create(qjeProduct, tb_prd_article2product.CN_SEQ), Tuple.Create(qjeProduct, tb_prd_article2product.CN_NAME), Tuple.Create(qjeOrder, tb_ord_order_master.CN_NAME), Tuple.Create(qjeArticle, tb_grp_article.CN_DUTYFREE), }; lBlocks.Add(qjeDetail); lBlocks.Add(qjeMain); lBlocks.Add(qjeMember); lBlocks.Add(qjeBranch); lBlocks.Add(qjeProduct); lBlocks.Add(qjeArticle); lBlocks.Add(qjeOrderDetail); lBlocks.Add(qjeOrder); sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes); if (sMsg != null) { break; } ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes); QueryDataSet qds = ai.RunQueryDataSet(cRes); if (cRes.IsSuccess == false) { sMsg = cRes.LastErrorCode; break; } // 判斷是否使用購物金 QueryJsonElementCollection lBlocks2 = new QueryJsonElementCollection(); QueryJsonElement qjePoints = lBlocks2.GetInst(); qjePoints.table = tb_meb_shopping_points_record.TABLENAME; qjePoints.displaycols = new List() { tb_meb_shopping_points_record.CN_AMOUNT }; List lsWn = new List(); lsWn.Add(new WhereNode(tb_meb_shopping_points_record.CN_AMOUNT, WhereNode.EColumnOperation.EOT_LT, typeof(tb_meb_shopping_points_record), 0)); lsWn.Add(new WhereNode(tb_meb_shopping_points_record.CN_PURCHASE_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_meb_shopping_points_record), dicCondition[tb_ord_purchase.CN_UID])); qjePoints.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lsWn.ToArray()); lBlocks2.Add(qjePoints); sMsg = MakeSelectJoinByBlocks(lBlocks2, out cRes); ArsenalInterface aiPoints = ArsenalDBMgr.GetInst(cRes); QueryDataSet qdsPoints = aiPoints.RunQueryDataSet(cRes); if (cRes.IsSuccess == false) { sMsg = cRes.LastErrorCode; break; } crmRes = new CSuccessResponseMessage(null, i_crmInput); crmRes.param.Add(BLWording.DATA, qds); crmRes.param.Add("points", qdsPoints); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex)); sMsg = $"{nameof(GetPurchaseDetail)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. "; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } if (null != sMsg) { crmRes = new CErrorResponseMessage(sMsg, i_crmInput); } return crmRes; } } }