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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallProductQaConvert;
import com.jeesharp.business.modules.mall.dto.MallProductQaSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallProductQa;
import com.jeesharp.business.modules.mall.service.MallProductQaService;
import com.jeesharp.business.modules.mall.vo.MallProductQaInfoVO;
import com.jeesharp.business.modules.mall.vo.MallProductQaListVO;
import com.jeesharp.business.modules.mall.vo.MallProductQaQueryVO;
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;

/**
 * 商品问答 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallProductQaApplicationService {

    private final MallProductQaService mallProductQaService;

    public MallProductQaInfoVO get(String id) {
        return MallProductQaConvert.INSTANCE.toInfoVO(mallProductQaService.get(id));
    }

    public PageRespVO<MallProductQaListVO> findPage(Page<MallProductQa> page, MallProductQaQueryVO query) {
        Page<MallProductQa> result = mallProductQaService.findPage(page, query);
        return MallProductQaConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallProductQaSaveDTO dto) {
        MallProductQa entity = MallProductQaConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallProductQaService.createQa(entity);
            return;
        }
        mallProductQaService.updateQa(entity);
    }

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

    /**
     * 用户提问
     */
    @Transactional(readOnly = false)
    public void addQuestion(String memberId, String spuId, String question, Integer isAnonymous) {
        mallProductQaService.addQuestion(memberId, spuId, question, isAnonymous);
    }

    /**
     * 商家回复
     */
    @Transactional(readOnly = false)
    public void reply(String qaId, String answer, String replyBy) {
        mallProductQaService.reply(qaId, answer, replyBy);
    }

    /**
     * 获取商品问答列表
     */
    public PageRespVO<MallProductQaListVO> getProductQaList(String spuId, Page<MallProductQa> page) {
        PageRespVO<MallProductQa> result = mallProductQaService.getProductQaList(spuId, page);
        PageRespVO<MallProductQaListVO> resp = new PageRespVO<>();
        resp.setRecords(MallProductQaConvert.INSTANCE.toListVO(result.getRecords()));
        resp.setTotal(result.getTotal());
        resp.setPageNo(result.getPageNo());
        resp.setPageSize(result.getPageSize());
        return resp;
    }
}
