package com.jeesharp.business.modules.mall.service.impl;

import com.jeesharp.business.modules.mall.entity.MallBrowseFootprint;
import com.jeesharp.business.modules.mall.mapper.MallBrowseFootprintMapper;
import com.jeesharp.business.modules.mall.service.MallBrowseFootprintService;
import com.jeesharp.business.modules.mall.vo.MallBrowseFootprintQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class MallBrowseFootprintServiceImpl extends CrudService<MallBrowseFootprintMapper, MallBrowseFootprint, MallBrowseFootprintQueryVO> implements MallBrowseFootprintService {

    private static final String IDS_SEPARATOR = ",";
    private static final long MAX_USER_FOOTPRINTS = 200L;

    @Override
    @Transactional(readOnly = false)
    public void createFootprint(MallBrowseFootprint entity) {
        entity.setBrowseTime(new Date());
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateFootprint(MallBrowseFootprint entity) {
        entity.setBrowseTime(new Date());
        update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void recordBrowseFootprint(String memberId, String spuId) {
        if (memberId == null || spuId == null) {
            return;
        }
        MallBrowseFootprintQueryVO query = new MallBrowseFootprintQueryVO();
        query.setMemberId(memberId);
        query.setSpuId(spuId);
        List<MallBrowseFootprint> existing = findList(query);
        if (!existing.isEmpty()) {
            MallBrowseFootprint footprint = existing.get(0);
            footprint.setBrowseTime(new Date());
            updateFootprint(footprint);
            return;
        }
        MallBrowseFootprint footprint = new MallBrowseFootprint();
        footprint.setMemberId(memberId);
        footprint.setSpuId(spuId);
        createFootprint(footprint);
    }

    @Override
    public PageRespVO<MallBrowseFootprint> getUserBrowseFootprints(String memberId, Page<MallBrowseFootprint> page) {
        MallBrowseFootprintQueryVO query = new MallBrowseFootprintQueryVO();
        query.setMemberId(memberId);
        query.setPageSize(MAX_USER_FOOTPRINTS);
        Page<MallBrowseFootprint> result = findPage(page, query);
        PageRespVO<MallBrowseFootprint> resp = new PageRespVO<>();
        resp.setRecords(result.getRecords());
        resp.setTotal(result.getTotal());
        resp.setPageNo(result.getPageNo());
        resp.setPageSize(result.getPageSize());
        return resp;
    }

    @Override
    @Transactional(readOnly = false)
    public void clearUserBrowseFootprints(String memberId) {
        MallBrowseFootprintQueryVO query = new MallBrowseFootprintQueryVO();
        query.setMemberId(memberId);
        List<MallBrowseFootprint> footprints = findList(query);
        if (footprints.isEmpty()) {
            return;
        }
        batchDelete(footprints.stream().map(MallBrowseFootprint::getId).toList());
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteFootprint(String memberId, String spuId) {
        MallBrowseFootprintQueryVO query = new MallBrowseFootprintQueryVO();
        query.setMemberId(memberId);
        query.setSpuId(spuId);
        List<MallBrowseFootprint> footprints = findList(query);
        if (!footprints.isEmpty()) {
            delete(footprints.get(0).getId());
        }
    }
}
