package com.jeesharp.business.modules.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jeesharp.business.modules.mall.entity.MallCartItem;
import com.jeesharp.business.modules.mall.mapper.MallCartItemMapper;
import com.jeesharp.business.modules.mall.service.MallCartItemService;
import com.jeesharp.business.modules.mall.vo.MallCartItemPortalRowVO;
import com.jeesharp.business.modules.mall.vo.MallCartItemQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.utils.WebFrameworkUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车行领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@Transactional(readOnly = true)
public class MallCartItemServiceImpl extends CrudService<MallCartItemMapper, MallCartItem, MallCartItemQueryVO> implements MallCartItemService {

    private static final String IDS_SEPARATOR = ",";

    @Override
    @Transactional(readOnly = false)
    public void createCartItem(MallCartItem entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateCartItem(MallCartItem entity) {
        update(entity);
    }

    @Override
    public MallCartItem findActiveByMemberAndSku(String memberId, String skuId) {
        LambdaQueryWrapper<MallCartItem> w = new LambdaQueryWrapper<>();
        w.eq(MallCartItem::getMemberId, memberId);
        w.eq(MallCartItem::getSkuId, skuId);
        w.eq(MallCartItem::getDelFlag, "0");
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public List<MallCartItemPortalRowVO> listPortalRowsByMemberId(String memberId) {
        if (memberId == null || memberId.isBlank()) {
            return Collections.emptyList();
        }
        return getBaseMapper().selectPortalRowsByMemberId(memberId, WebFrameworkUtils.getLoginUserTenantCode());
    }

    @Override
    @Transactional(readOnly = false)
    public void updateSelectAllByMember(String memberId, Integer selected) {
        MallCartItem patch = new MallCartItem();
        patch.setSelected(selected);
        LambdaUpdateWrapper<MallCartItem> u = new LambdaUpdateWrapper<>();
        u.eq(MallCartItem::getMemberId, memberId);
        u.eq(MallCartItem::getDelFlag, "0");
        getBaseMapper().update(patch, u);
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteByMemberAndItemIds(String memberId, List<String> itemIds) {
        if (memberId == null || memberId.isBlank() || itemIds == null || itemIds.isEmpty()) {
            return;
        }
        LambdaQueryWrapper<MallCartItem> w = new LambdaQueryWrapper<>();
        w.eq(MallCartItem::getMemberId, memberId);
        w.eq(MallCartItem::getDelFlag, "0");
        w.in(MallCartItem::getId, itemIds);
        List<MallCartItem> list = getBaseMapper().selectList(w);
        if (list.isEmpty()) {
            return;
        }
        String ids = list.stream().map(MallCartItem::getId).collect(Collectors.joining(IDS_SEPARATOR));
        batchDelete(Arrays.asList(ids.split(IDS_SEPARATOR)));
    }
}
