背景:
后端框架: .net framework4.6.1
数据库 : sqlserver
PC框架: layui
前端框架: Angular5.0.3+ionic3.9.2
项目: 瀚晖
先记录一下常用的目录:
如下图:
- Business里面的Models文件夹下放的是自己定义的实体类.
- 例如(一般是一个模块一个文件夹,一个文件夹下是这个模块所需要用到的多个实体类文件):
- Business里面的Services文件夹下放的是逻辑处理.与数据库交互.
- 例如:
- Entity里面是利用ORM工具生成的数据库实体类.
- EntityManager里面是生成的基本的增删改查的方法
- EntityObject里面就是每张表的实体类.
- WebApi里面是定义与前端交互的地址,接口等等.
- Controllers里面是前端的接口
- WebSite里面是PC端的页面,接口,交互的地址.
- App_Config里是设置环境
- 例如:ApplicationConfig里面是数据库地址
- Sys.ORMapping是用ORM生成的表字段映射
- App_Start文件夹里面有一些绑定页面的设置
例如:BundleConfig里面是哪个页面对应的js的处理文件的绑定以及conterollers的绑定
- Controllers里面是页面和接口
- Resources里面是PC端页面的JS处理的地方.
- Views里面是页面
常用接口返回值:
- 第一种:返回boolean值
///
/// 是否已考试
///
///
///
public bool GetIsExam(string ExamID) {
try
{
var manage = new r_ExaminationManager();
// 根据id获取数据
r_Examination exam = manage.GetEntityObject(ExamID);
exam.IsExam = !exam.IsExam;
exam.Update();
}
catch (Exception)
{
return false;
}
return true;
}
- 第二种,返回一个list集合
///
/// 获取问卷题目
///
///
public IList GetSurveyList()
{
// 查询数据库
string strSql = @"select SurveyID,SurveyName from b_Survey_New where Status='Active'";
// 用dt处理一下sql及拿到返回值
DataTable dt = Query.ExecuteSQLQuery(strSql, SystemEnvironment.Instance.DefaultDataSource);
// 把返回值return出去
return dt.AsEnumerable().Select(x => new Survey
{
SurveyName = x["SurveyName"].ToString(),
SurveyID = ConvertObject.ToString(x["SurveyID"])
}).ToList();
}
例如:
- 第三种,返回一个map
///
///
public Institution GetInstitutionManagerRepots(string territoryLevel, string territoryID, string month)
{
Institution institution = new Institution();
string strsql = string.Format(@"
SELECT DISTINCT InstitutionCount,DeepCultureInsCount,DevelopingInsCount,
InstitutionCount- DeepCultureInsCount-DevelopingInsCount as OtherCount
FROM rpt_s_Manager_New_Month_Summary
where Month='{0}' and TerritoryID='{1}'
", month, territoryID);
DataTable dt = Query.ExecuteSQLQuery(strsql, "RPT");
if (dt.Rows.Count == 0)
{
institution.DeptList = GetDeptClientManagerRepots(territoryLevel, territoryID, month);
return institution;
}
return dt.AsEnumerable().Select(x => new Institution
{
InstitutionCount = ConvertObject.ToInt32(x["InstitutionCount"]),
DeepCultureInsCount = ConvertObject.ToInt32(x["DeepCultureInsCount"]),
DevelopingInsCount = ConvertObject.ToInt32(x["DevelopingInsCount"]),
OtherCount = ConvertObject.ToInt32(x["OtherCount"]),
DeptList = GetDeptClientManagerRepots(territoryLevel,territoryID, month)
}).ToList().FirstOrDefault();
}
例如:
代码分组
///
/// (已答题的详情列表)根据问卷获取题目及选项答案
///
///
///
///
public IList GetQuestionList(string SurveyID,string ExamID)
{
// 查询数据库
string strSql = string.Format(@"SELECT r.[ActivityID]
,r.[SurveyID]
,r.[SurveyName]
,r.[QuestionID]
,r.[QuestionName]
,q.[QuestionType]
,r.[OptionID]
,r.[OptionName]
,r.[Selected]
,o.Remark,o.Correct
FROM r_Examination_Survey_Record r
left join b_Survey_Question_Option_New o on o.OptionID = r.OptionID
left join b_Survey_Question_New q on q.QuestionID = r.QuestionID
where r.SurveyID='{0}' and r.ExamID='{1}' and r.Status = 'Active'", SurveyID,ExamID);
// 用dt处理一下sql及拿到返回值
DataTable dt = Query.ExecuteSQLQuery(strSql, SystemEnvironment.Instance.DefaultDataSource);
// 把返回值return出去(GroupBy:需要去重的列表.select不需要去重的里层列表)
return dt.AsEnumerable().GroupBy(c => new
{
SurveyName = c["SurveyName"].ToString(),
SurveyID = ConvertObject.ToString(c["SurveyID"])
}).Select(x => new QuestionAndOption
{
SurveyName = x.Key.SurveyName,
SurveyID = x.Key.SurveyID,
QuestionList = x.GroupBy(s => new
{
QuestionID = s["QuestionID"].ToString(),
QuestionName = s["QuestionName"].ToString(),
QuestionType = s["QuestionType"].ToString(),
}).Select(g => new QuestionList
{
QuestionID = g.Key.QuestionID,
QuestionName = g.Key.QuestionName,
QuestionType= g.Key.QuestionType,
OptionList = g.Select(o => new OptionList
{
OptionID = o["OptionID"].ToString(),
OptionName = o["OptionName"].ToString(),
Remark = o["Remark"].ToString(),
Correct = o["Correct"].ToString(),
Selected = ConvertObject.ToBoolean(o["Selected"])
}).ToList()
}).ToList()
}).ToList();
}
例如:
获取当前用户信息
string staffId = HttpContext.Current.User.Identity.GetUser().StaffID;
增加事务
///
/// 保存考试内容
///
/// 返回的模型
/// 当前人员
///
public Result SaveExam(QuestionAndOption model, string staffId)
{
// 返回值
Result result = new Result();
Transaction ts = new Transaction();
//开启事务
ts.BeginTransaction();
try
{
foreach (var question in model.QuestionList)
{
foreach (var option in question.OptionList)
{
new r_Examination_Survey_Record
{
ActivityID = NewID.GetID(),
SurveyID = model.SurveyID,
SurveyName = model.SurveyName,
QuestionID = question.QuestionID,
ExamID = model.ExamID,
QuestionName = question.QuestionName,
OptionID = option.OptionID,
OptionName = option.OptionName,
AnswerStatus = "",
AnswerType = "",
StaffID = staffId,
CreateStaffID = staffId,
CreateStamp = ConvertDateTime.ToDateString(),
Status = "Active",
Selected = option.Selected ? option.Selected : false,
}.Insert(ts);
}
}
// 修改考试人员表的考试状态
string strSql = string.Format(@"update r_Examination_Survey set IsExam=1 where SurveyID='{0}'
and ExamID='{1}' and StaffID='{2}'", model.SurveyID, model.ExamID, staffId);
try
{
Query.ExecuteSQL(strSql, SystemEnvironment.Instance.DefaultDataSource);
result.ResultStatus = ResultStatus.SUCCESS.ToString();
result.ResultMessage = "提交成功!";
}
catch (Exception)
{
result.ResultStatus = ResultStatus.SUCCESS.ToString();
result.ResultMessage = "提交失败!";
}
}
catch (Exception)
{
// 关闭事务
ts.RollbackTransaction();
result.ResultStatus = ResultStatus.ERROR.ToString();
result.ResultMessage = "提交失败!";
throw;
}
// 提交事务
ts.CommitTransaction();
return result;
}
总结
- 从完全不会到慢慢懵懂到增删改查到处理遗留问题.一步步走过来用时一个半月.
- 可能现在公司用的这些框架已经很老旧了.新的技术总监也在搭建新的框架.
- 但是我相信我学会的是使用方式.以后学习新的框架会用时更短更顺手.新的一年开始了.timor要加油!
- 下面是我新开的公众号,里面有本人联系方式.会记录一些学习心得以及生活.微信群也准备好了.欢迎大家关注联系!