package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreStaffFitnessCourse;
import shop.alien.mapper.StoreStaffFitnessCourseMapper;
import shop.alien.store.service.StoreStaffFitnessCourseService;

import java.util.List;

/**
 * 运动健身员工课程信息表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StoreStaffFitnessCourseServiceImpl extends ServiceImpl<StoreStaffFitnessCourseMapper, StoreStaffFitnessCourse> implements StoreStaffFitnessCourseService {

    @Override
    public R<StoreStaffFitnessCourse> addStoreStaffFitnessCourse(StoreStaffFitnessCourse storeStaffFitnessCourse) {
        log.info("StoreStaffFitnessCourseServiceImpl.addStoreStaffFitnessCourse?storeStaffFitnessCourse={}", storeStaffFitnessCourse);
        // 设置默认值
        if (storeStaffFitnessCourse.getDeleteFlag() == null) {
            storeStaffFitnessCourse.setDeleteFlag(0);
        }
        if (storeStaffFitnessCourse.getCreatedUserId() == null) {
            storeStaffFitnessCourse.setCreatedUserId(0);
        }
        if (storeStaffFitnessCourse.getUpdatedUserId() == null) {
            storeStaffFitnessCourse.setUpdatedUserId(0);
        }

        boolean result = this.save(storeStaffFitnessCourse);
        if (result) {
            return R.data(storeStaffFitnessCourse);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<StoreStaffFitnessCourse> editStoreStaffFitnessCourse(StoreStaffFitnessCourse storeStaffFitnessCourse) {
        log.info("StoreStaffFitnessCourseServiceImpl.editStoreStaffFitnessCourse?storeStaffFitnessCourse={}", storeStaffFitnessCourse);
        if (storeStaffFitnessCourse.getId() == null) {
            log.error("更新运动健身员工课程失败：ID不能为空");
            return R.fail("ID不能为空");
        }

        // 设置更新人
        if (storeStaffFitnessCourse.getUpdatedUserId() == null) {
            storeStaffFitnessCourse.setUpdatedUserId(0);
        }

        boolean result = this.updateById(storeStaffFitnessCourse);
        if (result) {
            return R.data(storeStaffFitnessCourse);
        }
        return R.fail("修改失败");
    }

    @Override
    public R<Boolean> deleteStoreStaffFitnessCourse(Integer id) {
        log.info("StoreStaffFitnessCourseServiceImpl.deleteStoreStaffFitnessCourse?id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @Override
    public R<StoreStaffFitnessCourse> getStoreStaffFitnessCourseById(Integer id) {
        log.info("StoreStaffFitnessCourseServiceImpl.getStoreStaffFitnessCourseById?id={}", id);
        StoreStaffFitnessCourse storeStaffFitnessCourse = this.getById(id);
        if (storeStaffFitnessCourse != null) {
            return R.data(storeStaffFitnessCourse);
        }
        return R.fail("查询失败，数据不存在");
    }

    @Override
    public IPage<StoreStaffFitnessCourse> getPage(int pageNum, int pageSize, Integer staffId, String courseType, String courseName) {
        LambdaQueryWrapper<StoreStaffFitnessCourse> queryWrapper = new LambdaQueryWrapper<>();

        // 员工ID查询
        if (staffId != null) {
            queryWrapper.eq(StoreStaffFitnessCourse::getStaffId, staffId);
        }

        // 课程类型查询
        if (StringUtils.hasText(courseType)) {
            queryWrapper.eq(StoreStaffFitnessCourse::getCourseType, courseType);
        }

        // 项目名称模糊查询
        if (StringUtils.hasText(courseName)) {
            queryWrapper.like(StoreStaffFitnessCourse::getCourseName, courseName);
        }

        // 按创建时间倒序
        queryWrapper.orderByDesc(StoreStaffFitnessCourse::getCreatedTime);

        return this.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public List<StoreStaffFitnessCourse> getListByStaffId(Integer staffId) {
        if (staffId == null) {
            return null;
        }
        LambdaQueryWrapper<StoreStaffFitnessCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreStaffFitnessCourse::getStaffId, staffId)
                .orderByDesc(StoreStaffFitnessCourse::getCreatedTime);
        return this.list(queryWrapper);
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            log.error("批量删除运动健身员工课程失败：ID列表不能为空");
            return false;
        }
        // 逻辑删除，MyBatis-Plus会自动处理
        return this.removeByIds(ids);
    }
}

