Przeglądaj źródła

finikes alpha 22

finikes 2 lat temu
rodzic
commit
e1c101d1df

+ 42 - 0
src/main/java/com/finikes/oc/common/Ten2Base36.java

@@ -0,0 +1,42 @@
+package com.finikes.oc.common;
+
+import java.util.HashMap;
+
+public class Ten2Base36 {
+    // 定义36进制数字
+    private static final String X36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    // 拿到10进制转换36进制的值键对
+    private static HashMap<Integer, Character> tenToBase36 = createMapTenToBase36();
+    // 定义静态进制数
+    private static int BASE = 36;
+
+    private static HashMap<Integer, Character> createMapTenToBase36() {
+        HashMap<Integer, Character> map = new HashMap<Integer, Character>();
+        for (int i = 0; i < X36.length(); i++) {
+            map.put(i, X36.charAt(i));
+        }
+        return map;
+    }
+
+    /**
+     * 用递归来实现10 to 36
+     *
+     * @param iSrc
+     * @return
+     */
+    public static String decimalToBase36(int iSrc) {
+        String result = "";
+        int key;
+        int value;
+
+        key = iSrc / BASE;
+        value = iSrc - key * BASE;
+        if (key != 0) {
+            result = result + decimalToBase36(key);
+        }
+
+        result = result + tenToBase36.get(value).toString();
+
+        return result;
+    }
+}

+ 25 - 6
src/main/java/com/finikes/oc/vote/controller/VoteController.java

@@ -1,6 +1,7 @@
 package com.finikes.oc.vote.controller;
 
 import com.finikes.oc.common.ApiResponse;
+import com.finikes.oc.common.Ten2Base36;
 import com.finikes.oc.vote.dao.ChoiceDao;
 import com.finikes.oc.vote.dao.VoteActivityDao;
 import com.finikes.oc.vote.dto.*;
@@ -10,7 +11,9 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 投票控制器
@@ -142,9 +145,11 @@ public class VoteController {
      * @return 接口返回对象
      */
     @PutMapping("/choice")
-    public ApiResponse<Void> choice(@RequestBody @Valid ChoiceDto dto) {
-        voteService.madeChoice(dto);
-        return ApiResponse.successful();
+    public ApiResponse<SignCodeDTO> choice(@RequestBody @Valid ChoiceDto dto) {
+        int signCode = voteService.madeChoice(dto);
+        SignCodeDTO response = new SignCodeDTO();
+        response.setSignCode(convertSignCode(signCode));
+        return ApiResponse.successful(response);
     }
 
     /**
@@ -154,9 +159,23 @@ public class VoteController {
      * @return 接口返回对象
      */
     @PutMapping("/choice/delegation")
-    public ApiResponse<Void> choiceDelegation(@RequestBody @Valid ChoiceDelegationDto dto) {
-        voteService.madeChoiceDelegation(dto);
-        return ApiResponse.successful();
+    public ApiResponse<SignCodeDTO> choiceDelegation(@RequestBody @Valid ChoiceDelegationDto dto) {
+        int signCode = voteService.madeChoiceDelegation(dto);
+        SignCodeDTO response = new SignCodeDTO();
+        response.setSignCode(convertSignCode(signCode));
+        return ApiResponse.successful(response);
+    }
+
+    private String convertSignCode(int signCode) {
+        String code = Ten2Base36.decimalToBase36(signCode);
+        int diff = 6 - code.length();
+        if (diff > 0) {
+            for (int i = 0; i < diff; i++) {
+                code = "0" + code;
+            }
+        }
+
+        return code;
     }
 
     @GetMapping("/vote/result")

+ 9 - 0
src/main/java/com/finikes/oc/vote/dao/SignCodeDAO.java

@@ -0,0 +1,9 @@
+package com.finikes.oc.vote.dao;
+
+import com.finikes.oc.vote.entity.SignCode;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface SignCodeDAO {
+    int nextCode(SignCode signCode);
+}

+ 13 - 0
src/main/java/com/finikes/oc/vote/dto/SignCodeDTO.java

@@ -0,0 +1,13 @@
+package com.finikes.oc.vote.dto;
+
+public class SignCodeDTO {
+    private String signCode;
+
+    public String getSignCode() {
+        return signCode;
+    }
+
+    public void setSignCode(String signCode) {
+        this.signCode = signCode;
+    }
+}

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

@@ -12,6 +12,15 @@ public class Choice {
     private int assigneeId; // 代理者ID
     private int houseId;
     private String signature;
+    private int signCode;
+
+    public int getSignCode() {
+        return signCode;
+    }
+
+    public void setSignCode(int signCode) {
+        this.signCode = signCode;
+    }
 
     public String getSignature() {
         return signature;

+ 13 - 0
src/main/java/com/finikes/oc/vote/entity/SignCode.java

@@ -0,0 +1,13 @@
+package com.finikes.oc.vote.entity;
+
+public class SignCode {
+    private int code;
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+}

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

@@ -60,14 +60,14 @@ public interface VoteService {
      *
      * @param dto 投票数据传输对象
      */
-    void madeChoice(ChoiceDto dto);
+    int madeChoice(ChoiceDto dto);
 
     /**
      * 代理投票
      *
      * @param dto 代理投票数据传输对象
      */
-    void madeChoiceDelegation(ChoiceDelegationDto dto);
+    int madeChoiceDelegation(ChoiceDelegationDto dto);
 
     VoteResultDto voteResult(Integer voteId);
 }

+ 22 - 11
src/main/java/com/finikes/oc/vote/service/VoteServiceImpl.java

@@ -5,15 +5,9 @@ import com.finikes.oc.management.dao.HouseRelationDAO;
 import com.finikes.oc.management.entity.HouseRelation;
 import com.finikes.oc.common.LockService;
 import com.finikes.oc.common.PassportHelper;
-import com.finikes.oc.vote.dao.ChoiceDao;
-import com.finikes.oc.vote.dao.OptionDao;
-import com.finikes.oc.vote.dao.VoteActivityDao;
-import com.finikes.oc.vote.dao.VoteDao;
+import com.finikes.oc.vote.dao.*;
 import com.finikes.oc.vote.dto.*;
-import com.finikes.oc.vote.entity.Choice;
-import com.finikes.oc.vote.entity.Option;
-import com.finikes.oc.vote.entity.Vote;
-import com.finikes.oc.vote.entity.VoteActivity;
+import com.finikes.oc.vote.entity.*;
 import com.finikes.oc.common.exception.BusinessException;
 import com.finikes.oc.vote.mapper.VoteActivityMapper;
 import com.finikes.oc.vote.mapper.VoteMapper;
@@ -44,14 +38,17 @@ public class VoteServiceImpl implements VoteService {
 
     private final HouseRelationDAO houseRelationDAO;
 
+    private final SignCodeDAO signCodeDAO;
+
     public VoteServiceImpl(ChoiceDao choiceDao, OptionDao optionDao, VoteActivityDao voteActivityDao, VoteDao voteDao,
-                           LockService lockService, HouseRelationDAO houseRelationDAO) {
+                           LockService lockService, HouseRelationDAO houseRelationDAO, SignCodeDAO signCodeDAO) {
         this.choiceDao = choiceDao;
         this.optionDao = optionDao;
         this.voteActivityDao = voteActivityDao;
         this.voteDao = voteDao;
         this.lockService = lockService;
         this.houseRelationDAO = houseRelationDAO;
+        this.signCodeDAO = signCodeDAO;
     }
 
     @Override
@@ -227,7 +224,7 @@ public class VoteServiceImpl implements VoteService {
     }
 
     @Override
-    public void madeChoice(ChoiceDto dto) {
+    public int madeChoice(ChoiceDto dto) {
         Passport passport = PassportHelper.currentPassport();
         if (passport == null) {
             throw new BusinessException("获取登录状态异常");
@@ -284,6 +281,10 @@ public class VoteServiceImpl implements VoteService {
                 throw new BusinessException("此房产已经参与过投票了");
             }
 
+            SignCode signCode = new SignCode();
+            signCodeDAO.nextCode(signCode);
+            int code = signCode.getCode();
+
             Choice choice = new Choice();
             choice.setPassportId(passport.getId());
             choice.setOptionId(dto.getOptionId());
@@ -291,8 +292,11 @@ public class VoteServiceImpl implements VoteService {
             choice.setChooseTime(now);
             choice.setHouseId(dto.getHouseId());
             choice.setSignature(dto.getSignature());
+            choice.setSignCode(code);
 
             choiceDao.insert(choice);
+
+            return code;
         } finally {
             lockService.unlock(voteResourceId, ownerId);
             lockService.unlock(houseResourceId, ownerId);
@@ -300,7 +304,7 @@ public class VoteServiceImpl implements VoteService {
     }
 
     @Override
-    public void madeChoiceDelegation(ChoiceDelegationDto dto) {
+    public int madeChoiceDelegation(ChoiceDelegationDto dto) {
         int houseId = dto.getHouseId();
         Passport passport = PassportHelper.currentPassport();
         if (passport == null) {
@@ -348,6 +352,10 @@ public class VoteServiceImpl implements VoteService {
                 throw new BusinessException("此房产已经参与过投票了");
             }
 
+            SignCode signCode = new SignCode();
+            signCodeDAO.nextCode(new SignCode());
+            int code = signCode.getCode();
+
             Choice choice = new Choice();
             choice.setOptionId(dto.getOptionId());
             choice.setAssigneeId(passport.getId());
@@ -355,8 +363,11 @@ public class VoteServiceImpl implements VoteService {
             choice.setChooseTime(now);
             choice.setHouseId(houseId);
             choice.setSignature(dto.getSignature());
+            choice.setSignCode(code);
 
             choiceDao.insert(choice);
+
+            return code;
         } finally {
             lockService.unlock(voteResourceId, ownerId);
             lockService.unlock(houseResourceId, ownerId);

+ 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)
-        VALUES (#{optionId}, #{chooseTime}, #{proxy}, #{assigneeId}, #{houseId}, #{signature})
+        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>
 
     <update id="update" parameterType="com.finikes.oc.vote.entity.Choice">

+ 20 - 29
src/main/resources/mapper/HouseRelationMapper.xml

@@ -4,8 +4,8 @@
 
     <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
             parameterType="com.finikes.oc.management.entity.HouseRelation">
-        INSERT INTO t_house_relation (passportId, houseId, state, certificateNo, certificateUrl)
-        VALUES (#{passportId}, #{houseId}, #{state}, #{certificateNo}, #{certificateUrl})
+        INSERT INTO t_house_relation (passportId, houseId, state, certificateNo, certificateUrl, name, idNumber)
+        VALUES (#{passportId}, #{houseId}, #{state}, #{certificateNo}, #{certificateUrl}, #{name}, #{idNumber})
     </insert>
 
     <select id="findByPassportAndHouse" resultType="com.finikes.oc.management.entity.HouseRelation">
@@ -18,7 +18,8 @@
     <select id="findByPassport" resultType="com.finikes.oc.management.entity.HouseRelation">
         SELECT passportId, houseId, state, certificateNo, certificateUrl
         FROM t_house_relation
-        WHERE passportId = #{id} AND state = 1
+        WHERE passportId = #{id}
+          AND state = 1
     </select>
 
     <update id="update" parameterType="com.finikes.oc.management.entity.HouseRelation">
@@ -53,36 +54,26 @@
     </select>
 
     <select id="findHousesByNotPassportChoice" resultType="java.lang.Integer">
-        SELECT
-            houseId
-        FROM
-            t_house_relation
-        WHERE
-            passportId = #{passportId}
+        SELECT houseId
+        FROM t_house_relation
+        WHERE passportId = #{passportId}
           AND houseId NOT IN (
-            SELECT
-                house_id
-            FROM
-                t_choice
-            WHERE
-                option_id IN ( SELECT id FROM t_option WHERE vote_id = #{voteId} )
-              AND house_id IN ( SELECT houseId FROM t_house_relation WHERE passportId = #{passportId} AND state = 1 ))
+            SELECT house_id
+            FROM t_choice
+            WHERE option_id IN (SELECT id FROM t_option WHERE vote_id = #{voteId})
+              AND house_id IN (SELECT houseId FROM t_house_relation WHERE passportId = #{passportId}))
+          AND state = 1
     </select>
 
     <select id="findHousesByPassportChoice" resultType="java.lang.Integer">
-        SELECT
-            houseId
-        FROM
-            t_house_relation
-        WHERE
-            passportId = #{passportId}
+        SELECT houseId
+        FROM t_house_relation
+        WHERE passportId = #{passportId}
           AND houseId IN (
-            SELECT
-                house_id
-            FROM
-                t_choice
-            WHERE
-                option_id IN ( SELECT id FROM t_option WHERE vote_id = #{voteId} )
-              AND house_id IN ( SELECT houseId FROM t_house_relation WHERE passportId = #{passportId} AND state = 1 ))
+            SELECT house_id
+            FROM t_choice
+            WHERE option_id IN (SELECT id FROM t_option WHERE vote_id = #{voteId})
+              AND house_id IN (SELECT houseId FROM t_house_relation WHERE passportId = #{passportId}))
+          AND state = 1
     </select>
 </mapper>

+ 9 - 0
src/main/resources/mapper/SignCodeMapper.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.finikes.oc.vote.dao.SignCodeDAO">
+    <insert id="nextCode" useGeneratedKeys="true" keyProperty="code" keyColumn="code"
+            parameterType="com.finikes.oc.vote.entity.SignCode">
+        INSERT INTO t_sign_code ()
+        VALUES ()
+    </insert>
+</mapper>