package shop.alien.store.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.store.LifeBrowseRecord;
import shop.alien.mapper.LifeBrowseRecordMapper;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 浏览记录
 */
@Service
@RequiredArgsConstructor
public class LifeBrowseRecordService {

    private final LifeBrowseRecordMapper lifeBrowseRecordMapper;

    public int save(LifeBrowseRecord record) {
        LocalDate now = LocalDate.now();

        LambdaQueryWrapper<LifeBrowseRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LifeBrowseRecord::getUserId, record.getUserId());
        wrapper.eq(record.getStoreId() != null, LifeBrowseRecord::getStoreId, record.getStoreId());
        wrapper.eq(record.getCouponId() != null, LifeBrowseRecord::getCouponId, record.getCouponId());
        wrapper.eq(record.getDynamicsId() != null, LifeBrowseRecord::getDynamicsId, record.getDynamicsId());
//        wrapper.eq(LifeBrowseRecord::getLiulanDate, now.toString());
        List<LifeBrowseRecord> recordList = lifeBrowseRecordMapper.selectList(wrapper);

        if (!CollectionUtils.isEmpty(recordList)) {
            lifeBrowseRecordMapper.delete(wrapper);
        }

        record.setLiulanDate(now.toString());
        record.setLiulanTime(new Date());
        return lifeBrowseRecordMapper.insert(record);
    }

    public Map<String, List<Map<String, Object>>> getBrowseRecord(String userId, String type) {
        List<Map<String, Object>> list = new ArrayList<>();
        if("1".equals(type)){
            list = lifeBrowseRecordMapper.getBrowseRecordByUserId(userId);
        }else if("2".equals(type)){
            list = lifeBrowseRecordMapper.getGroupBuyBrowseRecordByUserId(userId);
        }else if("3".equals(type)){
            list = lifeBrowseRecordMapper.getDynamicsBrowseRecordByUserId(userId);
        }
        Map<String, List<Map<String, Object>>> listMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(list)) {
            listMap = list.stream().collect(Collectors.groupingBy(
                    record -> (String) record.get("liulanDate") // 根据 create_date 字段分组
            ));
        }
        return listMap;
    }

    public int clearBrowseRecord(String userId, String recordId,String type) {
        if(StringUtils.isNotBlank(recordId)){
            return lifeBrowseRecordMapper.deleteById(recordId);
        }
        LambdaUpdateWrapper<LifeBrowseRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LifeBrowseRecord::getUserId, userId);
        wrapper.set(LifeBrowseRecord::getDeleteFlag, 1);
        switch (type){
            case "1":
                wrapper.isNotNull(LifeBrowseRecord::getStoreId);
                break;
            case "2":
                wrapper.isNotNull(LifeBrowseRecord::getCouponId);
                break;
            case "3":
                wrapper.isNotNull(LifeBrowseRecord::getDynamicsId);
                break;
        }
        return lifeBrowseRecordMapper.update(null, wrapper);
    }
}
