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.StoreStaffFitnessExperience;
import shop.alien.mapper.StoreStaffFitnessExperienceMapper;
import shop.alien.store.service.StoreStaffFitnessExperienceService;

import java.util.List;

/**
 * 运动健身员工从业经历表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StoreStaffFitnessExperienceServiceImpl extends ServiceImpl<StoreStaffFitnessExperienceMapper, StoreStaffFitnessExperience> implements StoreStaffFitnessExperienceService {

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

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

    @Override
    public R<StoreStaffFitnessExperience> editStoreStaffFitnessExperience(StoreStaffFitnessExperience storeStaffFitnessExperience) {
        log.info("StoreStaffFitnessExperienceServiceImpl.editStoreStaffFitnessExperience?storeStaffFitnessExperience={}", storeStaffFitnessExperience);
        if (storeStaffFitnessExperience.getId() == null) {
            log.error("更新运动健身员工从业经历失败：ID不能为空");
            return R.fail("ID不能为空");
        }

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

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

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

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

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

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

        // 工作单位模糊查询
        if (StringUtils.hasText(workUnit)) {
            queryWrapper.like(StoreStaffFitnessExperience::getWorkUnit, workUnit);
        }

        // 职位模糊查询
        if (StringUtils.hasText(workPosition)) {
            queryWrapper.like(StoreStaffFitnessExperience::getWorkPosition, workPosition);
        }

        // 按开始时间倒序
        queryWrapper.orderByDesc(StoreStaffFitnessExperience::getStartTime);

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

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

