package com.gic.haoban.manage.web.controller.content;

import cn.hutool.core.text.StrSpliter;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.material.ContentMaterialBaseDTO;
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.web.controller.content.adaptor.ContentMaterialAdaptor;
import com.gic.haoban.manage.web.qo.content.InteractRecordQO;
import com.gic.haoban.manage.web.vo.content.ContentMaterialInfoVO;
import com.gic.haoban.manage.web.vo.content.InteractRecordExtendInfoVO;
import com.gic.haoban.manage.web.vo.content.InteractRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 好办-互动记录
 **/
@Slf4j
@RestController
@RequestMapping(path = "/interact/record")
public class InteractRecordController {

    @Autowired
    InteractRecordApiService interactRecordApiService;
    @Autowired
    ContentMaterialAdaptor contentMaterialAdaptor;

    /**
     * 会员互动记录列表
     *
     * @return
     */
    @RequestMapping(path = "/list")
    public RestResponse<Page<InteractRecordVO>> queryInteractRecordList(InteractRecordQO interactRecordQO) {
        InteractRecordQDTO searchQdto = new InteractRecordQDTO();
        BeanUtils.copyProperties(interactRecordQO, searchQdto);
        ServiceResponse<Page<InteractRecordInfoDTO>> serviceResponse = interactRecordApiService.queryInteractRecordList(searchQdto);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        if (CollectionUtils.isEmpty(serviceResponse.getResult().getResult())) {
            return RestResponse.successResult(new Page<>());
        }
        List<Long> materialIds = serviceResponse.getResult()
                .getResult()
                .stream()
                .map(item -> Long.parseLong(item.getBizId()))
                .distinct()
                .collect(Collectors.toList());
        Map<Long, ContentMaterialBaseDTO> materialBaseMap =
                contentMaterialAdaptor.queryMaterialInfoMap(interactRecordQO.getEnterpriseId(), materialIds);
        List<InteractRecordVO> interactRecordVos = serviceResponse.getResult()
                .getResult()
                .stream()
                .map(item -> {
                    InteractRecordVO interactRecordVO = new InteractRecordVO();
                    BeanUtils.copyProperties(item, interactRecordVO);
                    ContentMaterialBaseDTO materialBaseInfo = materialBaseMap.get(Long.parseLong(item.getBizId()));
                    if (materialBaseInfo != null) {
                        ContentMaterialInfoVO temp = new ContentMaterialInfoVO();
                        BeanUtils.copyProperties(materialBaseInfo, temp);
                        if (StringUtils.isNotBlank(materialBaseInfo.getMaterialThumbnailImageUrls())) {
                            temp.setMaterialThumbnailImageUrls(StrSpliter.split(materialBaseInfo.getMaterialThumbnailImageUrls(), ",", true, true));
                        }
                        if (StringUtils.isNotBlank(materialBaseInfo.getMaterialImageUrls())) {
                            temp.setMaterialImageUrls(StrSpliter.split(materialBaseInfo.getMaterialImageUrls(), ",", true, true));
                        }
                        if (null != item.getExtendInfo()) {
                            InteractRecordExtendInfoVO extendInfoVO = EntityUtil.changeEntityByJSON(InteractRecordExtendInfoVO.class, item.getExtendInfo());
                            interactRecordVO.setExtendInfo(extendInfoVO);
                        }
                        interactRecordVO.setContentMaterialInfoVO(temp);
                    }
                    return interactRecordVO;
                })
                .collect(Collectors.toList());

        Page<InteractRecordVO> result = new Page<>();
        result.setTotalCount(serviceResponse.getResult().getTotalCount());
        result.setResult(interactRecordVos);
        return RestResponse.successResult(result);
    }

}
