package com.app.service.basics;

import com.app.dto.PageResult;
import com.app.dto.basics.ResponseCourseDto;
import com.app.dto.basics.operation.CourseDto;
import com.app.dto.basics.search.SearchCourseDto;
import com.app.entity.basics.Course;
import com.app.mapper.basics.CourseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 功能描述：课程
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Slf4j
@Service
public class CourseService {

    @Resource
    private CourseMapper mapper;

    /**
     * 功能描述：查询[课程]详情
     *
     * @param id 标识
     * @return
     */
    public Optional<Course> findById(String id) {
        return Optional.ofNullable(mapper.selectById(id));
    }

    /**
     * 功能描述：查询[课程]列表
     *
     */
    public PageResult<Course> page(SearchCourseDto dto) {
        IPage<ResponseCourseDto> iPage = mapper.page(dto,new Page<>(dto.getPage(),dto.getSize()));
        return new PageResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 功能描述：保存/更新[课程]
     *
     * @param courseDto
     */
    public Course saveOrUpdate(CourseDto courseDto) {
        Course _course;
        if (StringUtils.isEmpty(courseDto.getId())) {
            _course = new Course();
            _course.setName(courseDto.getName());
            _course.setIntroduceUrl(courseDto.getIntroduceUrl());
            _course.setBanner(courseDto.getBanner());
            _course.setPrice(courseDto.getPrice());
            _course.setThumbnail(courseDto.getThumbnail());
            _course.setBanner(courseDto.getBanner());
            _course.setType(courseDto.getType());
            _course.setIntroduceUrl(courseDto.getIntroduceUrl());
            _course.setRemarks(courseDto.getRemarks());
            _course.setOrganId(courseDto.getOrganId());
            mapper.insert(_course);
        } else {
            Optional<Course> optional = findById(courseDto.getId());
            _course = optional.get();
            _course.setName(courseDto.getName());
            _course.setIntroduceUrl(courseDto.getIntroduceUrl());
            _course.setBanner(courseDto.getBanner());
            _course.setPrice(courseDto.getPrice());
            _course.setThumbnail(courseDto.getThumbnail());
            _course.setBanner(courseDto.getBanner());
            _course.setType(courseDto.getType());
            _course.setIntroduceUrl(courseDto.getIntroduceUrl());
            _course.setRemarks(courseDto.getRemarks());
            _course.setOrganId(courseDto.getOrganId());
            mapper.updateById(_course);
        }

        return _course;
    }

    /**
     * 功能描述：替换[课程]删除状态
     * @param id
     */
    public void handleIsDel(String id) {
        Optional<Course> optional = findById(id);
        if (optional.isPresent()) {
            Course course = optional.get();
            course.setIsDel(!course.getIsDel());
            mapper.updateById(course);
        }
    }

}
