【Basic Code】一个被我写烂的坏味道,引以为戒

2023-11-04 22:20
文章标签 code basic 味道 引以为戒

本文主要是介绍【Basic Code】一个被我写烂的坏味道,引以为戒,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        今天被组长训了,因为这个坏味道,封装啊!!!

public static List<LeKeCardInfo> QueryStoreChange(LeKeQueryReq PayREQ){//存储最终的数据List<LeKeCardInfo> resultList = new List<LeKeCardInfo>();List<LeKeCardInfo> resultNull = new List<LeKeCardInfo>();List<D.Consume> consumelist = new List<Consume>();var where = new Where<D.LeaguerEvent>();var where1 = new Where<D.Consume>();var where2 = new Where<D.ExperienceRecord>();var where3 = new Where<D.TemporaryEvent>();where.And(a => a.Createdatetime >= PayREQ.StartTime && a.Createdatetime <= PayREQ.EndTime && a.Event != (int)OperateEvent.RemakeCard && a.Event != (int)OperateEvent.ChangeCard);where1.And(a => a.Createdatetime >= PayREQ.StartTime && a.Createdatetime <= PayREQ.EndTime && a.IsSuccess == 1);where3.And(a => a.Createdatetime >= PayREQ.StartTime && a.Createdatetime <= PayREQ.EndTime && a.Event != (int)OperateEvent.RemakeCard && a.Event != (int)OperateEvent.ChangeCard);//终端ID不为空if (PayREQ.TerminalId > 0){string terminalname = DbCard.GetTerminalName(PayREQ.TerminalId);where2.And(a => a.LeaguerID == terminalname);where.And(a => a.TerminalId == PayREQ.TerminalId);where.And(a => a.TerminalId == PayREQ.TerminalId);}//操作员不为空if (PayREQ.OperatorId > 0)where.And(a => a.Operator == PayREQ.OperatorId);else{where2.And(a => a.EndTime >= PayREQ.StartTime && a.EndTime <= PayREQ.EndTime);if (!string.IsNullOrEmpty(PayREQ.CardNum))where2.And(a => a.LeaguerID == PayREQ.CardNum);//找出体验表中符合条件的数据List<D.ExperienceRecord> ExList = MySql.From<D.ExperienceRecord>().Where(where2).ToList();var conslist = MySql.From<D.Consume>().Where(where1).ToList();foreach (var e in ExList){//找出消费表中体验id为ExList中的数据var consume = conslist.Find(a => a.ExperienceId == e.Id);if (consume != null)consumelist.Add(consume);}//级别和名称作为筛选条件,应对异常条件筛选 - 2016年9月27日22:34:19if (PayREQ.LeaguerLevelId > 0){var test = MySql.From<D.LeaguerEvent>().Where(b => b.LeaguerLevelId == PayREQ.LeaguerLevelId).ToList();if (test == null || test.Count == 0){return resultNull;}}}var leaguereventlist = MySql.From<D.LeaguerEvent>().Where(where).ToList();var temporaryEventList = MySql.From<D.TemporaryEvent>().Where(where3).ToList();//卡号不为空if (!string.IsNullOrEmpty(PayREQ.CardNum)){//验证卡号异常输入 - 2016年9月27日22:36:04var test = MySql.From<D.Card>().Where(a => a.CardNo == PayREQ.CardNum).ToList();if (test == null || test.Count == 0){return resultNull;}var card = DbCard.GetCardInfo(PayREQ.CardNum);if (card == null)return resultNull;List<int> id = new List<int>();if (card.Type == (int)CardType.LeaguerCard){temporaryEventList.Clear();var leag = MySql.From<D.LeaguerCard>().Where(a => a.CardId == card.Id).ToList();if (leag == null || leag.Count == 0)return resultNull;var leaguerId = from fn in leagselect fn.Id;id = leaguerId.ToList();var leaguerEventSelect = leaguereventlist.Where(p => id.Contains(p.LeaguerId)).ToList();leaguereventlist.Clear();leaguereventlist = leaguerEventSelect;}if (card.Type == (int)CardType.TemporaryCard){leaguereventlist.Clear();var temp = MySql.From<D.TemporaryCard>().Where(a => a.CardId == card.Id).ToList();if (temp == null || temp.Count == 0)return resultNull;var tempId = from fn in tempselect fn.Id;id = tempId.ToList();var tempEventSelect = temporaryEventList.Where(p => id.Contains(p.TemporaryCardId)).ToList();temporaryEventList.Clear();temporaryEventList = tempEventSelect;}           //还有 consumelistvar consumelistSelect = consumelist.Where(p => id.Contains(p.ChildCardId)).ToList();consumelist.Clear();consumelist = consumelistSelect;}if (!string.IsNullOrEmpty(PayREQ.LeaguerName)){temporaryEventList.Clear();var leag = MySql.From<D.LeaguerCard>().Where(a => a.UserName == PayREQ.LeaguerName).ToList();if (leag == null || leag.Count == 0)return resultNull;var leaguerId = from fn in leagselect fn.Id;List<int> id = leaguerId.ToList();var leaguerEventSelect = leaguereventlist.Where(p => id.Contains(p.LeaguerId)).ToList();leaguereventlist.Clear();leaguereventlist = leaguerEventSelect;//还有 consumelistvar consumelistSelect = consumelist.Where(p => id.Contains(p.ChildCardId)).ToList();consumelist.Clear();consumelist = consumelistSelect;}if (!string.IsNullOrEmpty(PayREQ.MenuName)){temporaryEventList.Clear();var menu = MySql.From<D.Menu>().Where(a => a.Name == PayREQ.MenuName).ToList();if (menu == null || menu.Count == 0)return resultNull;var menuId = from fn in menuselect fn.Id;var leaguerEventSelect = leaguereventlist.Where(p => menuId.Contains(p.MenuId)).ToList();leaguereventlist.Clear();leaguereventlist = leaguerEventSelect;}if (PayREQ.LeaguerLevelId > 0){temporaryEventList.Clear();var leaguerEventSelect = leaguereventlist.Where(a => a.LeaguerLevelId == PayREQ.LeaguerLevelId).ToList();leaguereventlist.Clear();leaguereventlist = leaguerEventSelect;//找出通过卡号、会员名称限制的开卡记录var consumelistSelect = consumelist.Where(p => p.LeaguerLevelId == PayREQ.LeaguerLevelId).ToList();consumelist.Clear();consumelist = consumelistSelect;}foreach (var item in leaguereventlist){LeKeCardInfo resultLeaguer = new LeKeCardInfo();if ((OperateEvent)item.Event == OperateEvent.CancelCard){resultLeaguer = resultList.Find(a => a.LeaguerId == item.LeaguerId && a.opevent == OperateEvent.CancelCard);if (resultList.Count > 0 && resultLeaguer !=null){resultLeaguer.NowBalance = 0;resultLeaguer.PreBalance += item.Point;resultLeaguer.Balance += item.Point;continue;}elseresultLeaguer = new LeKeCardInfo();resultLeaguer.NowBalance = 0;}elseresultLeaguer.NowBalance = item.PreBalance + item.Point;resultLeaguer.opevent = (OperateEvent)item.Event;resultLeaguer.LeaguerId = item.LeaguerId;resultLeaguer.CardNo = DbLeaguer.GetCardNo(item.LeaguerId);resultLeaguer.OperateTime = item.Createdatetime;resultLeaguer.cardType = (int)CardType.LeaguerCard;resultLeaguer.cardTypeName = EnumHelper.GetDescription(CardType.LeaguerCard);var LeaguerInfo = DbLeaguer.GetLeaguerInfoById(item.LeaguerId);if (LeaguerInfo == null)continue;resultLeaguer.UserName = LeaguerInfo.UserName;resultLeaguer.Tel = LeaguerInfo.Tel;resultLeaguer.LeaguerLevel = DbLeaguerLevel.GetLeaguerLevelName(item.LeaguerLevelId);resultLeaguer.Remark = EnumHelper.GetDescription((OperateEvent)item.Event);resultLeaguer.PreBalance = item.PreBalance;resultLeaguer.Balance = item.Point;resultLeaguer.OperatorName = DbUserInfo.GetOperatorName(item.Operator);resultLeaguer.TerminalName = DbCard.GetTerminalName(item.TerminalId);resultList.Add(resultLeaguer);}foreach (var item in consumelist){LeKeCardInfo resultLeaguer = new LeKeCardInfo();resultLeaguer.OperateTime = item.Createdatetime;resultLeaguer.cardType = (CardType)item.Cardtype;resultLeaguer.cardTypeName = EnumHelper.GetDescription(resultLeaguer.cardType);string userName = "-";string tel = "-";string leaguerLevel = "-";if (resultLeaguer.cardType == CardType.LeaguerCard){var LeaguerInfo = DbLeaguer.GetLeaguerInfoById(item.ChildCardId);if (LeaguerInfo == null)continue;userName = LeaguerInfo.UserName;tel = LeaguerInfo.Tel;if (PayREQ.LeaguerLevelId > 0)leaguerLevel = DbLeaguerLevel.GetLeaguerLevelName(PayREQ.LeaguerLevelId);elseleaguerLevel = DbLeaguerLevel.GetLevelNameByLeaguerId(item.ChildCardId);}resultLeaguer.CardNo = DbCard.GetCardNoByChildId(item.ChildCardId, resultLeaguer.cardType);resultLeaguer.UserName = userName;resultLeaguer.Tel = tel;resultLeaguer.LeaguerLevel = leaguerLevel;resultLeaguer.Remark = "消费";resultLeaguer.PreBalance = item.PreBalance;resultLeaguer.Balance = item.ConsumePoint;resultLeaguer.NowBalance = resultLeaguer.PreBalance - item.ConsumePoint;//resultLeaguer.OperatorName = DbUserInfo.GetOperatorName(item.Operator);   消费没有操作人resultLeaguer.TerminalName = DbTerminal.GetLTerminalItemByExperienceId(item.ExperienceId);//消费的姓名、手机号、会员级别if (item.Cardtype == (int)CardType.LeaguerCard){var leaguer = DbLeaguer.GetLeaguerInfoById(item.ChildCardId);resultLeaguer.UserName = leaguer.UserName;resultLeaguer.Tel = leaguer.Tel;//resultLeaguer.LeaguerLevel = DbLeaguerLevel.GetLevelNameByLeaguerId(item.ChildCardId);if (PayREQ.LeaguerLevelId > 0)leaguerLevel = DbLeaguerLevel.GetLeaguerLevelName(PayREQ.LeaguerLevelId);elseleaguerLevel = DbLeaguerLevel.GetLevelNameByLeaguerId(item.ChildCardId);}resultList.Add(resultLeaguer);}if (temporaryEventList != null && temporaryEventList.Count > 0){foreach (var item in temporaryEventList){LeKeCardInfo resultTemp = new LeKeCardInfo();resultTemp.PreBalance = item.PreBalance;resultTemp.Balance = item.Point;if ((OperateEvent)item.Event == OperateEvent.CancelCard){resultTemp.NowBalance = 0;resultTemp.PreBalance = item.Point;resultTemp.Balance = item.Point;}elseresultTemp.NowBalance = item.PreBalance + item.Point;resultTemp.UserName = "-";resultTemp.Tel = "-";resultTemp.LeaguerLevel = "-";resultTemp.opevent = (OperateEvent)item.Event;resultTemp.CardNo = DbTemporaryCard.GetCardNo(item.TemporaryCardId);resultTemp.OperateTime = item.Createdatetime;resultTemp.cardType = CardType.TemporaryCard;resultTemp.cardTypeName = EnumHelper.GetDescription(CardType.TemporaryCard);resultTemp.Remark = EnumHelper.GetDescription((OperateEvent)item.Event);resultTemp.OperatorName = DbUserInfo.GetOperatorName(item.Operator);resultTemp.TerminalName = DbCard.GetTerminalName(item.TerminalId);resultList.Add(resultTemp);}}return resultList;}

       引以为戒吧……

***************************************************************************************************************************************************************************************

       昨天晚上抽了点时间,对这个玩意重构了一下:

       

Part 1:

 #region 定义变量以及where//存储最终的数据List<LeKeCardInfo> resultList = new List<LeKeCardInfo>();List<LeKeCardInfo> resultNull = new List<LeKeCardInfo>();List<D.Consume> consumeList = new List<Consume>();var where = new Where<D.LeaguerEvent>();var where1 = new Where<D.Consume>();var where2 = new Where<D.ExperienceRecord>();var where3 = new Where<D.TemporaryEvent>();where.And(a => a.Createdatetime >= PayREQ.StartTime && a.Createdatetime <= PayREQ.EndTime && a.Event != (int)OperateEvent.RemakeCard && a.Event != (int)OperateEvent.ChangeCard);where1.And(a => a.Createdatetime >= PayREQ.StartTime && a.Createdatetime <= PayREQ.EndTime && a.IsSuccess == 1);where2.And(a => a.EndTime >= PayREQ.StartTime && a.EndTime <= PayREQ.EndTime);where3.And(a => a.Createdatetime >= PayREQ.StartTime && a.Createdatetime <= PayREQ.EndTime && a.Event != (int)OperateEvent.RemakeCard && a.Event != (int)OperateEvent.ChangeCard);#endregion

Part2:

#region 根据条件拼接where条件()//终端ID不为空if (PayREQ.TerminalId > 0){string terminalname = DbCard.GetTerminalName(PayREQ.TerminalId);where2.And(a => a.LeaguerID == terminalname);where.And(a => a.TerminalId == PayREQ.TerminalId);where3.And(a => a.TerminalId == PayREQ.TerminalId);}//卡号不为空if (!string.IsNullOrEmpty(PayREQ.CardNum)){var card = DbCard.GetCardInfo(PayREQ.CardNum);if (card == null)return resultNull;List<int> id = new List<int>();if (card.Type == (int)CardType.LeaguerCard){int leagId = DbLeaguer.GetLeaguerId(card.Id);where.And(a => a.LeaguerId == leagId);where1.And(a => a.ChildCardId == leagId && a.Cardtype == (int)CardType.LeaguerCard);where2.And(a => a.LeaguerID == PayREQ.CardNum);}if (card.Type == (int)CardType.TemporaryCard){int tempId = DbTemporaryCard.GetTempId(card.Id);if (!string.IsNullOrEmpty(PayREQ.LeaguerName) || !string.IsNullOrEmpty(PayREQ.MenuName) || PayREQ.LeaguerLevelId > 0)return resultNull;where1.And(a => a.ChildCardId == tempId && a.Cardtype == (int)CardType.TemporaryCard);where3.And(a => a.TemporaryCardId == tempId);}if (card.Type == (int)CardType.TestCard)return resultNull;}//会员名不为空if (!string.IsNullOrEmpty(PayREQ.LeaguerName)){var leag = MySql.From<D.LeaguerCard>().Where(a => a.UserName == PayREQ.LeaguerName).ToList();if (leag == null || leag.Count == 0)return resultNull;var leaguerId = from fn in leagselect fn.Id;List<int> id = leaguerId.ToList();where.And(p => id.Contains(p.LeaguerId));where1.And(a => id.Contains(a.ChildCardId) && a.Cardtype == (int)CardType.LeaguerCard);}//套餐名不为空if (!string.IsNullOrEmpty(PayREQ.MenuName)){var menu = MySql.From<D.Menu>().Where(a => a.Name == PayREQ.MenuName).ToList();if (menu == null || menu.Count == 0)return resultNull;var menuId = from fn in menuselect fn.Id;where.And(p => menuId.Contains(p.MenuId));}//会员级别不为空if (PayREQ.LeaguerLevelId > 0){where.And(a => a.LeaguerLevelId == PayREQ.LeaguerLevelId);where1.And(a => a.LeaguerLevelId == PayREQ.LeaguerLevelId);}#endregion
       这部分很难提取出一个新的方法(需要有4个不同的返回值),而且也没有必要,逻辑清晰了,组长说,封装的方法,一定是要常用的,你仅仅就这里用一次,封装没有必要了。

      

Part3:

#region 获取where查询结果var leagEventlist = MySql.From<D.LeaguerEvent>().Where(where).ToList();var consList = MySql.From<D.Consume>().Where(where1).ToList();var experienceList = MySql.From<D.ExperienceRecord>().Where(where2).ToList();var tempEventList = MySql.From<D.TemporaryEvent>().Where(where3).ToList();foreach (var e in experienceList){var consume = consList.Find(a => a.ExperienceId == e.Id);if (consume != null)consumeList.Add(consume);}#endregion

Part4:

#region 遍历组合返回结果resultList.AddRange(GetLeagEventList(leagEventlist));resultList.AddRange(GetTempEventList(tempEventList));resultList.AddRange(GetConsumeList(consumeList,PayREQ.LeaguerLevelId));return resultList;#endregion

Part5:

 public static List<LeKeCardInfo> GetLeagEventList(List<LeaguerEvent> leagEventlist){List<LeKeCardInfo> resultList = new List<LeKeCardInfo>();foreach (var item in leagEventlist){LeKeCardInfo resultLeaguer = new LeKeCardInfo();if ((OperateEvent)item.Event == OperateEvent.CancelCard){resultLeaguer = resultList.Find(a => a.LeaguerId == item.LeaguerId && a.opevent == OperateEvent.CancelCard);if (resultList.Count > 0 && resultLeaguer != null){resultLeaguer.NowBalance = 0;resultLeaguer.PreBalance += item.Point;resultLeaguer.Balance += item.Point;continue;}elseresultLeaguer = new LeKeCardInfo();resultLeaguer.NowBalance = 0;}elseresultLeaguer.NowBalance = item.PreBalance + item.Point;resultLeaguer.opevent = (OperateEvent)item.Event;resultLeaguer.LeaguerId = item.LeaguerId;resultLeaguer.CardNo = DbLeaguer.GetCardNo(item.LeaguerId);resultLeaguer.OperateTime = item.Createdatetime;resultLeaguer.cardType = (int)CardType.LeaguerCard;resultLeaguer.cardTypeName = EnumHelper.GetDescription(CardType.LeaguerCard);var LeaguerInfo = DbLeaguer.GetLeaguerInfoById(item.LeaguerId);if (LeaguerInfo == null)continue;resultLeaguer.UserName = LeaguerInfo.UserName;resultLeaguer.Tel = LeaguerInfo.Tel;resultLeaguer.LeaguerLevel = DbLeaguerLevel.GetLeaguerLevelName(item.LeaguerLevelId);resultLeaguer.Remark = EnumHelper.GetDescription((OperateEvent)item.Event);resultLeaguer.PreBalance = item.PreBalance;resultLeaguer.Balance = item.Point;resultLeaguer.OperatorName = DbUserInfo.GetOperatorName(item.Operator);resultLeaguer.TerminalName = DbCard.GetTerminalName(item.TerminalId);resultList.Add(resultLeaguer);}return resultList;}
        这里还有3个类似的方法被封装,就不展示了.

        That's all.


这篇关于【Basic Code】一个被我写烂的坏味道,引以为戒的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/345560

相关文章

Debugging Lua Project created in Cocos Code IDE creates “Waiting for debugger to connect” in Win-7

转自 I Installed Cocos Code IDE and created a new Lua Project. When Debugging the Project(F11) the game window pops up and gives me the message waiting for debugger to connect and then freezes. Also a

LLVM入门2:如何基于自己的代码生成IR-LLVM IR code generation实例介绍

概述 本节将通过一个简单的例子来介绍如何生成llvm IR,以Kaleidoscope IR中的例子为例,我们基于LLVM接口构建一个简单的编译器,实现简单的语句解析并转化为LLVM IR,生成对应的LLVM IR部分,代码如下,文件名为toy.cpp,先给出代码,后面会详细介绍每一步分代码: #include "llvm/ADT/APFloat.h"#include "llvm/ADT/S

VS Code 调试go程序的相关配置说明

用 VS code 调试Go程序需要在.vscode/launch.json文件中增加如下配置:  // launch.json{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information,

code: 400, msg: Required request body is missing 错误解决

引起这个错误的原因是,请求参数按照get方式给。 应该给json字符串才对 补充: 1. @RequestBody String resource 加@RequestBody必须给json字符串,否则会报错400,记如标题错误。 不加这个的进行请求的话,其实post和get就没有什么区别了。 2. List<String> indexCodes=(List<String>)json.

iOS项目发布提交出现invalid code signing entitlements错误。

1、进入开发者账号,选择App IDs,找到自己项目对应的AppId,点击进去编辑, 2、看下错误提示出现  --Specifically, value "CVYZ6723728.*" for key "com.apple.developer.ubiquity-container-identifiers" in XX is not supported.-- 这样的错误提示 将ubiquity

查看Excel 中的 Visual Basic 代码,要先设置excel选项

1. excel VB的简单介绍 百度安全验证 2.excel选项设置 excel表格中在选项->自定义功能区域,选择开发工具,visual baisc/查看代码,即可看到代码。 3.excel已经设置,可以直接查看

解决服务器VS Code中Jupyter突然崩溃的问题

问题 本来在服务器Anaconda的Python环境里装其他的包,装完了想在Jupyter里写代码验证一下有没有装好,一运行发现Jupyter崩溃了!?报错如下所示 Failed to start the Kernel. ImportError: /home/hujh/anaconda3/envs/mia/lib/python3.12/lib-dynload/_sqlite3.cpython-

Behind the Code:与 Rakic 和 Todorovic 对话 OriginTrail 如何实现 AI 去中心化

原文:https://www.youtube.com/watch?v=ZMuLyLCtE3s&list=PLtyd7v_I7PGnko80O0LCwQQsvhwAMu9cv&index=12 作者:The Kusamarian 编译:OneBlock+ 随着人工智能技术的飞速发展,一系列前所未有的挑战随之而来:模型的衰退与互联网的潜在威胁愈发明显。AI 的增长曲线可能因训练过程中的瓶颈而趋于平

冒泡排序和鸡尾酒排序(code)

昨天回顾了下冒泡排序和鸡尾酒排序,用面向对象的方式写了一下,并且优化了代码,记录一下~ 一、冒泡排序 # 冒泡排序class BubbleSort(object):def __init__(self, data_list):self.data_list = data_listself.length = len(data_list)# 简单粗暴的排序方式def b_sort(self):d

编译时出现错误 -- clang: error: linker command failed with exit code 1 (use -v to see invocation)

出现这个错误的原因有多种,常见的是因为某些文件的缺失或者是文件的重复导致的。 这类错误查看的关键在于其上一行的文字。 对于文件缺少而导致错误的情况: 例如上图中的示例,其上一行文字为 ld:library not found for -lrxl,可以看出是缺失了某一文件而导致的错误,这行文字中的最后“ -lrxl ”:-l 代表着其前缀是“lib”,连着后面的 rxl,其名称为 libr