TKzyCourseServiceImpl.java 11.5 KB
package com.server.web.common.service.Impl;

import com.server.utils.weixin.WeixinBaseUtil;
import com.server.utils.weixin.bean.AccessTokenBean;
import com.server.web.common.mapper.TKzyCommentMapper;
import com.server.web.common.mapper.TKzyCourseMapper;
import com.server.web.common.mapper.TKzyUserMapper;
import com.server.web.common.mapping.CommentMapper;
import com.server.web.common.mapping.CourseMapper;
import com.server.web.common.model.TKzyComment;
import com.server.web.common.model.TKzyCourse;
import com.server.web.common.service.TKzyCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.JedisPool;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 课程相关
 * Created by weiwenfu@163.com on 2018/12/5 下午 1:45.
 */
@Service
public class TKzyCourseServiceImpl implements TKzyCourseService {
    @Autowired
    private TKzyCourseMapper tKzyCourseMapper;
    @Autowired
    private TKzyCommentMapper tKzyCommentMapper;
    @Autowired
    private TKzyUserMapper tKzyUserMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CourseMapper courseMapper;

    private static final int ERCORD_WIDTH = 600;
    private static final int ERCORD_HEIGHT = 600;

    @Value("${remoteFileUrl}")
    private String remoteFileUrl;
    @Value("${upyun.baseDir}")
    private String upyunBaseDir;
    @Value("${upyun.bucket}")
    private String upyunBucket;
    @Value("${upyun.username}")
    private String upyunUsername;
    @Value("${upyun.password}")
    private String upyunPassword;

    @Value("${wx.http.url}")
    private String WX_HTTP_URL;
    @Value("${wx.app.secret}")
    private String appSecret;
    @Value("${wx.app.id}")
    private String appId;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 热门课程
     *
     * @return
     */
    @Override
    public Map<String, Object> hotCourse() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "1");
        resultMap.put("message", "");
        Map<String, Object> param = new HashMap<>();
        param.put("limit", 4);
        List<Map<String, Object>> list = courseMapper.hotCourse(param);
        resultMap.put("data", list);
        return resultMap;
    }

    /**
     * 直播课
     *
     * @return
     */
    @Override
    public Map<String, Object> livingCourse() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "1");
        resultMap.put("message", "");
        Map<String, Object> param = new HashMap<>();
        List<Map<String, Object>> list = courseMapper.livingCourse(param);
        resultMap.put("data", list);
        return resultMap;
    }

    /**
     * 录播课
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> recordCourse(Long typeId, Integer pageNo, Integer pageSize) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "1");
        resultMap.put("message", "");
        if (pageNo == null || pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("start", (pageNo - 1) * pageSize);
        param.put("pageSize", pageSize);
//        param.put("typeId", typeId);
        List<Map<String, Object>> list = courseMapper.recordCourse(param);
        resultMap.put("data", list);
        return resultMap;
    }

    @Override
    public Map<String, Object> livingCourseDetail(Long courseId, Long userId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "0");
        resultMap.put("message", "");
        if (courseId == null || courseId <= 0l) {
            resultMap.put("message", "课程ID不能为空或有误~");
            return resultMap;
        }
        TKzyCourse tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
        if (tKzyCourse == null) {
            resultMap.put("message", "未查到该课程信息,请确认~");
            return resultMap;
        }
        resultMap.put("status", "1");
        Map<String, Object> courseMap = new HashMap<>();
        courseMap.put("courseName", tKzyCourse.getCourseName());
        courseMap.put("courseIntroduce", tKzyCourse.getCourseIntroduce());
        courseMap.put("startDt", tKzyCourse.getStartDt());
        courseMap.put("endDt", tKzyCourse.getEndDt());
        courseMap.put("picUrl", tKzyCourse.getPicUrl());
        courseMap.put("liveUrl", tKzyCourse.getLiveUrl());
        courseMap.put("courseType", tKzyCourse.getCourseType());

        resultMap.put("data", courseMap);
        return resultMap;
    }

    @Override
    public Map<String, Object> recordCourseDetail(Long courseId, Long userId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "0");
        resultMap.put("message", "");
        if (courseId == null || courseId <= 0l) {
            resultMap.put("message", "课程ID不能为空或有误~");
            return resultMap;
        }
        TKzyCourse tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
        if (tKzyCourse == null) {
            resultMap.put("message", "未查到该课程信息,请确认~");
            return resultMap;
        }
        resultMap.put("status", "1");
        Map<String, Object> courseMap = new HashMap<>();
        courseMap.put("courseName", tKzyCourse.getCourseName());
        courseMap.put("courseIntroduce", tKzyCourse.getCourseIntroduce());
        courseMap.put("startDt", tKzyCourse.getStartDt());
        courseMap.put("endDt", tKzyCourse.getEndDt());
        courseMap.put("picUrl", tKzyCourse.getPicUrl());
        courseMap.put("liveUrl", tKzyCourse.getLiveUrl());

        resultMap.put("data", courseMap);
        return resultMap;
    }

    /**
     * 评论列表
     *
     * @param courseId
     * @return
     */
    @Override
    public Map<String, Object> commentList(Long courseId, Integer pageNo, Integer pageSize) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "1");
        resultMap.put("message", "");
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("courseId", courseId);
        param.put("start", (pageNo - 1) * pageSize);
        param.put("pageSize", pageSize);
        List<Map<String, Object>> list = commentMapper.commentList(param);
        Integer count = commentMapper.commentListTotal(param);
        dataMap.put("list", list);
        dataMap.put("count", count);
        resultMap.put("data", dataMap);
        return resultMap;
    }


    public String getAccessToken(String appId, String appSecret) {
        if (org.springframework.util.StringUtils.hasText(appSecret)) {
            String json = "";
            boolean redisAccessToken = true;
            if (redisTemplate.hasKey("wx_accessToken_" + appId)) {
                json = (String) redisTemplate.opsForValue().get("wx_accessToken_" + appId);
                if (org.springframework.util.StringUtils.hasText(json)) {
                    redisAccessToken = false;
                }
            }
            if (redisAccessToken) {

                String url = WeixinBaseUtil.ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret;
                RestTemplate restTemplate = new RestTemplate();
                json = restTemplate.getForObject(url, String.class);
                redisTemplate.opsForValue().set("wx_accessToken_" + appId, json, 60 * 60, TimeUnit.SECONDS);
            }
            AccessTokenBean accessTokenBean = new AccessTokenBean(json);
            return accessTokenBean.getAccessToken();
        } else {
            return null;
        }
    }

    /**
     * 评论
     *
     * @param courseId
     * @param userId
     * @param comment
     * @return
     */
    @Override
    public Map<String, Object> comment(Long courseId, Long userId, String comment) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "0");
        resultMap.put("message", "");
        if (courseId == null || courseId <= 0l) {
            resultMap.put("message", "课程ID不能为空或有误~");
            return resultMap;
        }
        TKzyCourse tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
        if (tKzyCourse == null) {
            resultMap.put("message", "未查到该课程信息,请确认~");
            return resultMap;
        }
        if (userId == null || userId <= 0l) {
            resultMap.put("message", "请先登录~");
            resultMap.put("status", "-1");
            return resultMap;
        }
        if (comment == null) {
            resultMap.put("message", "评论内容不能为空~");
            return resultMap;
        }
        TKzyComment tKzyCourseComment = new TKzyComment();
        tKzyCourseComment.setCreateDt(new Date());
        tKzyCourseComment.setUserId(userId);
        tKzyCourseComment.setRelationId(courseId);
        tKzyCourseComment.setIsDelete(0);
        tKzyCourseComment.setReplyContent(comment);
        tKzyCourseComment.setType(2);
        tKzyCourseComment.setAuthStatus(1);
        tKzyCommentMapper.insertSelective(tKzyCourseComment);
        resultMap.put("message", "评论成功~");
        resultMap.put("status", "1");
        return resultMap;
    }

    /**
     * 课程分类
     *
     * @return
     */
    @Override
    public Map<String, Object> courseType() {
        return null;
    }


    @Override
    public TKzyCourse selectByPrimaryKey(Long courseId) {
        TKzyCourse tKzyCourse = null;
        if (courseId != null && courseId > 0l) {
            tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
        }
        return tKzyCourse;
    }

    @Autowired
    private JedisPool jedisPool;


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    public static String formateCourseDate(Date date) {
        DateFormat yearFormat = new SimpleDateFormat("YYYY年MM月dd日");
        DateFormat timeFormat = new SimpleDateFormat("HH:mm");
        String courseDate = yearFormat.format(date);
        String courseTime = timeFormat.format(date);
        return courseDate + " " + getWeekOfDate(date) + " " + courseTime;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static double getWith(Font font, String str) {
        FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
        Rectangle rec = font.getStringBounds(str, frc).getBounds();
        return rec.getWidth();
    }
}