Pārlūkot izejas kodu

finikes alpha 23

finikes 2 gadi atpakaļ
vecāks
revīzija
98abc3daea

+ 35 - 7
src/main/java/com/finikes/oc/vote/controller/VoteController.java

@@ -8,12 +8,12 @@ import com.finikes.oc.vote.dto.*;
 import com.finikes.oc.vote.service.VoteService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.Valid;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.io.IOException;
+import java.util.*;
 
 /**
  * 投票控制器
@@ -155,17 +155,45 @@ public class VoteController {
     /**
      * 代理投票
      *
-     * @param dto 代理投票数据传输对象
      * @return 接口返回对象
      */
     @PutMapping("/choice/delegation")
-    public ApiResponse<SignCodeDTO> choiceDelegation(@RequestBody @Valid ChoiceDelegationDto dto) {
-        int signCode = voteService.madeChoiceDelegation(dto);
+    public ApiResponse<SignCodeDTO> choiceDelegation(@RequestParam("photo") MultipartFile photo, @RequestParam("optionId") Integer optionId
+            , @RequestParam("houseId") Integer houseId, @RequestParam("signature") String signature) {
+        ChoiceDelegationDto dto = new ChoiceDelegationDto();
+        dto.setHouseId(houseId);
+        dto.setOptionId(optionId);
+        dto.setSignature(signature);
+        String extName = photo.getOriginalFilename().split("\\.")[1];
+        dto.setImgType(extName);
+        long[] insertResults = voteService.madeChoiceDelegation(dto);
+        int signCode = (int) insertResults[0];
+        long choiceId = insertResults[1];
+        try {
+            savePhoto(photo, choiceId, extName);
+        } catch (IOException e) {
+            e.printStackTrace();
+            // fixme delete choice[choiceId] 模拟事务回滚
+            throw new RuntimeException(e);
+        }
         SignCodeDTO response = new SignCodeDTO();
         response.setSignCode(convertSignCode(signCode));
         return ApiResponse.successful(response);
     }
 
+    public String savePhoto(MultipartFile photo, long choiceId, String extName) throws IOException {//将文件保存到本地
+        String dir = "/home/ocv1-server";//建议这里写resources目录的绝对路径
+        File path = new File(dir + "/vote-photo/");
+        if (!path.exists()) {//如果当前目录不存在
+            path.mkdir();
+        }
+
+        File file = new File(dir + "/vote-photo/" + choiceId + "." + extName);
+        photo.transferTo(file);//将此图像保存到file本地
+
+        return extName;
+    }
+
     private String convertSignCode(int signCode) {
         String code = Ten2Base36.decimalToBase36(signCode);
         int diff = 6 - code.length();

+ 9 - 0
src/main/java/com/finikes/oc/vote/dto/ChoiceDelegationDto.java

@@ -4,6 +4,15 @@ public class ChoiceDelegationDto {
     private int optionId;
     private int houseId;
     private String signature;
+    private String imgType;
+
+    public String getImgType() {
+        return imgType;
+    }
+
+    public void setImgType(String imgType) {
+        this.imgType = imgType;
+    }
 
     public String getSignature() {
         return signature;

+ 9 - 0
src/main/java/com/finikes/oc/vote/entity/Choice.java

@@ -13,6 +13,15 @@ public class Choice {
     private int houseId;
     private String signature;
     private int signCode;
+    private String imgType;
+
+    public String getImgType() {
+        return imgType;
+    }
+
+    public void setImgType(String imgType) {
+        this.imgType = imgType;
+    }
 
     public int getSignCode() {
         return signCode;

+ 1 - 1
src/main/java/com/finikes/oc/vote/service/VoteService.java

@@ -67,7 +67,7 @@ public interface VoteService {
      *
      * @param dto 代理投票数据传输对象
      */
-    int madeChoiceDelegation(ChoiceDelegationDto dto);
+    long[] madeChoiceDelegation(ChoiceDelegationDto dto);
 
     VoteResultDto voteResult(Integer voteId);
 }

+ 7 - 4
src/main/java/com/finikes/oc/vote/service/VoteServiceImpl.java

@@ -304,7 +304,7 @@ public class VoteServiceImpl implements VoteService {
     }
 
     @Override
-    public int madeChoiceDelegation(ChoiceDelegationDto dto) {
+    public long[] madeChoiceDelegation(ChoiceDelegationDto dto) {
         int houseId = dto.getHouseId();
         Passport passport = PassportHelper.currentPassport();
         if (passport == null) {
@@ -353,9 +353,8 @@ public class VoteServiceImpl implements VoteService {
             }
 
             SignCode signCode = new SignCode();
-            signCodeDAO.nextCode(new SignCode());
+            signCodeDAO.nextCode(signCode);
             int code = signCode.getCode();
-
             Choice choice = new Choice();
             choice.setOptionId(dto.getOptionId());
             choice.setAssigneeId(passport.getId());
@@ -364,10 +363,14 @@ public class VoteServiceImpl implements VoteService {
             choice.setHouseId(houseId);
             choice.setSignature(dto.getSignature());
             choice.setSignCode(code);
+            choice.setImgType(dto.getImgType());
 
             choiceDao.insert(choice);
 
-            return code;
+            long[] results = new long[2];
+            results[0] = code;
+            results[1] = choice.getId();
+            return results;
         } finally {
             lockService.unlock(voteResourceId, ownerId);
             lockService.unlock(houseResourceId, ownerId);

+ 1 - 1
src/main/resources/application.yml

@@ -9,7 +9,7 @@ spring:
   datasource:
     # matrix34.tpddns.cn:13306
     # localhost:3306
-    url: jdbc:mysql://matrix34.tpddns.cn:13306/community?useSSL=false&useUnicode=true&characterEncoding=utf-8&useAffectedRows=true&serverTimezone=UTC
+    url: jdbc:mysql://localhost:3306/community?useSSL=false&useUnicode=true&characterEncoding=utf-8&useAffectedRows=true&serverTimezone=UTC
     username: root
     password: 9a41012499f38251
     driver-class-name: com.mysql.cj.jdbc.Driver

+ 2 - 2
src/main/resources/mapper/ChoiceMapper.xml

@@ -5,8 +5,8 @@
 
     <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
             parameterType="com.finikes.oc.vote.entity.Choice">
-        INSERT INTO t_choice (option_id, choose_time, proxy, assignee_id, house_id, signature, sign_code)
-        VALUES (#{optionId}, #{chooseTime}, #{proxy}, #{assigneeId}, #{houseId}, #{signature}, #{signCode})
+        INSERT INTO t_choice (option_id, choose_time, proxy, assignee_id, house_id, signature, sign_code, img_type)
+        VALUES (#{optionId}, #{chooseTime}, #{proxy}, #{assigneeId}, #{houseId}, #{signature}, #{signCode}, #{imgType})
     </insert>
 
     <update id="update" parameterType="com.finikes.oc.vote.entity.Choice">