package com.yunzhi.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.demo.common.BaseController;
import com.yunzhi.demo.common.Constants;
import com.yunzhi.demo.common.ResponseBean;
import com.yunzhi.demo.common.StringUtils;
import com.yunzhi.demo.entity.*;
import com.yunzhi.demo.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 文章表 前端控制器
*
*
* @author yansen
* @since 2021-04-15
*/
@Api(tags = "文章表")
@RestController
@RequestMapping("/")
public class TaPostController extends BaseController {
private final Logger logger = LoggerFactory.getLogger(TaPostController.class);
@Autowired
public ITaPostService iTaPostService;
@Autowired
public ITdPostTypeService iTdPostTypeService;
@Autowired
public ITaPostDataService iTaPostDataService;
@Autowired
public ITaPostTestService iTaPostTestService;
@Autowired
public ITaPostSaveService iTaPostSaveService;
@Autowired
public ITaShareSettingService iTaShareSettingService;
/**
* 分页查询列表
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value="/{client}/post",method= RequestMethod.GET)
@ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
public ResponseBean taPostList(@ApiParam(value = "客户端", allowableValues = "admin,ma") @PathVariable String client,
@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
@ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
@ApiParam("名称") @RequestParam(value = "name", required = false) String name,
@ApiParam("分类") @RequestParam(value = "typeId", required = false) String typeId,
@ApiParam("状态") @RequestParam(value = "status", required = false) Integer status) throws Exception{
IPage pg = new Page<>(pageNum, pageSize);
QueryWrapper queryWrapper = new QueryWrapper()
.like(!StringUtils.isEmpty(name), "name", name)
.eq(!StringUtils.isEmpty(typeId), "type_id", typeId)
.eq(null != status, "status", status)
.gt("status", Constants.STATUS_DELETED)
.orderByDesc("weight")
.orderByDesc("create_date");
IPage result = iTaPostService.page(pg, queryWrapper);
return ResponseBean.success(result);
}
/**
* 保存对象
* @param taPost 实体对象
* @return
*/
@RequestMapping(value="/admin/post",method= RequestMethod.POST)
@ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
public ResponseBean taPostAdd(@ApiParam("保存内容") @RequestBody TaPost taPost) throws Exception{
TdPostType postType = iTdPostTypeService.getById(taPost.getTypeId());
if (null != postType) {
taPost.setTypeName(postType.getName());
}
if (iTaPostService.save(taPost)){
TaPostData postData = new TaPostData();
postData.setPostId(taPost.getPostId());
iTaPostDataService.save(postData);
return ResponseBean.success(iTaPostService.getById(taPost.getPostId()));
}else {
return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
}
}
/**
* 根据id删除对象
* @param id 实体ID
*/
@RequestMapping(value="/admin/post/{id}", method= RequestMethod.DELETE)
@ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
public ResponseBean taPostDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception {
TaPost taPost = iTaPostService.getById(id);
if (null == taPost || Constants.STATUS_DELETED.equals(taPost.getStatus())) {
return ResponseBean.success("success");
}
taPost.setStatus(Constants.STATUS_DELETED);
if(iTaPostService.updateById(taPost)){
return ResponseBean.success("success");
}else {
return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
}
}
/**
* 修改对象
* @param id 实体ID
* @param taPost 实体对象
* @return
*/
@RequestMapping(value="/admin/post/{id}",method= RequestMethod.PUT)
@ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
public ResponseBean taPostUpdate(@ApiParam("对象ID") @PathVariable String id,
@ApiParam("更新内容") @RequestBody TaPost taPost) throws Exception{
TdPostType postType = iTdPostTypeService.getById(taPost.getTypeId());
if (null != postType) {
taPost.setTypeName(postType.getName());
}
if (iTaPostService.updateById(taPost)){
return ResponseBean.success(iTaPostService.getById(id));
}else {
return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
}
}
/**
* 根据id查询文章详情
* @param id 实体ID
*/
@RequestMapping(value="/{client}/post/{id}",method= RequestMethod.GET)
@ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = TaPost.class)
public ResponseBean taPostGet(@ApiParam(value = "客户端", allowableValues = "admin,ma") @PathVariable String client,
@ApiParam("文章ID") @PathVariable String id) throws Exception{
// 获取文章信息
TaPost taPost = iTaPostService.getById(id);
if (null == taPost || Constants.STATUS_DELETED.equals(taPost.getStatus())) {
throw new Exception("文章内容不存在");
}
// 获取统计信息
TaPostData taPostData = iTaPostDataService.getById(id);
taPost.setPostData(taPostData);
// 分享信息
TaShareSetting taShareSetting = iTaShareSettingService.getSettingBy(Constants.RESOURCE_TYPE_POST, id);
if (!"admin".equals(client)) {
if (Constants.STATUS_NORMAL.equals(taShareSetting.getStatus())) {
taPost.setShareSetting(taShareSetting);
}
} else {
taPost.setShareSetting(taShareSetting);
}
// 获取试题
List postTestList = new ArrayList<>();
if ("admin".equals(client)) {
postTestList = iTaPostTestService.getListByPostId(id);
} else {
// 当前人员是否收藏过
TaPerson taPerson = getCurrentPerson();
boolean saved = iTaPostSaveService.checkSaved(taPerson.getPersonId(), id);
taPost.setIsSaved(saved);
// 埋点数据
iTaPostDataService.recordBy(id, taPerson);
// 随机取题目
if (null != taPost.getAnswerNum() && taPost.getAnswerNum() > 0) {
for (int i = 0; i < taPost.getAnswerNum(); i++) {
TaPostTest taPostTest = iTaPostTestService.getRandRowBy(id);
taPostTest.setCorrectAnswers(null);
postTestList.add(taPostTest);
}
}
}
taPost.setPostTestList(postTestList);
return ResponseBean.success(taPost);
}
}