package com.jeesharp.business.modules.mall.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallBargainRecordConvert;
import com.jeesharp.business.modules.mall.dto.MallBargainRecordSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallBargainRecord;
import com.jeesharp.business.modules.mall.service.MallBargainRecordService;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordListVO;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 砍价记录 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallBargainRecordApplicationService {

    private final MallBargainRecordService mallBargainRecordService;

    public MallBargainRecordInfoVO get(String id) {
        return mallBargainRecordService.getRecord(id);
    }

    public PageRespVO<MallBargainRecordListVO> findPage(Page<MallBargainRecord> page, MallBargainRecordQueryVO query) {
        return mallBargainRecordService.findRecordPage(page, query);
    }

    @Transactional(readOnly = false)
    public void save(MallBargainRecordSaveDTO dto) {
        MallBargainRecord entity = MallBargainRecordConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallBargainRecordService.createRecord(entity);
            return;
        }
        mallBargainRecordService.updateRecord(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        mallBargainRecordService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 用户发起砍价
     */
    @Transactional(readOnly = false)
    public String startBargain(String activityId, String memberId) {
        return mallBargainRecordService.startBargain(activityId, memberId);
    }

    /**
     * 获取用户的砍价记录
     */
    public PageRespVO<MallBargainRecordListVO> getUserBargainRecords(String memberId, Page<MallBargainRecord> page) {
        return mallBargainRecordService.getUserBargainRecords(memberId, page);
    }

    /**
     * 检查砍价是否已完成（达到底价）
     */
    public boolean isBargainCompleted(String recordId) {
        return mallBargainRecordService.isBargainCompleted(recordId);
    }

    /**
     * 更新砍价状态
     */
    @Transactional(readOnly = false)
    public void updateBargainStatus(String recordId, Integer status) {
        mallBargainRecordService.updateBargainStatus(recordId, status);
    }

    /**
     * 更新砍价记录
     */
    @Transactional(readOnly = false)
    public void updateBargainRecord(MallBargainRecord record) {
        mallBargainRecordService.updateBargainRecord(record);
    }

    /**
     * 计算砍价进度
     */
    public Map<String, Object> calculateBargainProgress(String recordId) {
        return mallBargainRecordService.calculateBargainProgress(recordId);
    }
}
