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

import com.jeesharp.business.modules.mall.convert.MallBargainActivityConvert;
import com.jeesharp.business.modules.mall.entity.MallBargainActivity;
import com.jeesharp.business.modules.mall.mapper.MallBargainActivityMapper;
import com.jeesharp.business.modules.mall.service.MallBargainActivityService;
import com.jeesharp.business.modules.mall.vo.MallBargainActivityInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainActivityListVO;
import com.jeesharp.business.modules.mall.vo.MallBargainActivityQueryVO;
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 实现类
 */
@Service
@Transactional(readOnly = true)
public class MallBargainActivityServiceImpl extends CrudService<MallBargainActivityMapper, MallBargainActivity, MallBargainActivityQueryVO> implements MallBargainActivityService {

    private static final int ENABLED_STATUS = 1;
    private static final String IDS_SEPARATOR = ",";

    @Override
    public MallBargainActivityInfoVO getActivity(String id) {
        MallBargainActivity activity = get(id);
        MallBargainActivityInfoVO vo = MallBargainActivityConvert.INSTANCE.toInfoVO(activity);
        if (vo != null) {
            vo.setIsActive(isActivityActive(activity));
        }
        return vo;
    }

    @Override
    public PageRespVO<MallBargainActivityListVO> findActivityPage(Page<MallBargainActivity> page, MallBargainActivityQueryVO query) {
        Page<MallBargainActivity> result = findPage(page, query);
        PageRespVO<MallBargainActivityListVO> pageResp = new PageRespVO<>();
        List<MallBargainActivityListVO> list = result.getRecords().stream().map(activity -> {
            MallBargainActivityListVO vo = MallBargainActivityConvert.INSTANCE.toListSingleVO(activity);
            vo.setIsActive(isActivityActive(activity));
            return vo;
        }).toList();
        pageResp.setRecords(list);
        return pageResp;
    }

    @Override
    @Transactional(readOnly = false)
    public void createActivity(MallBargainActivity entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateActivity(MallBargainActivity entity) {
        update(entity);
    }

    @Override
    public List<MallBargainActivity> getActiveActivities() {
        MallBargainActivityQueryVO query = new MallBargainActivityQueryVO();
        query.setStatus(ENABLED_STATUS);
        return findList(query).stream().filter(this::isActivityActive).toList();
    }

    @Override
    public MallBargainActivity getBySkuId(String skuId) {
        MallBargainActivityQueryVO query = new MallBargainActivityQueryVO();
        query.setSkuId(skuId);
        query.setStatus(ENABLED_STATUS);
        return findList(query).stream().filter(this::isActivityActive).findFirst().orElse(null);
    }

    private boolean isActivityActive(MallBargainActivity activity) {
        if (activity == null || activity.getStatus() != ENABLED_STATUS) {
            return false;
        }
        Date now = new Date();
        return now.after(activity.getStartTime()) && now.before(activity.getEndTime());
    }
}
