功能实现:该功能可分为四个点,一是文章的主体的添加,二是问题的添加,三是问题选项的添加,四是选项与用户的关系(确定用户是否投票);问题和选项添加与编辑时都是可增删改的,在数据库创建时,就需要将文章主体,问题,选项,用户与选项的关系各创建一个表,通过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;
}
posted on 2019-06-21 14:28  lazyli  阅读(5113)  评论(0编辑  收藏  举报