|
@@ -1,10 +1,33 @@
|
|
|
package com.management.platform.controller;
|
|
package com.management.platform.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
|
+import com.google.zxing.BarcodeFormat;
|
|
|
|
|
+import com.google.zxing.EncodeHintType;
|
|
|
|
|
+import com.google.zxing.MultiFormatWriter;
|
|
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
|
|
+import com.google.zxing.common.BitMatrix;
|
|
|
|
|
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
|
|
+import com.management.platform.entity.MealApplications;
|
|
|
|
|
+import com.management.platform.entity.User;
|
|
|
|
|
+import com.management.platform.service.MealApplicationsService;
|
|
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* <p>
|
|
* <p>
|
|
|
* 前端控制器
|
|
* 前端控制器
|
|
@@ -16,6 +39,127 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping("/meal-applications")
|
|
@RequestMapping("/meal-applications")
|
|
|
public class MealApplicationsController {
|
|
public class MealApplicationsController {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private MealApplicationsService mealApplicationsService;
|
|
|
|
|
+ @RequestMapping("/apply")
|
|
|
|
|
+ public HttpRespMsg apply(HttpServletRequest request,String mealType, String factory){
|
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
|
+ String userId= request.getHeader("token");
|
|
|
|
|
+ if(userId==null){
|
|
|
|
|
+ return msg.fail("请先登录");
|
|
|
|
|
+ }
|
|
|
|
|
+ QueryWrapper<MealApplications> eq = new QueryWrapper<MealApplications>().eq("user_id", userId).eq("meal_type_id", mealType).eq("factory_id", factory).eq("application_date", LocalDate.now());
|
|
|
|
|
+ List<MealApplications> list = mealApplicationsService.list(eq);
|
|
|
|
|
+ Optional<MealApplications> first = list.stream().filter(mealApplications -> mealApplications.getStatus().equals("0")).findFirst();
|
|
|
|
|
+ if (first.isPresent()){
|
|
|
|
|
+ return msg.fail("已申报过");
|
|
|
|
|
+ }
|
|
|
|
|
+// HttpRespMsg qrCode = this.getQrCode(request, mealType, factory);
|
|
|
|
|
+// if (qrCode.getCode().equals("ok")){
|
|
|
|
|
+// return msg.fail("已申报过");
|
|
|
|
|
+// }
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 生成二维码内容
|
|
|
|
|
+// String qrContent = "mealId:" + mealType + ",factoryId:" + factory + ",userId:" + userId;
|
|
|
|
|
+// // 生成二维码图片
|
|
|
|
|
+// String qrCodeBase64 = generateQRCode(qrContent, 300, 300);
|
|
|
|
|
+ MealApplications mealApplications = new MealApplications();
|
|
|
|
|
+ mealApplications.setUserId(userId);
|
|
|
|
|
+ mealApplications.setMealTypeId(mealType);
|
|
|
|
|
+ mealApplications.setFactoryId(Integer.valueOf(factory));
|
|
|
|
|
+// mealApplications.setQrCode(qrCodeBase64);
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+
|
|
|
|
|
+ mealApplications.setApplicationDate(now.toLocalDate());
|
|
|
|
|
+ mealApplications.setAppliedAt(now);
|
|
|
|
|
+ mealApplications.setStatus(0);
|
|
|
|
|
+ boolean save = mealApplicationsService.save(mealApplications);
|
|
|
|
|
+ if (save) {
|
|
|
|
|
+ Integer generatedId = mealApplications.getId();
|
|
|
|
|
+
|
|
|
|
|
+ // 生成二维码内容
|
|
|
|
|
+ String qrContent = now + "" + generatedId;
|
|
|
|
|
+ // 生成二维码图片
|
|
|
|
|
+ String qrCodeBase64 = generateQRCode(qrContent, 300, 300);
|
|
|
|
|
+ mealApplications.setQrCode(qrCodeBase64);
|
|
|
|
|
+ mealApplications.setCode(qrContent);
|
|
|
|
|
+ boolean update = mealApplicationsService.updateById(mealApplications);
|
|
|
|
|
+
|
|
|
|
|
+ if (update) {
|
|
|
|
|
+ msg.setData(mealApplications);
|
|
|
|
|
+ return msg;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return msg.fail("报餐失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }else {
|
|
|
|
|
+ return msg.fail("报餐失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return msg.fail("生成二维码失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping("/cancel")
|
|
|
|
|
+ public HttpRespMsg cancelMeal(HttpServletRequest request,String mealType, String factory,String reason){
|
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
|
+// LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ UpdateWrapper<MealApplications> eq = new UpdateWrapper<MealApplications>().set("status", "2").set("cancelled_at",LocalDateTime.now()).set("cancel_reason", reason)
|
|
|
|
|
+ .eq("user_id", request.getHeader("token")).eq("meal_type_id", mealType).eq("factory_id", factory).eq("application_date", LocalDate.now()).eq("status", "0")
|
|
|
|
|
+ .orderByDesc("applied_at").last("limit 1");
|
|
|
|
|
+ boolean update = mealApplicationsService.update(eq);
|
|
|
|
|
+ if(update){
|
|
|
|
|
+ return msg;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ msg.setCode("error");
|
|
|
|
|
+ return msg;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping("/getQrCode")
|
|
|
|
|
+ public HttpRespMsg getQrCode(HttpServletRequest request,String mealType, String factory){
|
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ QueryWrapper<MealApplications> eq = new QueryWrapper<MealApplications>().eq("user_id", request.getHeader("token")).eq("meal_type_id", mealType).eq("factory_id", factory)
|
|
|
|
|
+ .eq("application_date", now.toLocalDate()).orderByDesc("applied_at").last("limit 1");
|
|
|
|
|
+ MealApplications mealApplications = mealApplicationsService.getOne(eq);
|
|
|
|
|
+ if(mealApplications==null){
|
|
|
|
|
+ return msg.fail("二维码不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ msg.setData(mealApplications);
|
|
|
|
|
+ return msg;
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成二维码并返回Base64编码
|
|
|
|
|
+ * @param content 二维码内容
|
|
|
|
|
+ * @param width 宽度
|
|
|
|
|
+ * @param height 高度
|
|
|
|
|
+ * @return Base64编码的二维码图片
|
|
|
|
|
+ */
|
|
|
|
|
+ private String generateQRCode(String content, int width, int height) throws Exception {
|
|
|
|
|
+ //创建二维码配置
|
|
|
|
|
+ Map<EncodeHintType, Object> hints = new HashMap<>();
|
|
|
|
|
+ hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
|
|
|
+ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
|
|
|
|
+ hints.put(EncodeHintType.MARGIN, 1);
|
|
|
|
|
+
|
|
|
|
|
+ //生成二维码矩阵
|
|
|
|
|
+ BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
|
|
|
|
|
+
|
|
|
|
|
+ //转换为BufferedImage
|
|
|
|
|
+ BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
|
|
|
|
|
+
|
|
|
|
|
+ //转换为Base64字符串
|
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
+ ImageIO.write(image, "PNG", outputStream);
|
|
|
|
|
+ byte[] bytes = outputStream.toByteArray();
|
|
|
|
|
+
|
|
|
|
|
+ return "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|