package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.StoreInfo;
import shop.alien.entity.store.StorePersonalCustomized;
import shop.alien.entity.store.vo.StorePersonalCustomizedVo;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.StoreInfoMapper;
import shop.alien.mapper.StorePersonalCustomizedMapper;
import shop.alien.store.service.StorePersonalCustomizedService;
import shop.alien.util.common.DistanceUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 个性化定制 服务实现类
 * </p>
 *
 * @author ssk
 * @since 2025-03-18
 */
@Service
@RequiredArgsConstructor
public class StorePersonalCustomizedServiceImpl extends ServiceImpl<StorePersonalCustomizedMapper, StorePersonalCustomized> implements StorePersonalCustomizedService {

    private final LifeCouponMapper lifeCouponMapper;

    private final StoreInfoMapper storeInfoMapper;

    private final StorePersonalCustomizedMapper storePersonalCustomizedMapper;

    @Override
    public Boolean createPersonalCustomizedRecord(StorePersonalCustomizedVo storePersonalCustomizedVo) {
        // 酒店
        if (1 == storePersonalCustomizedVo.getCustomizedType()) {
            return false;
        } else if (2 == storePersonalCustomizedVo.getCustomizedType()) {
            // 商家信息
            LambdaQueryWrapper<StoreInfo> storeWrapper = new LambdaQueryWrapper<>();
            storeWrapper.like(StoreInfo::getStorePosition, ",");
            List<StoreInfo> storeList = storeInfoMapper.selectList(storeWrapper);
            List<Integer> storeIdList = new ArrayList<>();
            storeList.forEach(store -> {
                double position = DistanceUtil.haversineCalculateDistance(storePersonalCustomizedVo.getJingdu(), storePersonalCustomizedVo.getWeidu(),
                        Double.parseDouble(store.getStorePosition().split(",")[0]), Double.parseDouble(store.getStorePosition().split(",")[1]));
                if (position <= 0.5) {
                    storeIdList.add(store.getId());
                }
            });
            if (CollectionUtils.isEmpty(storeIdList)) return false;

            // 团购套餐
            LambdaQueryWrapper<LifeCoupon> couponWrapper = new LambdaQueryWrapper<>();
            couponWrapper.in(LifeCoupon::getStoreId, storeIdList);
            couponWrapper.eq(LifeCoupon::getType, 2);
            couponWrapper.eq(LifeCoupon::getStatus, 1);
            couponWrapper.le(LifeCoupon::getMinUsePeoples, storePersonalCustomizedVo.getUsePeoples());
            couponWrapper.ge(LifeCoupon::getMaxUsePeoples, storePersonalCustomizedVo.getUsePeoples());
            couponWrapper.between(LifeCoupon::getPrice, storePersonalCustomizedVo.getMinPrice(), storePersonalCustomizedVo.getMaxPrice());
            couponWrapper.eq(LifeCoupon::getDietaryPreferences, storePersonalCustomizedVo.getDietaryPreferences());
            couponWrapper.eq(LifeCoupon::getCookingMethods, storePersonalCustomizedVo.getCookingMethods());
            List<LifeCoupon> couponList = lifeCouponMapper.selectList(couponWrapper);
            if (CollectionUtils.isEmpty(couponList)) return false;

            List<StorePersonalCustomized> customizedList = new ArrayList<>();
            couponList.forEach(coupon -> {
                StorePersonalCustomized customized = new StorePersonalCustomized();
                customized.setUserId(storePersonalCustomizedVo.getUserId());
                customized.setStoreId(Integer.parseInt(coupon.getStoreId()));
                customized.setCouponId(Integer.parseInt(coupon.getId()));
                customized.setCustomizedType(2);
                customizedList.add(customized);
            });
            this.saveBatch(customizedList);
        } else {
            return false;
        }

        return true;
    }

    @Override
    public IPage<StorePersonalCustomizedVo> getPersonalCustomizedList(StorePersonalCustomizedVo storePersonalCustomizedVo) {
        IPage<StorePersonalCustomized> page = new Page<>(storePersonalCustomizedVo.getPage(), storePersonalCustomizedVo.getSize());
        QueryWrapper<StorePersonalCustomized> wrapper = new QueryWrapper<>();
        wrapper.eq("custom.user_id", storePersonalCustomizedVo.getUserId());
        wrapper.eq("custom.delete_flag", 0);
        wrapper.eq("coupon.delete_flag", 0);
        wrapper.eq("coupon.type", 2);
        wrapper.eq("coupon.status", 1);
        wrapper.orderByDesc("custom.created_time", "custom.store_id");
        return storePersonalCustomizedMapper.getPersonalCustomizedList(page, wrapper);
    }
}
