package com.hzu.labbooking.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzu.labbooking.domain.vo.MyCourseVO;
import com.hzu.labbooking.service.IOfficeService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.stereotype.Service;
import com.hzu.labbooking.mapper.CourseMapper;
import com.hzu.labbooking.domain.Course;
import com.hzu.labbooking.service.ICourseService;

/**
 * 课程信息Service业务层处理
 *
 * @author yezihao
 * @date 2021-06-13
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IOfficeService officeService;

    @Autowired
    private ISysUserService userService;

    /**
     * 修改课程信息
     *
     * @param course 课程信息
     * @return 结果
     */
    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除课程信息
     *
     * @param ids 需要删除的课程信息ID
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(Long[] ids) {
        return courseMapper.deleteCourseByIds(ids);
    }

    @Override
    public List<Course> selectCourse() {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUserId());
        List<Course> list = baseMapper.selectList(wrapper);
        for (Course course : list) {
            course.setCourseName(course.getName());
            course.setCourseId(course.getId());
        }
        return list;
    }

    @Override
    public List<MyCourseVO> selectCourseList(Course course) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String office = officeService.selectOfficeById(user.getUserId()).getOffice();
        // 拼接检索条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUserId()).eq("year", course.getYear()).eq("term", course.getTerm())
                .like(StringUtils.isNotEmpty(course.getName()), "name", course.getName())
                .like(StringUtils.isNotEmpty(course.getRequirement()), "requirement", course.getRequirement())
                .like(StringUtils.isNotEmpty(course.getClassName()), "class_name", course.getClassName())
                .eq(StringUtils.isNotNull(course.getNumber()), "number", course.getNumber());

        List<Course> courseList = baseMapper.selectList(wrapper);
        // 封装课程VO
        List<MyCourseVO> list = new ArrayList<>();
        for (Course myCourse : courseList) {
            MyCourseVO myCourseVO = new MyCourseVO();
            BeanUtils.copyProperties(myCourse, myCourseVO);
            myCourseVO.setNickName(user.getNickName());
            myCourseVO.setEmail(user.getEmail());
            myCourseVO.setPhonenumber(user.getPhonenumber());
            myCourseVO.setUserName(user.getUserName());
            myCourseVO.setOffice(office);
            list.add(myCourseVO);
        }
        return list;
    }

    @Override
    public MyCourseVO selectCourseById(Long id) {
        Course course = courseMapper.selectCourseById(id);
        SysUser user = userService.selectUserById(course.getUserId());

        MyCourseVO myCourseVO = new MyCourseVO();
        BeanUtils.copyProperties(course, myCourseVO);
        myCourseVO.setNickName(user.getNickName());
        myCourseVO.setUserName(user.getUserName());
        myCourseVO.setEmail(user.getEmail());
        myCourseVO.setPhonenumber(user.getPhonenumber());
        myCourseVO.setOffice(officeService.selectOfficeById(user.getUserId()).getOffice());

        return myCourseVO;
    }

    @Override
    public int insertCourse(Course course) {
        course.setUserId(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        return baseMapper.insert(course);
    }

}
