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.StoreStaffFitnessCertification;
import shop.alien.mapper.StoreStaffFitnessCertificationMapper;
import shop.alien.store.service.StoreStaffFitnessCertificationService;

import java.util.List;

/**
 * 运动健身员工认证/荣誉表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StoreStaffFitnessCertificationServiceImpl extends ServiceImpl<StoreStaffFitnessCertificationMapper, StoreStaffFitnessCertification> implements StoreStaffFitnessCertificationService {

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

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

    @Override
    public R<StoreStaffFitnessCertification> editStoreStaffFitnessCertification(StoreStaffFitnessCertification storeStaffFitnessCertification) {
        log.info("StoreStaffFitnessCertificationServiceImpl.editStoreStaffFitnessCertification?storeStaffFitnessCertification={}", storeStaffFitnessCertification);
        if (storeStaffFitnessCertification.getId() == null) {
            log.error("更新运动健身员工认证/荣誉失败：ID不能为空");
            return R.fail("ID不能为空");
        }

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

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

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

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

    @Override
    public IPage<StoreStaffFitnessCertification> getPage(int pageNum, int pageSize, Integer staffId, Integer type, String name) {
        LambdaQueryWrapper<StoreStaffFitnessCertification> queryWrapper = new LambdaQueryWrapper<>();

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

        // 类型查询
        if (type != null) {
            queryWrapper.eq(StoreStaffFitnessCertification::getType, type);
        }

        // 名称模糊查询
        if (StringUtils.hasText(name)) {
            queryWrapper.like(StoreStaffFitnessCertification::getName, name);
        }

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

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

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

    @Override
    public List<StoreStaffFitnessCertification> getListByStaffIdAndType(Integer staffId, Integer type) {
        if (staffId == null) {
            return null;
        }
        LambdaQueryWrapper<StoreStaffFitnessCertification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreStaffFitnessCertification::getStaffId, staffId);
        if (type != null) {
            queryWrapper.eq(StoreStaffFitnessCertification::getType, type);
        }
        queryWrapper.orderByDesc(StoreStaffFitnessCertification::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);
    }
}
