微信小程序投票功能


功能實現:該功能可分為四個點,一是文章的主體的添加,二是問題的添加,三是問題選項的添加,四是選項與用戶的關系(確定用戶是否投票);問題和選項添加與編輯時都是可增刪改的,在數據庫創建時,就需要將文章主體,問題,選項,用戶與選項的關系各創建一個表,通過id來關聯它們之間的關系。頁面向后台是以json數據格式傳遞,后台接收json數據,根據數據模型創建對應的Java對象。傳遞是以文章為主體,改文章對象需要嵌套問題列表,問題對象需要嵌套選項列表。

前端傳遞的json對象格式:

{ type:"2", title:"母親節", url:"", content:"關於母親節的相關內容", endTime:"", questionModels:[ { title:"對母親的看法", type:2, options:[ {option:"她很善良"}, {option:"她很啰嗦"} ] } ] }

-----

后台控制層接收json數據:

@RequestMapping("/voteShare")
@ResponseBody
public AjaxResult vote(@RequestBody String string){
System.out.println("接收投票的參數:"+string);
JSONObject jsonObject = JSONObject.parseObject(string);
ShareExtend shareExtend = JSON.toJavaObject(jsonObject,ShareExtend.class );
return userShareVoteService.checkIsVote(shareExtend);
}
文章的實體類:
public class ShareExtend {

    private Integer id;
    private Integer type;
    private String title;
    private String url;
    private String content;
    private String time;
    private String endTime;
  private String phone; private List<QuestionModel> questionModels;//問題 public List<QuestionModel> getQuestionModels() { return questionModels; } public void setQuestionModels(List<QuestionModel> questionModels) { this.questionModels = questionModels; } //若增加投票功能,需擴展字段 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; }
  public String getPhone()

}

  問題的實體類:

public class QuestionModel{

private Integer id; //標識符
private String title; //標題
private Integer type; //1-單選,2-多選
private Integer shareId; //分享主內容Id

private List<Option> options; //選項


public Integer getShareId() {
return shareId;
}

public void setShareId(Integer shareId) {
this.shareId = shareId;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Integer getType() {
return type;
}

public void setType(Integer type) {
this.type = type;
}

/*public String getOption() {
return option;
}

public void setOption(String option) {
this.option = option;
}*/

public List<Option> getOptions() {
return options;
}

public void setOptions(List<Option> options) {
this.options = options;
}
}
選項的實體類
public class Option {

private Integer id; //標識符
private String option; //選項
private Integer ticket; //票數
private Integer questionId; //選項問題id

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getTicket() {
return ticket;
}

public void setTicket(Integer ticket) {
this.ticket = ticket;
}

public Integer getQuestionId() {
return questionId;
}

public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}

public String getOption() {
return option;
}

public void setOption(String option) {
this.option = option;
}
}
----------------
public class UserShareVote {

private Integer id;
private String phone;
private Integer shareId;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public Integer getShareId() {
return shareId;
}

public void setShareId(Integer shareId) {
this.shareId = shareId;
}
}
--------------
添加業務Controller,Service方法
@RequestMapping("/addShare")
@ResponseBody
public AjaxResult add(@RequestBody String string){
//將json字符串轉換成Java對象
JSONObject jsonObject = JSONObject.parseObject(string);
Share share = JSON.toJavaObject(jsonObject,Share.class );
int result = shareService.add(share);
if(result > 0){
return AjaxResult.ok();
}else {
return AjaxResult.build("false", null);
}
}
Service方法主要將獲取的數據提取,然后添加到數據庫中。
public int add(Share share) {
//補全發布時間:
share.setTime(DateUtils.dateToString2(new Date()));
//返回添加后的Id,綁定對應的問題;
int result = shareMapper.add(share);
//如果類型是type是投票
if(share.getType() == 2){
int shareId = share.getId();
//問題添加完,返回問題的ID,綁定選項;
List<QuestionModel> questionModels = share.getQuestionModels();
for(QuestionModel questionModel:questionModels){
questionModel.setShareId(shareId);
//添加問題
questionModelMapper.add(questionModel);
//得到問題的id
int questionId = questionModel.getId();
//獲取選項列表
List<Option> options = questionModel.getOptions();
for(Option option : options){
option.setQuestionId(questionId);
option.setTicket(0);//初始化票數為0
//添加選項
optionMapper.add(option);
}
}
}
更新操作:
controller:
/**
* 跟新悅分享記錄數據
* @param
* @return
*/
@RequestMapping("updateShare")
@ResponseBody
public AjaxResult update(@RequestBody String string){
System.out.println("接收的參數:"+ string );
JSONObject jsonObject = JSONObject.parseObject(string);
Share share = JSON.toJavaObject(jsonObject,Share.class );
int result = shareService.update(share);
if(result > 0){
return AjaxResult.ok();
}else{
return AjaxResult.build("false",null );
}
}
Service方法:需要檢查問題有沒有修改,增加,刪除;對每一個問題還需要檢查其中的選項有沒有增加,修改,刪除;
/**
* 更新
* @param share
* @return
*/
public int update(Share share) {
//根據id獲取所要修改的記錄
Share share1 = getShareById(share.getId(),share.getType());
share1.setType(share.getType());
share1.setTitle(share.getTitle());
share1.setUrl(share.getUrl());
share1.setContent(share.getContent());
share1.setTime(DateUtils.dateToString2(new Date()));//重新修改發布時間
share1.setEndTime(share.getEndTime());
//若類型type為2;則刪除原先的問題,選項列表;(是動態修改,無法確定原來的問題、選項個數)
if(share.getType() == 2){
Integer shareId = share.getId();
//修改后問題列表對象
List<QuestionModel> aftQuestionModels = share.getQuestionModels();
//原先的列表對象
List<QuestionModel> befQuestionModels = share1.getQuestionModels();
//處理修改后新增和原先存在的問題
for(QuestionModel aftQuestion : aftQuestionModels){
boolean flag = false;
for(QuestionModel befQuestion : befQuestionModels){
if(aftQuestion.getId() != null){
if(aftQuestion.getId().intValue() == befQuestion.getId().intValue()){
//判斷問題中的選項
//如果找到相同的,設置flag為true;跳出循環
questionModelMapper.update(aftQuestion);
//更新選項
List<Option> aftOptions = aftQuestion.getOptions();
List<Option> befOptions = befQuestion.getOptions();
//處理修改后新增和原先存在的選項
for(Option aftOption : aftOptions){
boolean flag2 = false;
for(Option befOption : befOptions){
//新增的選項沒有id
if(aftOption.getId() != null){
if(aftOption.getId().intValue() == befOption.getId().intValue()){
//更新選項,
befOption.setOption(aftOption.getOption());
optionMapper.update(befOption);
//跳出循環
flag2 = true;
break;
}
}
}
//沒有匹配到選項,是新增選項
if(!flag2){
//添加選項
aftOption.setQuestionId(aftQuestion.getId());
aftOption.setTicket(0);
optionMapper.add(aftOption);
}
}
//刪除原先存在,修改后不存在的選項
for(Option befOption : befOptions){
boolean flag2 = false;
for(Option aftOption : aftOptions){
if(aftOption.getId() != null){
if(befOption.getId().intValue() == aftOption.getId().intValue()){
flag2 = true;
break;
}
}

}
//刪除沒有匹配到的選項
if(!flag2){
optionMapper.deleteById(befOption.getId());
}
}
flag = true;
break;
}
}
}
//沒有匹配到,則新增問題和選項
if(!flag){
aftQuestion.setShareId(shareId);
//添加問題
questionModelMapper.add(aftQuestion);
//得到問題的id
int questionId = aftQuestion.getId();
//獲取選項列表
List<Option> options = aftQuestion.getOptions();
for(Option option : options){
option.setQuestionId(questionId);
option.setTicket(0);//初始化票數為0
//添加選項
optionMapper.add(option);
}
}

}
//刪除原先存在,修改后不存在的問題和選項
for(QuestionModel befQuestion : befQuestionModels){
boolean flag = false;
for(QuestionModel aftQuestion : aftQuestionModels){
if(aftQuestion.getId() != null){
if(befQuestion.getId().intValue() == aftQuestion.getId().intValue()){
flag = true;
break;
}
}

}
//沒有匹配到,刪除問題記錄
if(!flag){
//刪除問題下面的選項
optionMapper.delete(befQuestion.getId());
//刪除問題,
questionModelMapper.deleteById(befQuestion.getId());

}
}

}

int result = shareMapper.update(share1);
return result;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM