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

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
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.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.InteractRecordVO;
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.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(interactRecordVO, interactRecordVO);
                    ContentMaterialBaseDTO materialBaseInfo = materialBaseMap.get(Long.parseLong(item.getBizId()));
                    if (materialBaseInfo != null) {
                        ContentMaterialInfoVO temp = new ContentMaterialInfoVO();
                        BeanUtils.copyProperties(materialBaseInfo, item);
                        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);
    }

//    /**
//     * 查询互动记录扩展信息
//     * @param interactRecordQO
//     * @return
//     */
//    @RequestMapping(path = "/relation/extend/info")
//    public RestResponse<InteractRecordExtendInfoVO> queryInteractRecordRelationGoods(InteractRecordQO interactRecordQO) {
//        return RestResponse.successResult();
//    }

}
