kongdeqiang
2023-07-10 85f5baee0e3d255860395278dcb5cbb5cc766023
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.boying.job;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boying.common.ReturnData;
import com.boying.common.SystemConfigProperties;
import com.boying.entity.EticketFile;
import com.boying.entity.FileInfo;
import com.boying.entity.OrderRecord;
import com.boying.service.FileInfoService;
import com.boying.service.OrderRecordService;
import com.boying.util.FileUtil;
import com.boying.util.HTTPEntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
 
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Component
public class GetEticketScheduled {
 
    @Autowired
    private OrderRecordService orderRecordService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private SystemConfigProperties systemConfigProperties;
    private Boolean taskFlag = false;
 
    //@Scheduled(cron = "0 0/1 * * * ?")
    public void execute() throws Exception {
        if (taskFlag == true) {
            System.out.println("正在运行,强制退出-------》");
            return;
        }
        QueryWrapper<OrderRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda()
//                .eq(OrderRecord::getTicketStatus, 0)
//                .isNotNull(OrderRecord::getEinvoicecode)
//                .orderByDesc(OrderRecord::getCreateTime);
                .in(OrderRecord::getPayCode,"13040623000000055070","13040623000000062406","13040623000000232508","13040623000000363081","13040623000000242992","13040623000000319574","13040623000000362919","13040623000000154458","13040623000000151150",
                        "13040623000000155450","13040623000000241009","13040623000000363014","13040623000000220187","13040623000000231732","13040623000000362898","13040623000000362900","13040623000000362951","13040623000000153156","13040623000000150991",
                        "13040623000000327435","13040623000000226482");
        List<OrderRecord> list = orderRecordService.list(wrapper);
        if (list.size() > 0) {
            if (list.size() > 50) {
                list = list.subList(0, 49);
            }
            //开取发票
            for (OrderRecord orderRecord : list) {
                System.out.println("正在申请开票:" + orderRecord);
                doTicket(orderRecord);
            }
        }
        taskFlag = false;
 
    }
 
    public void doTicket(OrderRecord orderRecord){
        String urlIp = systemConfigProperties.getIp4();
        String urlRoad = systemConfigProperties.getEINVOICESTATUS();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("eticketnum",orderRecord.getPayCode());
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ReturnData> entity = restTemplate.postForEntity(urlIp+urlRoad, HTTPEntityUtil.setEntity(map1), ReturnData.class);
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
        String txt = sdf3.format(new Date())+"申请开票缴款码为:"+orderRecord.getPayCode()+",\n";
        if(entity.getBody().getCode() == 1){
            System.out.println(entity.getBody());
            //申请成功,去获取开票文件
            txt += entity.getBody()+",\n";
            Object o = entity.getBody().getData().get(0);
            String s = JSON.toJSONString(o);
            Map<String,Object> mapTypes = (Map)JSON.parse(s);
            txt += mapTypes+",\n";
            writeTxt(txt);
            doTicketGet(mapTypes,orderRecord);
        }else {
            System.out.println(entity.getBody());
            txt += "查询有误,"+entity.getBody()+"\n";
            writeTxt(txt);
        }
    }
 
    public void doTicketGet(Map<String,Object> map,OrderRecord orderRecord){
        String urlIp = systemConfigProperties.getIp4();
        String urlRoad = systemConfigProperties.getGETEINVOICE();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("einvoicecode", map.get("einvoicecode"));
        map1.put("einvoicenumber",map.get("einvoicenumber"));
        map1.put("eticketnumber", map.get("eticketnum"));
        map1.put("randomnumber", map.get("randomnumber"));
        map1.put("billfiletype",1);
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
        String txt = sdf3.format(new Date())+"申请票据文件:"+map1+",\n";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ReturnData> entity = restTemplate.postForEntity(urlIp+urlRoad, HTTPEntityUtil.setEntity(map1), ReturnData.class);
        if(entity.getBody().getCode() == 1){
            System.out.println(entity.getBody());
            txt += entity.getBody()+",\n";
            Object o1 = entity.getBody().getData().get(0);
            String s1 = JSON.toJSONString(o1);
            EticketFile eticketFile = JSONObject.parseObject(s1, EticketFile.class);
            System.out.println(eticketFile);
            txt += eticketFile+"\n";
            FileInfo fileInfo = FileUtil.generateBase64StringToFile(eticketFile.getEinvoicefile(), 1,1);
            if(fileInfo != null){
                System.out.println(fileInfo);
                fileInfoService.saveOrUpdate(fileInfo);
                orderRecord.setFileId(fileInfo.getId());
                orderRecord.setTicketStatus(1);
                orderRecordService.saveOrUpdate(orderRecord);
                txt += fileInfo+"\n";
                writeTxt(txt);
            }
        }else {
            System.out.println(entity.getBody());
            txt += "查询有误,"+entity.getBody()+",\n";
            writeTxt(txt);
 
        }
    }
 
 
    private void writeTxt( String txt)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try
        {
            FileWriter f = new FileWriter(systemConfigProperties.getLogPath()+"获取开票文件日志"+sdf.format(new Date())+".txt",true);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(txt);
            bw.newLine();
            bw.close();
        }
        catch(Exception e)
        {
            System.out.println("打印错误");
        }
    }
 
}