package com.ycwl.basic.service.pc.impl;
import com.ycwl.basic.mapper.CouponMapper;
import com.ycwl.basic.model.pc.coupon.entity.CouponEntity;
import com.ycwl.basic.model.pc.coupon.req.CouponQueryReq;
import com.ycwl.basic.model.pc.coupon.resp.CouponRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private ScenicRepository scenicRepository;

    @Override
    public Integer add(CouponEntity coupon) {
        return couponMapper.insert(coupon);
    }

    @Override
    public Boolean update(CouponEntity coupon) {
        return couponMapper.updateById(coupon) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return couponMapper.deleteById(id) > 0;
    }

    @Override
    public CouponEntity getById(Integer id) {
        return couponMapper.selectById(id);
    }

    @Override
    public List<CouponRespVO> list(CouponQueryReq query) {
        List<CouponRespVO> list = couponMapper.selectByQuery(query);
        
        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(CouponRespVO::getScenicId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> scenicNames = scenicRepository.batchGetScenicNames(scenicIds);
        
        // 设置景区名称
        list.forEach(item -> {
            if (item.getScenicId() != null) {
                item.setScenicName(scenicNames.get(item.getScenicId()));
            }
        });
        
        return list;
    }

    @Override
    public Boolean updateStatus(Integer id) {
        return couponMapper.updateStatus(id) > 0;
    }
}