package shop.alien.storeplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.store.EssentialHolidayComparison;
import shop.alien.entity.store.StoreBusinessInfo;
import shop.alien.entity.store.vo.StoreBusinessInfoVo;
import shop.alien.mapper.EssentialHolidayComparisonMapper;
import shop.alien.mapper.StoreBusinessInfoMapper;
import shop.alien.storeplatform.service.StorePlatformBusinessInfoService;

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

/**
 * 二期-门店营业时间 服务实现类
 *
 * @author ssk
 * @since 2024-12-05
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class StorePlatformBusinessInfoServiceImpl extends ServiceImpl<StoreBusinessInfoMapper, StoreBusinessInfo> implements StorePlatformBusinessInfoService {

    private final EssentialHolidayComparisonMapper essentialHolidayComparisonMapper;

    /**
     * 获取门店营业信息（包含正常营业时间和特殊营业时间，特殊营业时间关联节假日信息）
     *
     * @param storeId 门店信息
     * @return list
     */
    @Override
    public List<StoreBusinessInfoVo> getStoreBusinessInfo(Long storeId) {
        // 查询营业时间（包含正常时间和特殊时间）
        List<StoreBusinessInfo> storeBusinessInfoList = this.list(
                new LambdaQueryWrapper<StoreBusinessInfo>()
                        .eq(StoreBusinessInfo::getStoreId, storeId)
                        .eq(StoreBusinessInfo::getDeleteFlag, 0)
                        .orderByAsc(StoreBusinessInfo::getBusinessType) // 先按类型排序：1-正常时间，2-特殊时间
                        .orderByAsc(StoreBusinessInfo::getBusinessDate) // 再按日期排序
        );

        // 转换为 VO 并关联节假日信息
        // store_business_info 的 essential_id 关联 essential_holiday_comparison 的 id
        List<StoreBusinessInfoVo> resultList = new ArrayList<>();
        for (StoreBusinessInfo businessInfo : storeBusinessInfoList) {
            StoreBusinessInfoVo vo = new StoreBusinessInfoVo();
            // 复制基本信息
            BeanUtils.copyProperties(businessInfo, vo);

            // 如果有关联的节假日ID（essential_id），查询 essential_holiday_comparison 表的节假日信息
            if (businessInfo.getEssentialId() != null && !businessInfo.getEssentialId().trim().isEmpty()) {
                try {
                    Integer essentialId = Integer.parseInt(businessInfo.getEssentialId().trim());
                    EssentialHolidayComparison holiday = essentialHolidayComparisonMapper.selectById(essentialId);
                    if (holiday != null) {
                        vo.setHolidayInfo(holiday);
                    } else {
                        log.warn("门店营业时间关联的节假日信息不存在，storeId={}, essentialId={}", storeId, essentialId);
                    }
                } catch (NumberFormatException e) {
                    log.warn("门店营业时间关联的节假日ID格式错误，storeId={}, essentialId={}", storeId, businessInfo.getEssentialId());
                } catch (Exception e) {
                    log.error("查询节假日信息失败，storeId={}, essentialId={}", storeId, businessInfo.getEssentialId(), e);
                }
            }

            resultList.add(vo);
        }

        return resultList;
    }

}
