예제 #1
0
 @RequiresPermissions("cms:article:view")
 @RequestMapping(value = "form")
 public String form(Article article, Model model) {
   // 如果当前传参有子节点,则选择取消传参选择
   if (article.getCategory() != null && StringUtils.isNotBlank(article.getCategory().getId())) {
     List<Category> list =
         categoryService.findByParentId(article.getCategory().getId(), Site.getCurrentSiteId());
     if (list.size() > 0) {
       article.setCategory(null);
     } else {
       article.setCategory(categoryService.get(article.getCategory().getId()));
     }
   }
   article.setArticleData(articleDataService.get(article.getId()));
   //		if (article.getCategory()=null && StringUtils.isNotBlank(article.getCategory().getId())){
   //			Category category = categoryService.get(article.getCategory().getId());
   //		}
   model.addAttribute("contentViewList", getTplContent());
   model.addAttribute("article_DEFAULT_TEMPLATE", Article.DEFAULT_TEMPLATE);
   model.addAttribute("article", article);
   CmsUtils.addViewConfigAttribute(model, article.getCategory());
   return "modules/cms/articleForm";
 }
예제 #2
0
/**
 * 任务Controller 需要使用shiro实现访问控制
 *
 * @author sa
 * @version 2015-12-24
 */
@Controller
@RequestMapping(value = "${frontPath}/task")
public class TaskController extends BaseController {

  @Autowired private PgTaskListService pgTaskListService;
  @Autowired private PgOnlineQuestionService pgOnlineQuestionService;
  @Autowired private PgOfflineQuestionService pgOfflineQuestionService;
  @Autowired private PgAnswerService pgAnswerService;
  @Autowired private PgOfflineTaskService pgOfflineTaskService;
  @Autowired private PgOnlineTaskService pgOnlineTaskService;
  @Autowired PgIntegralService pgIntegralService;

  Site site = CmsUtils.getSite(Site.defaultSiteId());

  /**
   * 无论调用改类的哪一个方法, 此方法都将先被调用
   *
   * @param model
   * @param request
   * @return
   */
  //    @ModelAttribute
  //    public Object get(@RequestParam(required=false) String id) {
  //        if (StringUtils.isNotBlank(id)){
  //            return taskService.get(id);
  //        }else{
  //            return new Task();
  //        }
  //    }

  /**
   * 已发布的任务列表(${frontPath}/task)
   *
   * @param pgTaskList
   * @param request
   * @param response
   * @param model
   * @return
   */
  // @RequiresPermissions("sys:user:view")
  @RequestMapping
  public String index(
      PgTaskList pgTaskList,
      HttpServletRequest request,
      HttpServletResponse response,
      Model model) {
    model.addAttribute("site", site);

    Page<PgTaskList> pgPage =
        pgTaskListService.findPage(new Page<PgTaskList>(request, response), pgTaskList);
    List<PgTaskList> pgTaskListList = pgPage.getList();

    Task task = null;
    List<Task> taskList = new ArrayList<Task>();
    if (null != pgTaskListList && pgTaskListList.size() > 0) {
      for (PgTaskList t : pgTaskListList) {
        task = new Task();
        String uuid = t.getQuestionId();
        // 0:线上任务,1:线下任务
        if (t.getTaskType().equals("0")) {
          PgOnlineQuestion online = pgOnlineQuestionService.get(uuid);
          task.setQuestionId(online.getId());
          task.setName(online.getQuestion());
          task.setType("0");
        } else if (t.getTaskType().equals("1")) {
          PgOfflineQuestion offline = pgOfflineQuestionService.get(uuid);
          task.setQuestionId(offline.getId());
          task.setName(offline.getOfflineTask());
          task.setType("1");
        }
        task.setId(t.getId());
        task.setCreateDate(t.getCreateDate());
        // task.setUserName(systemService.getUser(t.getCreateBy().getId()).getName());
        taskList.add(task);
      }
      Page<Task> page =
          pgTaskListService.findPage(new Page<Task>(request, response), task, taskList);
      model.addAttribute("page", page);
    }

    return "modules/cms/front/themes/" + site.getTheme() + "/taskList";
  }

  /**
   * 任务内容
   *
   * @param taskId 任务ID
   * @param request
   * @param response
   * @param model
   * @return
   */
  // @RequiresPermissions("sys:user:view")
  @RequestMapping(value = "view-{taskId}${urlSuffix}")
  public String context(
      @PathVariable String taskId,
      HttpServletRequest request,
      HttpServletResponse response,
      Model model) {
    model.addAttribute("site", site);
    // "全部任务"对象
    PgTaskList taskList = pgTaskListService.get(taskId);

    if (taskList.getTaskType().equals("0")) {
      // 线上任务
      model.addAttribute("online", pgOnlineQuestionService.get(taskList.getQuestionId()));
      List answerList =
          pgAnswerService.findList(
              new PgAnswer(pgOnlineQuestionService.get(taskList.getQuestionId())));
      model.addAttribute("answerList", answerList);
    } else if (taskList.getTaskType().equals("1")) {
      // 线下任务
      model.addAttribute("offline", pgOfflineQuestionService.get(taskList.getQuestionId()));
    }

    model.addAttribute("taskType", taskList.getTaskType());
    model.addAttribute("taskId", taskId);
    return "modules/cms/front/themes/" + site.getTheme() + "/task";
  }

  /**
   * 开始(接受)线上任务
   *
   * @param taskId
   * @param request
   * @param response
   * @param model
   * @return
   */
  @RequestMapping(value = "onlineBegin-{taskId}${urlSuffix}")
  public void onlineBegin(
      @PathVariable String taskId,
      HttpServletRequest request,
      HttpServletResponse response,
      Model model)
      throws IOException {
    // 点击"接受任务"/"开始任务"时,将任务信息和用户信息关联,并计算开始结束时间,最后返回线下任务的ID
    // 需要判断该任务是不是已经接受过了
    PgOnlineTask pot = new PgOnlineTask(taskId, UserUtils.getUser());
    List<PgOnlineTask> list = pgOnlineTaskService.findList(pot);
    if (null != list && list.size() > 0) { // 任务已经领取
      // 如果已经接受任务,判断任务是否完成
      pot = list.get(0);
      if (pot.getOnlineTaskStatus().equals("0")) {
        // status =0 表示任务未完成
        response.getWriter().print(pot.getId()); // 返回线上任务的ID
      } else {
        response.getWriter().print("exist"); // 返回线上任务的ID
      }
    } else {
      PgTaskList taskList = pgTaskListService.get(taskId);
      PgOnlineQuestion onlineQuestion = pgOnlineQuestionService.get(taskList.getQuestionId());
      Integer timeLength = onlineQuestion.getTimeLength();

      String status = "0"; // 未完成
      User user = UserUtils.getUser();
      // user:用户,taskId:任务ID,status:任务状态 answerBeginDate:任务开始时间,
      // answerEndDate:任务结束时间,answerId:答案ID,answerType:答题方式,rightOrNot:是否正确

      Date answerBeginDate = new Date();
      Date answerEndDate = new Date(answerBeginDate.getTime() + timeLength * 60 * 1000); // 任务结束时间
      PgOnlineTask onlineTask =
          new PgOnlineTask(user, taskId, status, answerBeginDate, answerEndDate, null, null, null);
      pgOnlineTaskService.save(onlineTask);
      response.getWriter().print(onlineTask.getId()); // 返回线上任务的ID
    }
  }

  /**
   * 完成线上任务
   *
   * @param onTaskId 线上任务ID
   * @param request
   * @param response POST不要使用@PathVariable
   * @return
   */
  @RequestMapping(value = "onlinecomplete")
  public String onlineComplete(
      String onTaskId, HttpServletRequest request, HttpServletResponse response) {
    PgOnlineTask onlineTask = pgOnlineTaskService.get(onTaskId); // 线上任务

    String onlineCheck = request.getParameter("answerChk"); // 用户选择的答案(ID)
    PgAnswer answer = pgAnswerService.get(onlineCheck); // 答案对象
    if (answer != null && answer.getRightOrNot().equals("1")) {
      onlineTask.setRightOrNot("1"); // 答案是否正确(1:正确;0:错误)
      // 答题正确给予积分奖励
      PgTaskList taskList = pgTaskListService.get(onlineTask.getTaskId());
      PgOnlineQuestion onlineQuestion = pgOnlineQuestionService.get(taskList.getQuestionId());
      Integer integral = onlineQuestion.getBaseIntegral();
      // 构造方法PgIntegral(String integralDate,String integralType,Integer integral,User user)
      // 积分类型(integralType):0线上任务/1线下任务/2签到/3日记
      PgIntegral pi = new PgIntegral(DateUtils.getDateTime(), "0", integral, UserUtils.getUser());
      pi.setRemarks(onlineQuestion.getQuestion());
      pgIntegralService.save(pi);

    } else {
      onlineTask.setRightOrNot("0"); // 答案是否正确(1:正确;0:错误)
    }
    onlineTask.setOnlineTaskStatus("1"); // 状态:1 表示已完成
    onlineTask.setAnswerId(onlineCheck); // 用户选择的答案(ID)
    String answerType =
        ""; // 答题方式
            // 未完成***********************************-------------------------------------------

    // 更新"是否正确"
    pgOnlineTaskService.save(onlineTask); // 更新会员在线任务
    return "redirect:" + Global.getFrontPath() + "/task";
  }

  /**
   * 开始(接受)线下任务
   *
   * @param taskId
   * @param request
   * @param response
   * @param model
   * @return
   */
  // @RequiresPermissions("sys:user:edit")
  @RequestMapping(value = "begin-{taskId}${urlSuffix}")
  public void beginTask(
      @PathVariable String taskId,
      HttpServletRequest request,
      HttpServletResponse response,
      Model model)
      throws IOException {
    // 需要判断该任务是不是已经接受过了
    PgOfflineTask off = new PgOfflineTask(UserUtils.getUser(), taskId, null);
    List<PgOfflineTask> list = pgOfflineTaskService.findList(off);
    if (null != list && list.size() > 0) { // 任务已经领取
      // 如果已经接受任务,判断任务是否完成
      off = list.get(0);
      if (off.getOfflineStatus().equals("0")) {
        // status =0 表示任务未完成
        response.getWriter().print(off.getId()); // 返回线下任务的ID
      } else {
        response.getWriter().print("exist"); // 返回线下任务的ID
      }

    } else {
      // 点击"接受任务"/"开始任务"时,将任务信息和用户信息关联,并计算开始结束时间,最后返回线下任务的ID
      String status = "0"; // 未完成
      User user = UserUtils.getUser();
      PgOfflineTask offlineTask = new PgOfflineTask(user, taskId, status);
      pgOfflineTaskService.save(offlineTask);
      response.getWriter().print(offlineTask.getId()); // 返回线下任务的ID
    }
  }

  /**
   * 完成线下任务
   *
   * @param offTaskId
   * @param request
   * @param response
   * @param model
   * @return
   */
  @RequestMapping(value = "offlinecomplete")
  public String offlinecomplete(
      String offTaskId, HttpServletRequest request, HttpServletResponse response, Model model) {
    model.addAttribute("site", site);
    String feel = request.getParameter("feel");
    PgOfflineTask offlineTask = pgOfflineTaskService.get(offTaskId);
    offlineTask.setOfflineFeel(feel);
    offlineTask.setOfflineStatus("1"); // 状态:1 表示已完成
    // 保存线下任务
    pgOfflineTaskService.save(offlineTask);

    // 完成线下任务给予积分奖励
    PgTaskList taskList = pgTaskListService.get(offlineTask.getTaskId());
    PgOfflineQuestion offlineQuestion = pgOfflineQuestionService.get(taskList.getQuestionId());
    Integer integral = offlineQuestion.getBaseIntegral();
    // 构造方法PgIntegral(String integralDate,String integralType,Integer integral,User user)
    // 积分类型(integralType):0线上任务/1线下任务/2签到/3日记
    PgIntegral pi = new PgIntegral(DateUtils.getDateTime(), "1", integral, UserUtils.getUser());
    pi.setRemarks(offlineQuestion.getOfflineTask());
    // 累计线下任务积分
    pgIntegralService.save(pi);

    return "redirect:" + Global.getFrontPath() + "/task";
  }

  /**
   * 我接受的任务列表
   *
   * @param taskType
   * @param request
   * @param response
   * @param model
   * @return
   */
  @RequestMapping(value = "list-{taskType}${urlSuffix}")
  public String taskList(
      @PathVariable String taskType,
      PgOnlineTask pgOnlineTask,
      PgOfflineTask pgOfflineTask,
      HttpServletRequest request,
      HttpServletResponse response,
      Model model) {
    model.addAttribute("site", site);
    // 0:线上任务,1:线下任务
    Page p = null;
    Task task = null;
    List<Task> taskList = new ArrayList<Task>();

    // 0:线上任务
    if (taskType.equals("0") || taskType.equals("all")) {
      pgOnlineTask.setUser(UserUtils.getUser());
      p = pgOnlineTaskService.findPage(new Page<PgOnlineTask>(request, response), pgOnlineTask);
      List<PgOnlineTask> list = p.getList();
      if (null != list && list.size() > 0) {
        for (PgOnlineTask on : list) {
          PgOnlineQuestion onlineQuestion =
              pgOnlineQuestionService.get(pgTaskListService.get(on.getTaskId()).getQuestionId());
          String id = on.getTaskId(); // 任务ID
          String questionId = onlineQuestion.getId();
          String name = onlineQuestion.getQuestion();
          String type = "0";
          String status = on.getOnlineTaskStatus();
          String time = DateUtils.formatDateTime(on.getCreateDate());
          String userName = UserUtils.getUser().getName();
          task = new Task(id, questionId, name, type, status, time, userName);
          taskList.add(task);
        }
      }
    }
    // 1:线下任务
    if (taskType.equals("1") || taskType.equals("all")) {
      pgOfflineTask.setUser(UserUtils.getUser());
      p = pgOfflineTaskService.findPage(new Page<PgOfflineTask>(request, response), pgOfflineTask);
      List<PgOfflineTask> list = p.getList();
      if (null != list && list.size() > 0) {
        for (PgOfflineTask off : list) {
          PgOfflineQuestion offlineQuestion =
              pgOfflineQuestionService.get(pgTaskListService.get(off.getTaskId()).getQuestionId());
          // public Task(String questionId,String name,String type,String time)
          String id = off.getTaskId();
          String questionId = offlineQuestion.getId();
          String name = offlineQuestion.getOfflineTask();
          String type = "1";
          String status = off.getOfflineStatus();
          String time = DateUtils.formatDateTime(off.getCreateDate());
          String userName = UserUtils.getUser().getName();
          task = new Task(id, questionId, name, type, status, time, userName);
          taskList.add(task);
        }
      }
    }
    if (taskList.size() != 0) {
      Page<Task> page =
          pgTaskListService.findPage(new Page<Task>(request, response), task, taskList);
      model.addAttribute("page", page);
    }
    return "modules/cms/front/themes/" + site.getTheme() + "/myTaskList";
  }

  @RequestMapping(value = "/mytask/view-{status}-{questionId}${urlSuffix}")
  public String myTask(
      @PathVariable String status,
      @PathVariable String questionId,
      HttpServletRequest request,
      HttpServletResponse response,
      Model model) {
    model.addAttribute("site", site);
    model.addAttribute("status", status);
    if (status.equals("0")) {
      // 线上任务
      PgOnlineQuestion online = pgOnlineQuestionService.get(questionId);
      model.addAttribute("online", online);
    } else if (status.equals("1")) {
      // 线下任务
      model.addAttribute("offline", pgOfflineQuestionService.get(questionId));
    }
    return "modules/cms/front/themes/" + site.getTheme() + "/myTask";
  }
}