image.png面向接口编程附属于面向对象思想体系,属于其一部分。
接口,在表面上是由几个没有主体代码的方法定义组成的集合体,有唯一的名称,可以被类或其他接口所实现(或者也可以说继承)也就是把平常的方法写成Interface接口方法,后面Service.Implement继承Interface接口实现调用
public interface InterfaceName
{
void Method1();
void Method2(int para1);
void Method3(string para2,string para3);
}
Service.Dto
- 相当于实体类Model
- 可以给条件限制 (保证代码后期if{}else{} 判断过多 代码冗余)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rdp.Core.Data;
using System.Collections.Generic;
namespace Service.Dto
{
public class StaffWhiteListsDto
{
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Seq")]
public string Seq { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Card_No")]
public long? CardNo { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "CCM.CRM_Domain")]
public int? CRM_Domain { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "CUM.Customer_ID")]
public int? CustomerID { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualLess, CompareField = "Create_Time")]
public DateTime? CreateTime { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Create_User")]
public string CreateUser { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualGreater, CompareField = "Effective_Time")]
public DateTime? EffectiveTimeFrom { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualLess, CompareField = "DATEADD(DAY,-1,Effective_Time)")]
public DateTime? EffectiveTimeTo { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualGreater, CompareField = "Invalid_Time")]
public DateTime? InvalidTimeFrom { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualLess, CompareField = "DATEADD(DAY,-1,Invalid_Time)")]
public DateTime? InvalidTimeTo { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Approval_User")]
public string ApprovalUser { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualLess, CompareField = "Approval_Time")]
public DateTime? ApprovalTime { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Update_User")]
public string UpdateUser { get; set; }
[SqlWhere(CompareType = SqlCompareType.EqualLess, CompareField = "Update_Time")]
public DateTime? UpdateTime { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Status_Flag")]
public int? StatusFlag { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "Remark")]
public string Remark { get; set; }
[SqlWhere(CompareType = SqlCompareType.Equal, CompareField = "CUM.Mobile_Phone1")]
public string MobilePhone { get; set; }
}
}
Interface
- 没有主体代码的方法定义组成的集合体,有唯一的名称,可以被类或其他接口所实现(或者也可以说继承)。
using System;
using System.Data;
using Service.Dto;
using Rdp.Core;
using Rdp.Core.Data;
namespace Service.Interface
{
public interface IStaffWhiteListService
{
/// <summary>
/// 筛选
/// </summary>
/// <param name="WhiteListDto"></param>
/// <param name="param"></param>
/// <returns></returns>
DataTable IndexFilter(StaffWhiteListsDto whitelistdto, PageParam param);
/// <summary>
/// 添加
/// </summary>
/// <param name="WhiteListDto"></param>
/// <returns></returns>
OperateResult Add(StaffWhiteListsDto whitelistdto);
/// <summary>
/// 删除
/// </summary>
/// <param name="WhiteListDto"></param>
/// <returns></returns>
OperateResult Delete(StaffWhiteListsDto whitelistdto);
/// <summary>
///审批
/// </summary>
/// <param name="WhiteListDto"></param>
/// <returns></returns>
OperateResult Approval(StaffWhiteListsDto whitelistdto);
/// <summary>
/// 添加筛选
/// </summary>
/// <param name="whitelistdto"></param>
/// <param name="pageParam"></param>
/// <returns></returns>
DataTable AddFilter(StaffWhiteListsDto whitelistdto, PageParam pageParam);
}
}
Implement
- Service.Implement继承Interface接口实现调用,编写业务逻辑。
using Service.Dto;
using Rdp.Core;
using Rdp.Core.Data;
using System.Data;
using System.Data.SqlClient;
namespace Service.Implement
{
public class StaffWhiteListService
{
/// <summary>
/// 添加
/// </summary>
/// <param name="whiteListDto"></param>
/// <returns></returns>
public OperateResult Add(StaffWhiteListsDto whitelistdto)
{
string strsql = string.Format(@"
INSERT INTO dbo.tbCRM_Staff_White_List
( CRM_Domain ,
Customer_ID ,
Create_User ,
Update_User ,
Status_Flag
)
SELECT CRM_Domain AS CRM_Domain ,
Customer_ID AS Customer_ID ,
'{0}' AS Create_User ,
'{0}' AS Update_User ,
2 AS Status_Flag
FROM tbCRM_Card_Master
WHERE Card_No = {1}
AND Card_No<>0
AND Status_ID = 0 ;", whitelistdto.CreateUser, whitelistdto.CardNo);
int Row = DbHelperSql.ExecuteSql(CRM.DBUtility.DbHelperSql.CrmQuery, strsql);
if (Row > 0)
return new OperateResult { Status = 0, Message = "添加成功!请等待审核" };
else
return new OperateResult { Status = 1, Message = "添加失败" };
}
/// <summary>
/// 审批
/// </summary>
/// <param name="whiteListDto"></param>
/// <returns></returns>
public OperateResult Approval(StaffWhiteListsDto whitelistdto)
{
string Timet = string.Empty;
if (whitelistdto.StatusFlag == 0)
Timet = " Effective_Time = GETDATE() , ";
if (whitelistdto.StatusFlag == 1)
Timet = " Invalid_Time = GETDATE() , ";
string strsql = string.Format(@"
UPDATE tbCRM_Staff_White_List
SET Status_Flag = @Status_Flag ,
Approval_User = @Approval_User ,
Approval_Time = GETDATE(),
Update_User = @Update_User ,
Update_Time = GETDATE() , {0}
Remark = @Remark
FROM tbCRM_Staff_White_List AS WL
INNER JOIN tbCRM_Card_Master AS CCM WITH ( NOLOCK )
ON CCM.CRM_Domain = WL.CRM_Domain
AND CCM.Customer_ID = WL.Customer_ID
AND CCM.Status_ID=0
WHERE WL.Seq=@Seq AND CCM.Customer_ID = @Customer_ID AND CCM.Card_No=@Card_No ;", Timet);
SqlParameter[] sqlParameter = new[]
{
new SqlParameter("@Status_Flag", SqlDbType.TinyInt,1),
new SqlParameter("@Approval_User",SqlDbType.VarChar,10),
new SqlParameter("@Update_User",SqlDbType.VarChar,10),
new SqlParameter("@Remark",SqlDbType.NVarChar,500),
new SqlParameter("@Seq", SqlDbType.VarChar, 50),
new SqlParameter("@Customer_ID", SqlDbType.Int, 4),
new SqlParameter("@Card_No", SqlDbType.BigInt, 8)
};
sqlParameter[0].Value = whitelistdto.StatusFlag;
sqlParameter[1].Value = whitelistdto.ApprovalUser;
sqlParameter[2].Value = whitelistdto.ApprovalUser;
sqlParameter[3].Value = whitelistdto.Remark;
sqlParameter[4].Value = whitelistdto.Seq;
sqlParameter[5].Value = whitelistdto.CustomerID;
sqlParameter[6].Value = whitelistdto.CardNo;
int Rowint = DbHelperSql.ExecuteSql(CRM.DBUtility.DbHelperSql.CrmQuery, strsql, sqlParameter);
if (Rowint > 0)
return new OperateResult { Status = 0, Message = "审批成功" };
else
return new OperateResult { Status = 1, Message = "审批失败" };
}
/// <summary>
/// 删除
/// </summary>
/// <param name="whitelistdto"></param>
/// <returns></returns>
public OperateResult Delete(StaffWhiteListsDto whitelistdto)
{
string sql = string.Format(@"
UPDATE tbCRM_Staff_White_List
SET Status_Flag = 1 ,
Invalid_Time = GETDATE() ,
Update_User = @Update_User,
Update_Time = GETDATE() ,
Remark=@Remark
FROM tbCRM_Staff_White_List AS WL
INNER JOIN dbo.tbCRM_Customer_Master AS CUM WITH(NOLOCK)
ON CUM.CRM_Domain = WL.CRM_Domain
AND CUM.Customer_ID = WL.Customer_ID
INNER JOIN tbCRM_Card_Master AS CCM WITH ( NOLOCK )
ON CCM.CRM_Domain = CUM.CRM_Domain
AND CCM.Customer_ID = CUM.Customer_ID
AND CCM.Status_ID=0
WHERE WL.Seq=@Seq AND CUM.Customer_ID=@Customer_ID ;");
SqlParameter[] sqlParameter = new[]
{
new SqlParameter("@Update_User",SqlDbType.VarChar,10),
new SqlParameter("@Remark",SqlDbType.NVarChar,500),
new SqlParameter("@Seq", SqlDbType.VarChar, 50),
new SqlParameter("@Customer_ID", SqlDbType.Int, 4)
};
sqlParameter[0].Value = whitelistdto.UpdateUser;
sqlParameter[1].Value = whitelistdto.Remark;
sqlParameter[2].Value = whitelistdto.Seq;
sqlParameter[3].Value = whitelistdto.CustomerID;
int Row = DbHelperSql.ExecuteSql(CRM.DBUtility.DbHelperSql.CrmQuery, sql, sqlParameter);
if (Row > 0)
return new OperateResult { Status = 0, Message = "删除成功" };
else
return new OperateResult { Status = 1, Message = "删除失败" };
}
/// <summary>
/// 主页筛选导出
/// </summary>
/// <param name="WhiteListDto"></param>
/// <param name="param"></param>
/// <returns></returns>
public DataTable IndexFilter(StaffWhiteListsDto whitelistdto, PageParam param)
{
var where = WhereSqlHelper.ModelGetWhere(whitelistdto, true);
param.TableName = string.Format(@" (
SELECT SWL.Seq ,
ISNULL(SWL.CRM_Domain, CCM.CRM_Domain) AS CRM_Domain ,
ISNULL(SWL.Customer_ID, CUM.Customer_ID) AS Customer_ID ,
CCM.Card_No ,
ISNULL(CUM.Chinese_Family_Name + CUM.Customer_Chi_Name,
CUM.English_Family_Name + CUM.Customer_Eng_Name) AS Customer_Name ,
ISNULL(CUM.Mobile_Phone1, CUM.Mobile_Phone2) AS Mobile_Phone ,
CASE WHEN SWL.Effective_Time > '1900-01-01' THEN CONVERT(VARCHAR(100), SWL.Effective_Time,20)
ELSE N''
END AS Effective_Time ,
CASE WHEN SWL.Invalid_Time > '1900-01-01' THEN CONVERT(VARCHAR(100), SWL.Invalid_Time,20)
ELSE N''
END AS Invalid_Time ,
CASE WHEN SWL.Status_Flag = 0 THEN N'有效'
WHEN SWL.Status_Flag = 1 THEN N'无效'
WHEN SWL.Status_Flag = 2 THEN N'审批中'
END AS Status_Flag ,
SWL.Create_User ,
CONVERT(VARCHAR(100),SWL.Create_Time,20) AS Create_Time ,
SWL.Approval_User ,
CASE WHEN SWL.Approval_User =0 THEN N''
ELSE CONVERT(VARCHAR(100), SWL.Approval_Time,20)
END AS Approval_Time ,
SWL.Update_User ,
CONVERT(VARCHAR(100),SWL.Update_Time,20) AS Update_Time ,
SWL.Remark
FROM tbCRM_Staff_White_List AS SWL WITH ( NOLOCK )
INNER JOIN tbCRM_Card_Master AS CCM WITH ( NOLOCK ) ON SWL.CRM_Domain = CCM.CRM_Domain
AND SWL.Customer_ID = CCM.Customer_ID
AND CCM.Status_ID=0
INNER JOIN tbCRM_Customer_Master AS CUM WITH ( NOLOCK ) ON CUM.CRM_Domain = CCM.CRM_Domain
AND CUM.Customer_ID = CCM.Customer_ID
{0}) AS TEMP ", where.whereSql);
param.SqlParamList = where.sqlParamList;
param.PrimaryKey = " Seq";
param.FieldList = param.FieldList;
param.Order = " Update_Time desc ";
return DbHelperSql.QueryByPage(CRM.DBUtility.DbHelperSql.CrmQuery, ref param);
}
/// <summary>
/// 添加页筛选
/// </summary>
/// <param name="WhiteListDto"></param>
/// <param name="pageParam"></param>
/// <returns></returns>
public DataTable AddFilter(StaffWhiteListsDto whitelistdto, PageParam pageParam)
{
var where = WhereSqlHelper.ModelGetWhere(whitelistdto, true);
pageParam.TableName = string.Format(@" (
SELECT CCM.CRM_Domain ,
CCM.Customer_ID ,
CCM.Card_No ,
ISNULL(CUM.Chinese_Family_Name + CUM.Customer_Chi_Name,
CUM.English_Family_Name + CUM.Customer_Eng_Name) AS Customer_Name ,
ISNULL(CUM.Mobile_Phone1, CUM.Mobile_Phone2) AS Mobile_Phone ,
WL.Status_Flag ,
CASE WHEN CCT.Tag_ID IS NOT NULL AND (WL.Status_Flag IS NULL OR WL.Status_Flag =1)THEN 1
ELSE 0
END AS Status,
CCT.Tag_ID
FROM tbCRM_Card_Master AS CCM WITH ( NOLOCK )
LEFT JOIN tbCRM_Customer_Master AS CUM WITH ( NOLOCK ) ON CUM.CRM_Domain = CCM.CRM_Domain
AND CUM.Customer_ID = CCM.Customer_ID
LEFT JOIN tbCRM_Staff_White_List AS WL WITH ( NOLOCK ) ON WL.CRM_Domain = CUM.CRM_Domain
AND WL.Customer_ID = CUM.Customer_ID
AND WL.Status_Flag <> 1
LEFT JOIN dbo.tbCRM_Customer_Tag AS CCT WITH ( NOLOCK ) ON CCT.CRM_Domain = CCM.CRM_Domain
AND CCT.Customer_ID = CCM.Customer_ID
AND CCT.Tag_ID = 108
AND CCT.Status_Flag = 0
{0} AND CCM.Status_ID = 0 ) AS TEMP ", where.whereSql);
pageParam.SqlParamList = where.sqlParamList;
pageParam.PrimaryKey = " Customer_ID";
pageParam.FieldList = " * ";
pageParam.Order = " Card_No desc ";
return DbHelperSql.QueryByPage(DBUtility.DbHelperSql.CrmQuery, ref pageParam);
}
}
}
从中看出代码非常简洁,几乎没有没有过多用到if判断和拼sql、where条件等;独立性高,相互间耦合低;后期扩展性好。
网友评论