forked from kidgrow-microservices-platform

dougang
2020-11-14 ef31d1678d820175f47c3645704ddecf171265ae
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.kidgrow.oprationcenter.controller;
 
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.kidgrow.common.controller.BaseController;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.utils.QRCodeUtil;
import com.kidgrow.oprationcenter.model.ScreeningOrganizationInfo;
import com.kidgrow.oprationcenter.service.IScreeningOrganizationInfoService;
import com.kidgrow.redis.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
 
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: 筛查机构信息表
 * @Project: 用户中心
 * @CreateDate: Created in 2020-10-12 17:10:31 <br>
 * @Author: <a href="78125310@kidgrow.com">dougang</a>
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/screeningorganizationinfo")
@Api(tags = "筛查机构信息表")
public class ScreeningOrganizationInfoController extends BaseController {
    @Autowired
    private IScreeningOrganizationInfoService organizationInfoService;
 
    @Autowired
    private RedisUtils redisUtils;
 
    private final String URL = "url";
 
    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping
    public PageResult<ScreeningOrganizationInfo> list(@RequestParam Map<String, Object> params) {
        if (params.size() == 0) {
            params.put("page", 1);
            params.put("limit", 10);
        }
        return organizationInfoService.findList(params);
    }
 
    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{organizationId}")
    public ResultBody findById(@PathVariable String organizationId) {
        ScreeningOrganizationInfo model = organizationInfoService.getById(organizationId);
        return ResultBody.ok().data(model).msg("查询成功");
    }
 
    /**
     * 根据OrganizationInfo当做查询条件进行查询
     */
    @ApiOperation(value = "根据OrganizationInfo当做查询条件进行查询")
    @PostMapping("/query")
    public ResultBody findByObject(@RequestBody ScreeningOrganizationInfo organizationInfo) {
        ScreeningOrganizationInfo model = organizationInfoService.findByObject(organizationInfo);
        return ResultBody.ok().data(model).msg("查询成功");
    }
 
    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public ResultBody save(@Valid @RequestBody ScreeningOrganizationInfo organizationInfo, BindingResult bindingResult) {
        List<String> errMsg = new ArrayList<>();
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                errMsg.add(error.getDefaultMessage());
            }
            return ResultBody.failed().msg(errMsg.toString());
        } else {
            if (organizationInfo.getId() == null) {
                //新增机构增加关注二维码
                String url = createQr(String.valueOf(organizationInfo.getId()));
                if (StringUtils.isNotBlank(url)) {
                    organizationInfo.setQrCode(url);
                }
            }
            boolean v = organizationInfoService.saveOrUpdate(organizationInfo);
            if (v) {
                return ResultBody.ok().data(organizationInfo).msg("保存成功");
            } else {
                return ResultBody.failed().msg("保存失败");
            }
        }
    }
 
    private String createQr(String id) {
        String url = "";
        String str = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"kagscreening_\" " + id + "}}}";
 
        String token = redisUtils.get("Wechat:Token:KagReceiveToken").toString();
        String result = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token)
                .header("Content-Type", "application/json")
                .body(str).execute().body();
 
 
        if (StringUtils.isBlank(result)) {
            return url;
        }
 
        JSONObject strJson = JSONObject.parseObject(result);
        if (strJson.containsKey(URL)) {
            url = strJson.getString("url");
        }
 
        return url;
    }
 
    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public ResultBody delete(@PathVariable String organizationId) {
        boolean v = organizationInfoService.removeById(organizationId);
        if (v) {
            return ResultBody.ok().msg("删除成功");
        } else {
            return ResultBody.failed().msg("删除失败");
        }
    }
 
    /**
     * 下载二维码
     */
    @ApiOperation(value = "下载二维码")
    @PostMapping("/downLoad")
    public void downLoad(@RequestBody Map<String, Object> param, HttpServletResponse response) {
        OutputStream out = null;
        InputStream fileInputStream = null;
        try {
            String base64Str = QRCodeUtil.creatRrCode(MapUtils.getString(param, "url"), 258, 258, 3);
            String fileName = MapUtils.getString(param, "no") + ".png";
 
            BASE64Decoder dencoder = new BASE64Decoder();
            byte[] arr = dencoder.decodeBuffer(base64Str.split(",")[1]);
 
            // 设置输出的格式
            response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Length", "" + arr.length);
            response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
 
            out = response.getOutputStream();
            fileInputStream = new ByteArrayInputStream(arr);
            int len = 0;
            byte[] by = new byte[1024*10];
            while ((len = fileInputStream.read(by))>0){
                out.write(by,0,len);
            }
 
        }catch (Exception w){
            log.error("",w);
        }finally {
            try {
                out.close();
                fileInputStream.close();
            }catch(Exception w){
 
            }
 
        }
 
    }
}