package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.clickhouse.service.StatsQueryService;
import com.ycwl.basic.mapper.BrokerMapper;
import com.ycwl.basic.model.pc.broker.entity.BrokerEntity;
import com.ycwl.basic.model.pc.broker.req.BrokerReqQuery;
import com.ycwl.basic.model.pc.broker.resp.BrokerRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.BrokerService;
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;

/**
 * @Author：longbinbin
 * @Date：2024/12/2 15:55
 */
@Service
public class BrokerServiceImpl implements BrokerService {

    @Autowired
    private BrokerMapper brokerMapper;
    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private StatsQueryService statsQueryService;

    @Override
    public PageInfo<BrokerRespVO> pageQuery(BrokerReqQuery brokerReqQuery) {
        PageHelper.startPage(brokerReqQuery.getPageNum(),brokerReqQuery.getPageSize());
        List<BrokerRespVO> list = brokerMapper.list(brokerReqQuery);

        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(BrokerRespVO::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()));
            }
            // 从 ClickHouse/MySQL 查询分销员扫码次数
            Integer scanCount = statsQueryService.countBrokerScanCount(item.getId());
            item.setBrokerScanCount(scanCount != null ? scanCount.longValue() : 0L);
        });

        PageInfo<BrokerRespVO> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<BrokerRespVO> list(BrokerReqQuery brokerReqQuery) {
        List<BrokerRespVO> list = brokerMapper.list(brokerReqQuery);

        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(BrokerRespVO::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()));
            }
            // 从 ClickHouse/MySQL 查询分销员扫码次数
            Integer scanCount = statsQueryService.countBrokerScanCount(item.getId());
            item.setBrokerScanCount(scanCount != null ? scanCount.longValue() : 0L);
        });

        return list;
    }

    @Override
    public BrokerRespVO getById(Long id) {
        return brokerMapper.getById(id);
    }

    @Override
    public int addOrUpdate(BrokerEntity broker) {
        Long id = broker.getId();
        if(id==null){
            return brokerMapper.add(broker);
        }else {
            return brokerMapper.update(broker);
        }
    }

    @Override
    public int delete(Long id) {
        return brokerMapper.deleteById(id);
    }

    @Override
    public int updateStatus(Long id) {
        return brokerMapper.updateStatus(id);
    }

    @Override
    public int updateBrokerEnable(Long id) {
        return brokerMapper.updateBrokerEnable(id);
    }
}
