VistarStarDataIntegration/admin.net.pro/Admin.NET/Vistar.Application/Service/SapOpenInterface/SapOpenInterfaceService.cs

130 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Admin.NET.Core;
using Admin.NET.Core.Service;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Vistar.Application.Const;
using Vistar.Application.Entity;
using Vistar.Application.Service.SapOpenInterface.Dto;
using Vistar.Application.SolidWorksManageService.Dto;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionResponse.Types;
namespace Vistar.Application.Service.SapOpenInterface;
/// <summary>
/// 开放接口给Sap
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "SapOpenInterface", Order = 100)]
public class SapOpenInterfaceService : IDynamicApiController, ITransient
{
public SqlSugarRepository<Obj132> _obj132Rep;
public SolidWorksManageService.SolidWorksManageService _solidWorksManageService;
public SapOpenInterfaceService(SqlSugarRepository<Obj132> obj132Rep, SolidWorksManageService.SolidWorksManageService solidWorksManageService)
{
_obj132Rep = obj132Rep;
_solidWorksManageService = solidWorksManageService;
}
/// <summary>
/// 生成签名 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("生成签名")]
[AllowAnonymous]
public string GenerateSignature(GenerateSignatureInput input)
{
// 密钥
var appSecretByte = Encoding.UTF8.GetBytes(input.AccessSecret);
// 拼接参数
var parameter = $"{input.Method.ToString().ToUpper()}&{input.Url}&{input.AccessKey}&{input.Timestamp}&{input.Nonce}";
// 使用 HMAC-SHA256 协议创建基于哈希的消息身份验证代码 (HMAC)以appSecretByte 作为密钥,对上面拼接的参数进行计算签名,所得签名进行 Base-64 编码
using HMAC hmac = new HMACSHA256();
hmac.Key = appSecretByte;
var sign = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(parameter)));
return sign;
}
/// <summary>
/// SAP创建、修改客户档案
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)]
[ApiDescriptionSettings(Name = "CreateClientRecord"), HttpPost]
[DisplayName("SAP创建、修改客户档案")]
public async Task<ManageResponse> CreateClientRecord(CreateClientRecordInput input)
{
var response = new ManageResponse();
if (string.IsNullOrWhiteSpace(input._System_objNBS) || string.IsNullOrWhiteSpace(input._System_objDescription))
{
response.Success = true;
response.Message = "请输入必填字段";
return response;
}
//判断是否存在客户记录
var clientExists = await _obj132Rep.AsQueryable().AnyAsync(x => x._System_objNBS == input._System_objNBS && x.deleted == false);
if (clientExists == false)
{
var fieldList = new List<Field>();
fieldList.Add(new Field { key = "_System_objNBS", value = input._System_objNBS });
fieldList.Add(new Field { key = "_System_objDescription", value = input._System_objDescription });
if (!string.IsNullOrWhiteSpace(input.fld004808))
{
fieldList.Add(new Field { key = "fld004808", value = input.fld004808 });
}
if (!string.IsNullOrWhiteSpace(input.fld004950))
{
fieldList.Add(new Field { key = "fld004950", value = input.fld004950 });
}
var data = new CreateRecordInput
{
object_id = 132,
field_group_id = 422,
fields = fieldList
};
response = await _solidWorksManageService.CreateRecord(data);
return response;
}
else if (clientExists == true)
{
var updateable = await _obj132Rep.AsUpdateable()
.SetColumns(it => new Obj132
{
_System_ObjDescription = input._System_objDescription,
fld004808 = input.fld004808,
fld004950 = input.fld004950
})
.Where(x => x._System_objNBS == input._System_objNBS && x.deleted == false)
.ExecuteCommandHasChangeAsync();
response.Success = true;
response.Message = updateable ? "修改成功" : "修改失败";
return response;
}
return response;
}
}