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.StoreProductBar;
import shop.alien.mapper.StoreProductBarMapper;
import shop.alien.store.service.StoreProductBarService;

import java.util.List;

/**
 * 酒吧商品表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StoreProductBarServiceImpl extends ServiceImpl<StoreProductBarMapper, StoreProductBar> implements StoreProductBarService {

    @Override
    public R<StoreProductBar> addStoreProductBar(StoreProductBar storeProductBar) {
        log.info("StoreProductBarServiceImpl.addStoreProductBar?storeProductBar={}", storeProductBar);
        // 设置默认值
        if (storeProductBar.getUnit() == null || storeProductBar.getUnit().isEmpty()) {
            storeProductBar.setUnit("份");
        }
        if (storeProductBar.getQuantity() == null) {
            storeProductBar.setQuantity(0);
        }
        if (storeProductBar.getStatus() == null) {
            storeProductBar.setStatus(1);
        }
        if (storeProductBar.getCreatedUserId() == null) {
            storeProductBar.setCreatedUserId(0);
        }
        if (storeProductBar.getUpdatedUserId() == null) {
            storeProductBar.setUpdatedUserId(0);
        }

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

    @Override
    public R<StoreProductBar> editStoreProductBar(StoreProductBar storeProductBar) {
        log.info("StoreProductBarServiceImpl.editStoreProductBar?storeProductBar={}", storeProductBar);
        if (storeProductBar.getId() == null) {
            log.error("更新酒吧商品失败：ID不能为空");
            return R.fail("ID不能为空");
        }

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

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

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

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

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

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

        // 品类查询
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(StoreProductBar::getCategory, category);
        }

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

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

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

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

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

    @Override
    public List<StoreProductBar> getListByCategory(String category) {
        if (!StringUtils.hasText(category)) {
            return null;
        }
        LambdaQueryWrapper<StoreProductBar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreProductBar::getCategory, category)
                .orderByDesc(StoreProductBar::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<StoreProductBar> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreProductBar::getId, id)
                .set(StoreProductBar::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<StoreProductBar> getByExtId(Integer id) {
        return this.lambdaQuery().eq(StoreProductBar::getExtId, id).list();
    }
}
