123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- package com.qxgmat.service.inline;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.Tools;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.constants.enums.status.AnswerStatus;
- import com.qxgmat.data.constants.enums.status.DirectionStatus;
- import com.qxgmat.data.constants.enums.user.MoneyRange;
- import com.qxgmat.data.dao.UserAskCourseMapper;
- import com.qxgmat.data.dao.entity.UserAskCourse;
- import com.qxgmat.data.relation.UserAskCourseRelationMapper;
- import com.qxgmat.data.relation.entity.UserAskCourseStatRelation;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.*;
- @Service
- public class UserAskCourseService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(UserAskCourseService.class);
- @Resource
- private UserAskCourseMapper userAskCourseMapper;
- @Resource
- private UserAskCourseRelationMapper userAskCourseRelationMapper;
- private Map<String, String> adminMap = new HashMap<String, String>(){{
- put("", "uac");
- }};
- public Page<UserAskCourse> listAdmin(int page, int size, Integer structId, Number courseId, AnswerStatus status, Integer showStatus, Integer userId, MoneyRange moneyRange, String startTime, String endTime, String order, DirectionStatus direction){
- Integer statusIndex = status != null ? status.index : null;
- Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
- Integer min = moneyRange != null ? moneyRange.min : null;
- if(order == null || order.isEmpty()){
- order = "id";
- }
- if(adminMap.containsKey(order)){
- order = adminMap.get(order)+".`"+Tools.underscoreName(order)+"`";
- }else{
- order = adminMap.get("")+".`"+Tools.underscoreName(order)+"`";
- }
- if (direction == null){
- direction = DirectionStatus.DESC;
- }
- String finalOrder = order;
- DirectionStatus finalDirection = direction;
- Page<UserAskCourse> p = page(
- ()-> userAskCourseRelationMapper.listAdmin(structId, courseId, statusIndex, showStatus, userId, min, max, startTime, endTime, finalOrder, finalDirection.key)
- , page, size);
- Collection ids = Transform.getIds(p, UserAskCourse.class, "id");
- Transform.replace(p, select(ids), UserAskCourse.class, "id");
- return p;
- }
- public List<UserAskCourse> listByCoursePosition(Number courseNoId, Integer position, Boolean showStatus){
- Example example = new Example(UserAskCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("courseNoId", courseNoId)
- .andEqualTo("position", position)
- );
- if (showStatus != null)
- example.and(
- example.createCriteria()
- .andEqualTo("showStatus", showStatus?1:0)
- .andEqualTo("answerStatus", 1)
- );
- example.orderBy("sort").desc();
- return select(userAskCourseMapper, example);
- }
- public Page<UserAskCourse> listByCourse(int page, int size, String keyword, Integer courseId, Integer courseNoId, Integer position, Boolean showStatus, String order, DirectionStatus direction){
- Example example = new Example(UserAskCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("courseId", courseId)
- );
- if(courseNoId != null)
- example.and(
- example.createCriteria()
- .andEqualTo("courseNoId", courseNoId)
- );
- if(position != null)
- example.and(
- example.createCriteria()
- .andEqualTo("position", position)
- );
- if (keyword != null)
- example.and(
- example.createCriteria()
- .orLike("content", "%"+keyword+"%")
- .orLike("answer", "%"+keyword+"%")
- );
- if (showStatus != null)
- example.and(
- example.createCriteria()
- .andEqualTo("showStatus", showStatus?1:0)
- .andEqualTo("answerStatus", 1)
- );
- if(order == null || order.isEmpty()) order = "sort";
- switch(direction){
- case ASC:
- example.orderBy(order).asc();
- break;
- case DESC:
- default:
- example.orderBy(order).desc();
- }
- return select(userAskCourseMapper, example, page, size);
- }
- public Page<UserAskCourse> listByUser(int page, int size, String keyword, Integer userId, Integer courseId, Integer courseNoId, Integer position, AnswerStatus answerStatus, String order, DirectionStatus direction){
- Example example = new Example(UserAskCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("courseId", courseId)
- );
- if(courseNoId != null)
- example.and(
- example.createCriteria()
- .andEqualTo("courseNoId", courseNoId)
- );
- if (position != null)
- example.and(
- example.createCriteria()
- .andEqualTo("position", position)
- );
- if (answerStatus != null)
- example.and(
- example.createCriteria()
- .andEqualTo("answerStatus", answerStatus.index)
- );
- if (keyword != null)
- example.and(
- example.createCriteria()
- .orLike("content", "%"+keyword+"%")
- .orLike("answer", "%"+keyword+"%")
- );
- if(order == null || order.isEmpty()) order = "id";
- switch(direction){
- case ASC:
- example.orderBy(order).asc();
- break;
- case DESC:
- default:
- example.orderBy(order).desc();
- }
- return select(userAskCourseMapper, example, page, size);
- }
- public List<UserAskCourse> listByRecord(Number recordId){
- Example example = new Example(UserAskCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("recordId", recordId)
- .andNotEqualTo("answerStatus", AnswerStatus.IGNORE.index)
- );
- example.orderBy("id").asc();
- return select(userAskCourseMapper, example);
- }
- /**
- * 根据列表顺序全部更新排序:从大到小
- * @param ids
- */
- @Transactional
- public void updateOrder(Integer[] ids){
- int sort = ids.length;
- List<UserAskCourse> userAskCourseList = select(userAskCourseMapper, ids);
- Map userAskCourseMap = Transform.getMap(userAskCourseList, UserAskCourse.class, "id");
- for (Integer id : ids){
- sort -= 1;
- UserAskCourse userAskCourse = (UserAskCourse)userAskCourseMap.get(id);
- if (userAskCourse == null) continue;
- if (userAskCourse.getSort() == sort) continue;
- update(userAskCourseMapper, UserAskCourse.builder()
- .id(id)
- .sort(sort)
- .showStatus(1)
- .build()
- );
- }
- }
- public List<UserAskCourse> getByRecordId(Integer recordId){
- Example example = new Example(UserAskCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("recordId", recordId)
- );
- return select(userAskCourseMapper, example);
- }
- /**
- * 获取课程记录分组列表
- * @param recordIds
- * @return
- */
- public Map<Object, Collection<UserAskCourse>> groupByRecordId(Collection recordIds){
- Map<Object, Collection<UserAskCourse>> relationMap = new HashMap<>();
- if(recordIds == null || recordIds.size() == 0) return relationMap;
- Example example = new Example(UserAskCourse.class);
- example.and(
- example.createCriteria()
- .andIn("recordId", recordIds)
- );
- List<UserAskCourse> nos = select(userAskCourseMapper, example);
- Collection<UserAskCourse> list;
- for(UserAskCourse no : nos){
- if (!relationMap.containsKey(no.getRecordId())){
- list = new ArrayList<>();
- relationMap.put(no.getRecordId(), list);
- }else{
- list = relationMap.get(no.getRecordId());
- }
- list.add(no);
- }
- return relationMap;
- }
- /**
- * 计算课程的汇总信息
- * @param courseIds
- * @return
- */
- public List<UserAskCourseStatRelation> statGroupCourse(Collection courseIds, Integer showStatus){
- return userAskCourseRelationMapper.statGroupCourse(courseIds, showStatus);
- }
- public UserAskCourseStatRelation statCourse(Integer courseId, Integer showStatus){
- List<UserAskCourseStatRelation> relation = userAskCourseRelationMapper.statGroupCourse(new ArrayList(){{add(courseId);}}, showStatus);
- if (relation != null && relation.size() > 0){
- return relation.get(0);
- }else{
- return null;
- }
- }
- /**
- * 累加访问记录
- */
- public void accumulation(Integer id, int view){
- userAskCourseRelationMapper.accumulation(id, view);
- }
- public UserAskCourse add(UserAskCourse message){
- int result = insert(userAskCourseMapper, message);
- message = one(userAskCourseMapper, message.getId());
- if(message == null){
- throw new SystemException("提问添加失败");
- }
- return message;
- }
- public UserAskCourse edit(UserAskCourse message){
- UserAskCourse in = one(userAskCourseMapper, message.getId());
- if(in == null){
- throw new ParameterException("提问不存在");
- }
- int result = update(userAskCourseMapper, message);
- return message;
- }
- public boolean delete(Number id){
- UserAskCourse in = one(userAskCourseMapper, id);
- if(in == null){
- throw new ParameterException("提问不存在");
- }
- int result = delete(userAskCourseMapper, id);
- return result > 0;
- }
- public UserAskCourse get(Number id){
- UserAskCourse in = one(userAskCourseMapper, id);
- if(in == null){
- throw new ParameterException("提问不存在");
- }
- return in;
- }
- public Page<UserAskCourse> select(int page, int pageSize){
- return select(userAskCourseMapper, page, pageSize);
- }
- public Page<UserAskCourse> select(Integer[] ids){
- return page(()->select(userAskCourseMapper, ids), 1, ids.length);
- }
- public List<UserAskCourse> select(Collection ids){
- return select(userAskCourseMapper, ids);
- }
- }
|