package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.StoreProductGym;
import shop.alien.mapper.StoreProductGymMapper;
import shop.alien.store.service.StoreProductGymService;

import java.util.List;

/**
 * 运动健身商品表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StoreProductGymServiceImpl extends ServiceImpl<StoreProductGymMapper, StoreProductGym> implements StoreProductGymService {

    @Override
    public R<StoreProductGym> addStoreProductGym(StoreProductGym storeProductGym) {
        log.info("StoreProductGymServiceImpl.addStoreProductGym?storeProductGym={}", storeProductGym);
        // 设置默认值
        if (storeProductGym.getPrice() == null) {
            storeProductGym.setPrice(new java.math.BigDecimal("0.00"));
        }
        if (storeProductGym.getStatus() == null) {
            storeProductGym.setStatus(1);
        }
        if (storeProductGym.getCreatedUserId() == null) {
            storeProductGym.setCreatedUserId(0);
        }
        if (storeProductGym.getUpdatedUserId() == null) {
            storeProductGym.setUpdatedUserId(0);
        }

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

    @Override
    public R<StoreProductGym> editStoreProductGym(StoreProductGym storeProductGym) {
        log.info("StoreProductGymServiceImpl.editStoreProductGym?storeProductGym={}", storeProductGym);
        if (storeProductGym.getId() == null) {
            log.error("更新运动健身商品失败：ID不能为空");
            return R.fail("ID不能为空");
        }

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

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

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

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

    @Override
    public IPage<StoreProductGym> getPage(int pageNum, int pageSize, String name, String classMode, Integer status, Integer extId) {
        LambdaQueryWrapper<StoreProductGym> queryWrapper = new LambdaQueryWrapper<>();

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

        // 上课形式查询
        if (StringUtils.hasText(classMode)) {
            queryWrapper.eq(StoreProductGym::getClassMode, classMode);
        }

        // 状态查询
        if (status != null) {
            queryWrapper.eq(StoreProductGym::getStatus, status);
        }

        // 商品表主键查询
        if (extId != null) {
            queryWrapper.eq(StoreProductGym::getExtId, extId);
        }

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

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

    @Override
    public List<StoreProductGym> getListByExtId(Integer extId) {
        if (extId == null) {
            return null;
        }
        LambdaQueryWrapper<StoreProductGym> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreProductGym::getExtId, extId)
                .orderByDesc(StoreProductGym::getCreatedTime);
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        if (id == null) {
            log.error("更新运动健身商品状态失败：ID不能为空");
            return false;
        }
        if (status == null || (status != 0 && status != 1)) {
            log.error("更新运动健身商品状态失败：状态值无效，只能为0或1");
            return false;
        }

        LambdaUpdateWrapper<StoreProductGym> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreProductGym::getId, id)
                .set(StoreProductGym::getStatus, status);

        return this.update(updateWrapper);
    }

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

    @Override
    public List<StoreProductGym> getByExtId(Integer id) {
        return this.lambdaQuery().eq(StoreProductGym::getExtId, id).list();
    }
}
