package com.ruoyi.school.course.service;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.page.RespPage;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.core.api.APIResponse;
import com.ruoyi.core.api.ApiResEnums;
import com.ruoyi.school.course.domain.ScCourse;
import com.ruoyi.school.course.domain.ScCourseType;
import com.ruoyi.school.course.domain.req.course.ReqSearchScCourseType;
import com.ruoyi.school.member.domain.ScMemberCardTypes;
import com.ruoyi.school.member.service.ScMemberCardTypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：zhangbaoyu
 * @date ：Created in 2020-01-14 17:24
 */
@Service
@Transactional
public class BusinessScCourseTypeService {

    @Autowired
    private IScCourseTypeService scCourseTypeService;
    @Autowired
    private IScCourseService courseService;

    /**
     * 查询
     *
     * @param reqSearchScCourseType
     * @return
     */
    public APIResponse searchList(ReqSearchScCourseType reqSearchScCourseType) {
        QueryWrapper qw = new QueryWrapper();
        if (StrUtil.isNotEmpty(reqSearchScCourseType.getCourseType())) {
            qw.eq("course_type", reqSearchScCourseType.getCourseType());
        }
        qw.eq("tenant_id", SecurityUtils.getLoginUser().getNowTenantId());
        RespPage<ScCourseType> page = new RespPage(reqSearchScCourseType.getPageNum(), reqSearchScCourseType.getPageSize());
        RespPage<ScCourseType> listPage = scCourseTypeService.page(page, qw);
        return APIResponse.toAPIResponse(listPage);
    }

    /**
     * select
     * @param reqSearchScCourseType
     * @return
     */
    public List<ScCourseType> select(ReqSearchScCourseType reqSearchScCourseType) {
        QueryWrapper qw = new QueryWrapper();
        qw.select("course_type", "course_type_id");
        if (StrUtil.isNotEmpty(reqSearchScCourseType.getCourseType())) {
            qw.like("course_type", reqSearchScCourseType.getCourseType());
        }
        if (ObjectUtil.isNotEmpty(reqSearchScCourseType.getTypeIds())&&reqSearchScCourseType.getTypeIds().length>0) {
            qw.in("course_type_id", reqSearchScCourseType.getTypeIds());
        }
        qw.eq("in_use", "1");
        qw.eq("tenant_id", SecurityUtils.getLoginUser().getNowTenantId());
        qw.orderByDesc("create_time");
        List<ScCourseType> list = scCourseTypeService.list(qw);
        return list;
    }

    /**
     * 详情
     *
     * @param courseTypeId
     * @return
     */
    public APIResponse detailById(Long courseTypeId) {
        if (null == courseTypeId) {
            return APIResponse.toAPIResponse(null);
        }
        ScCourseType detailInfo = scCourseTypeService.getById(courseTypeId);
        return APIResponse.toAPIResponse(detailInfo);
    }

    /**
     * 添加
     *
     * @param scCourseType
     * @return
     */
    public APIResponse addScCourseType(ScCourseType scCourseType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        scCourseType.setCreateUser(loginUser.getUserId());
        scCourseType.setTenantId(loginUser.getNowTenantId());
        boolean addScCourseType = scCourseTypeService.save(scCourseType);
        if (addScCourseType) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    /**
     * 更新
     *
     * @param scCourseType
     * @return
     */
    public APIResponse updateScCourseType(ScCourseType scCourseType) {
        if (null == scCourseType.getCourseTypeId()) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
        //校区是否变更 对应班级的校区修改

        boolean updateScCourseType = scCourseTypeService.updateById(scCourseType);
        if (updateScCourseType) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    @Autowired
    private ScMemberCardTypesService memberCardTypesService;
    /**
     * 删除
     *
     * @param courseTypeIds
     * @return
     */
    public APIResponse deleteById(Long[] courseTypeIds) {
        if (null == courseTypeIds || courseTypeIds.length == 0) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }

        QueryWrapper<ScCourse> qw = new QueryWrapper<>();
        qw.in("course_type_id", Arrays.asList(courseTypeIds));
        int courseCount = courseService.count(qw);
        if (courseCount != 0) {
            return APIResponse.toExceptionResponse("已有课程使用,无法删除");
        }


        String arrayAsString = Arrays.stream(courseTypeIds)
                .map(Object::toString) // 将Long转换为String
                .collect(Collectors.joining(",")); // 使用逗号和空格连接字符串

        int cardTypes= memberCardTypesService.count(new QueryWrapper<ScMemberCardTypes>()
                .last(" and JSON_CONTAINS(course_types, CAST("+arrayAsString+" AS JSON))"));
        if (cardTypes != 0) {
            return APIResponse.toExceptionResponse("已有会员卡项使用,无法删除");
        }


        boolean deleteScCourseType = scCourseTypeService.removeByIds(Arrays.asList(courseTypeIds));
        if (deleteScCourseType) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    /**
     * 变更是否开售
     *
     * @param ScCourseType
     * @return
     */
    public APIResponse changeCourseTypeSale(ScCourseType scCourseType) {
        if (null == scCourseType.getCourseTypeId()) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
        if (StrUtil.isEmpty(scCourseType.getInUse())) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
        UpdateWrapper uw = new UpdateWrapper();
        uw.set("in_use", scCourseType.getInUse());
        uw.eq("course_type_id", scCourseType.getCourseTypeId());
        boolean updateScCourseType = scCourseTypeService.update(uw);
        if (updateScCourseType) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }
}
