package cn.iocoder.yudao.module.jx.service.impl;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCourse;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolInfo;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCourseMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInfoMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolCourseService;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

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

/**
 * 驾校课程分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-21
 */
@Service
public class DriveSchoolCourseServiceImpl implements IDriveSchoolCourseService
{
    @Resource
    private DriveSchoolCourseMapper driveSchoolCourseMapper;

    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询驾校课程分类
     *
     * @param id 驾校课程分类主键
     * @return 驾校课程分类
     */
    @Override
    public DriveSchoolCourse selectDriveSchoolCourseById(Long id)
    {
        DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(id);
        String dayStart = driveSchoolCourse.getDayStart();
        String dayEnd = driveSchoolCourse.getDayEnd();
        String[] str3 = {dayStart, dayEnd};
        driveSchoolCourse.setWeekDay(str3);
        return driveSchoolCourse;
    }

    @Override
    public DriveSchoolCourse selectDriveSchoolCourseAppletById(Long id)
    {
        DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(id);
        String dayStart = driveSchoolCourse.getDayStart();
        String dayEnd = driveSchoolCourse.getDayEnd();
        driveSchoolCourse.setTime(dayStart + "~" + dayEnd);
        return driveSchoolCourse;
    }

    /**
     * 查询驾校课程分类列表
     *
     * @param driveSchoolCourse 驾校课程分类
     * @return 驾校课程分类
     */
    @Override
    public IPage<DriveSchoolCourse> selectDriveSchoolCourseList(DriveSchoolCourse driveSchoolCourse, Page<DriveSchoolCourse> page)
    {
        Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        driveSchoolCourse.setDeptId(deptId);
//        if (deptId == 100){
//            driveSchoolCourse.setDeptId(null);
//        }
        IPage<DriveSchoolCourse> driveSchoolCourseIPage = driveSchoolCourseMapper.selectDriveSchoolCourseList(driveSchoolCourse, page);
        List<DriveSchoolCourse> driveSchoolCourses = driveSchoolCourseIPage.getRecords();
        for (DriveSchoolCourse driveSchoolCours : driveSchoolCourses) {
            // update by pqz 2024年8月28日 解决空指针
            DriveSchoolInfo driveSchoolInfo = driveSchoolInfoMapper.getSchoolInfoByDeptId(driveSchoolCours.getDeptId() == null ? 100:driveSchoolCours.getDeptId());
            // update by pqz 2024年8月28日 解决空指针
            if (null != driveSchoolInfo){
                driveSchoolCours.setPhone(driveSchoolInfo.getPhone());
            }
        }
        if (ObjectUtils.isNotEmpty(driveSchoolCourse.getPayType()) && driveSchoolCourse.getPayType().equals("2")){
            driveSchoolCourses.forEach(item ->{
                item.setReserveMoney(item.getPrice());
                DriveSchoolInfo driveSchoolInfo = driveSchoolInfoMapper.getSchoolInfoByDeptId(item.getDeptId());
                item.setPhone(driveSchoolInfo.getPhone());
            });
        }
        return driveSchoolCourseIPage;
    }

    @Override
    public IPage<DriveSchoolCourse> selectDriveSchoolCourseAppList(DriveSchoolCourse driveSchoolCourse,Page<DriveSchoolCourse> page)
    {
        return driveSchoolCourseMapper.selectDriveSchoolCourseAppletList(driveSchoolCourse,page);
    }

    @Override
    public List<DriveSchoolCourse> selectDriveSchoolCourseAppletListAll(DriveSchoolCourse driveSchoolCourse)
    {
        return driveSchoolCourseMapper.selectDriveSchoolCourseAppletListAll(driveSchoolCourse);
    }

    @Override
    public List<DriveSchoolCourse> selectDriveSchoolCourseAppletFavorableListList(DriveSchoolCourse driveSchoolCourse)
    {
        return driveSchoolCourseMapper.selectDriveSchoolCourseAppletListAll(driveSchoolCourse);
    }

    /**
     * 新增驾校课程分类
     *
     * @param driveSchoolCourse 驾校课程分类
     * @return 结果
     */
    @Override
    public int insertDriveSchoolCourse(DriveSchoolCourse driveSchoolCourse)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCourse.setDeptId(deptId);
        return driveSchoolCourseMapper.insertDriveSchoolCourse(driveSchoolCourse);
    }

    /**
     * 修改驾校课程分类
     *
     * @param driveSchoolCourse 驾校课程分类
     * @return 结果
     */
    @Override
    public int updateDriveSchoolCourse(DriveSchoolCourse driveSchoolCourse)
    {
        //driveSchoolCourse.setUpdateTime(DateUtils.getNowDate());
        /*String[] weekDay = driveSchoolCourse.getWeekDay();
        driveSchoolCourse.setDayStart(weekDay[0]);
        driveSchoolCourse.setDayEnd(weekDay[1]);*/
        return driveSchoolCourseMapper.updateDriveSchoolCourse(driveSchoolCourse);
    }

    /**
     * 批量删除驾校课程分类
     *
     * @param ids 需要删除的驾校课程分类主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCourseByIds(Long[] ids)
    {
        return driveSchoolCourseMapper.deleteDriveSchoolCourseByIds(ids);
    }

    /**
     * 删除驾校课程分类信息
     *
     * @param id 驾校课程分类主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCourseById(Long id)
    {
        return driveSchoolCourseMapper.deleteDriveSchoolCourseById(id);
    }

    @Override
    public DriveSchoolCourse getOne(DriveSchoolCourse driveSchoolCourse) {
        return driveSchoolCourseMapper.getOne();
    }
}
