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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallBargainHelpConvert;
import com.jeesharp.business.modules.mall.dto.MallBargainHelpSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallBargainHelp;
import com.jeesharp.business.modules.mall.service.MallBargainHelpService;
import com.jeesharp.business.modules.mall.vo.MallBargainHelpInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainHelpListVO;
import com.jeesharp.business.modules.mall.vo.MallBargainHelpQueryVO;
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.math.BigDecimal;
import java.util.List;

/**
 * 砍价助力 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallBargainHelpApplicationService {

    private final MallBargainHelpService mallBargainHelpService;

    public MallBargainHelpInfoVO get(String id) {
        return MallBargainHelpConvert.INSTANCE.toInfoVO(mallBargainHelpService.get(id));
    }

    public PageRespVO<MallBargainHelpListVO> findPage(Page<MallBargainHelp> page, MallBargainHelpQueryVO query) {
        return mallBargainHelpService.findHelpPage(page, query);
    }

    @Transactional(readOnly = false)
    public void save(MallBargainHelpSaveDTO dto) {
        MallBargainHelp entity = MallBargainHelpConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallBargainHelpService.createHelp(entity);
            return;
        }
        mallBargainHelpService.updateHelp(entity);
    }

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

    /**
     * 好友助力砍价
     */
    @Transactional(readOnly = false)
    public BigDecimal helpBargain(String recordId, String memberId, Integer helpType) {
        return mallBargainHelpService.helpBargain(recordId, memberId, helpType);
    }

    /**
     * 获取砍价助力列表
     */
    public List<MallBargainHelpInfoVO> getBargainHelps(String recordId) {
        return mallBargainHelpService.getBargainHelps(recordId);
    }
}
