package com.gic.haoban.manage.service.service.out.impl.content;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.dto.qdto.content.InteractRecordQDTO;
import com.gic.haoban.manage.api.enums.content.InteractRecordExtendInfoDTO;
import com.gic.haoban.manage.api.enums.content.InteractRecordInfoDTO;
import com.gic.haoban.manage.api.service.content.InteractRecordApiService;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordBO;
import com.gic.haoban.manage.service.pojo.qo.content.InteractRecordQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/29 1:12 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component("interactRecordApiService")
public class InteractRecordApiServiceImpl implements InteractRecordApiService {

    @Autowired
    private InteractRecordService interactRecordService;

    /**
     * 查询互动记录
     *
     * @param interactRecordQDTO
     * @return
     */
    @Override
    public ServiceResponse<Page<InteractRecordInfoDTO>> queryInteractRecordList(InteractRecordQDTO interactRecordQDTO) {
        Page<InteractRecordInfoDTO> result = new Page<>();
        result.setTotalCount(0);
        result.setResult(Collections.emptyList());
        InteractRecordQO searchQo = new InteractRecordQO();
        BeanUtils.copyProperties(interactRecordQDTO, searchQo);
        Page<InteractRecordBO> pageResult = interactRecordService.queryInteractRecordPage(searchQo);
        if (pageResult == null || CollectionUtils.isEmpty(pageResult.getResult())) {
            log.info("查询结果为空 {}", JSON.toJSONString(interactRecordQDTO));
            return ServiceResponse.success(result);
        }

        // 拼接描述
        // 组装扩展信息
        List<InteractRecordInfoDTO> recordInfos = pageResult.getResult()
                .stream()
                .map(item -> {
                    InteractRecordInfoDTO temp = new InteractRecordInfoDTO();
                    BeanUtils.copyProperties(item, temp);
                    temp.setInteractRecordId(item.getId());
                    if (item.getExtendInfo() != null) {
                        InteractRecordExtendInfoDTO extendInfo = new InteractRecordExtendInfoDTO();
                        BeanUtils.copyProperties(item.getExtendInfo(), extendInfo);
                        temp.setExtendInfo(extendInfo);
                    }
                    return temp;
                })
                .collect(Collectors.toList());
        result.setTotalCount(pageResult.getTotalCount());
        result.setResult(recordInfos);
        return ServiceResponse.success(result);
    }
}
