zhanghonglin
7 小时以前 1e6a3ce80c38124fe8750f59956528204e696d4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Quartz;
using WIDESEA_Model.Models.Basic;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO.ERP;
using WIDESEAWCS_DTO.MES;
using WIDESEAWCS_Model.Models.ERP;
using WIDESEAWCS_QuartzJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class ERPJob : JobBase, IJob
    {
        private readonly IRepository<Dt_ERP> _ERPRepository;
 
        //秒
        private static int shijian = 0;
 
        public ERPJob(IRepository<Dt_ERP> ERPRepository)
        {
            _ERPRepository = ERPRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                //一天获取一次校验码
                Thread.Sleep(1000);
                shijian = shijian - 1;
                //获取ERP的token
                if (shijian <= 0)
                {
                    string ERP = AppSettings.Get("ERP");
                    if (!string.IsNullOrEmpty(ERP))
                    {
                        Dt_ERP Dt_ERP = _ERPRepository.QueryFirst(x => x.Id == 1);
 
                        AppERP appERP = new AppERP();
                        appERP.appTicket = Dt_ERP.appTicket;
                        appERP.certificate = Dt_ERP.certificate;
 
                        var headers = new Dictionary<string, string>();
                        headers.Add("AppKey", Dt_ERP.AppKey);
                        headers.Add("AppSecret", Dt_ERP.AppSecret);
                        ERPtoken s = JsonConvert.DeserializeObject<ERPtoken>(HttpHelper.Post($"{ERP}/v1/common/auth/selfBuiltApp/generateToken", appERP.Serialize(), "application/json", headers));
                        if (s.result.Equals("true"))
                        {
                            Dt_ERP.Token = s.value.accessToken;
                            shijian = 86400;
 
                            _ERPRepository.UpdateData(Dt_ERP);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //写入日志
                LogLock.OutLogAOP("ERP日志", new string[] { "ERP", $"{ex.Message}" });
            }
            return Task.CompletedTask;
        }
    }
}