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.StoreStaffFitnessBase;
import shop.alien.mapper.StoreStaffFitnessBaseMapper;
import shop.alien.store.service.StoreStaffFitnessBaseService;

import java.util.List;

/**
 * 运动健身员工基本信息表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StoreStaffFitnessBaseServiceImpl extends ServiceImpl<StoreStaffFitnessBaseMapper, StoreStaffFitnessBase> implements StoreStaffFitnessBaseService {

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

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

    @Override
    public R<StoreStaffFitnessBase> editStoreStaffFitnessBase(StoreStaffFitnessBase storeStaffFitnessBase) {
        log.info("StoreStaffFitnessBaseServiceImpl.editStoreStaffFitnessBase?storeStaffFitnessBase={}", storeStaffFitnessBase);
        if (storeStaffFitnessBase.getId() == null) {
            log.error("更新运动健身员工基本信息失败：ID不能为空");
            return R.fail("ID不能为空");
        }

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

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

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

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

    @Override
    public R<StoreStaffFitnessBase> getByStaffId(Integer staffId) {
        log.info("StoreStaffFitnessBaseServiceImpl.getByStaffId?staffId={}", staffId);
        if (staffId == null) {
            return R.fail("员工ID不能为空");
        }
        LambdaQueryWrapper<StoreStaffFitnessBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreStaffFitnessBase::getStaffId, staffId)
                .orderByDesc(StoreStaffFitnessBase::getCreatedTime)
                .last("LIMIT 1");
        StoreStaffFitnessBase storeStaffFitnessBase = this.getOne(queryWrapper);
        if (storeStaffFitnessBase != null) {
            return R.data(storeStaffFitnessBase);
        }
        return R.fail("查询失败，数据不存在");
    }

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

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

        // 学历查询
        if (StringUtils.hasText(education)) {
            queryWrapper.eq(StoreStaffFitnessBase::getEducation, education);
        }

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

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

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

