package com.tourism.webadmin.app.wechat.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.github.pagehelper.page.PageMethod;
import com.tourism.common.additional.config.ApplicationConfig;
import com.tourism.common.additional.utils.UrlConvertUtils;
import com.tourism.common.core.object.MyPageData;
import com.tourism.common.core.object.ResponseResult;
import com.tourism.common.core.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.webadmin.back.dto.TourTourismProjectTravelNotesDto;
import com.tourism.webadmin.back.model.DirectoryInfo;
import com.tourism.webadmin.back.model.TourTourismProjectTravelNotes;
import com.tourism.webadmin.back.model.TourismProject;
import com.tourism.webadmin.back.service.TourTourismProjectTravelNotesService;
import com.tourism.webadmin.back.service.TourismProjectService;
import com.tourism.webadmin.back.vo.TourTourismProjectTravelNotesVo;
import com.tourism.webadmin.back.vo.TourismProjectVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Tag(name = "小程序旅游游记板块接口")
@Slf4j
@RestController
@RequestMapping("/wechat/tour/travel")
@SaIgnore
public class WechatTourTravelNoteController {
    @Autowired
    private TourTourismProjectTravelNotesService tourTourTravelNotesService;
    @Autowired
    private TourismProjectService tourismProjectService;
    @Autowired
    private ApplicationConfig applicationConfig;

    /**
     * 游记列表接口
     * @param tourTourismProjectTravelNotesDto
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(TourTourismProjectTravelNotesDto tourTourismProjectTravelNotesDto) {

        if(tourTourismProjectTravelNotesDto.getBelongTab() !=null && tourTourismProjectTravelNotesDto.getBelongTab()>=10 && tourTourismProjectTravelNotesDto.getBelongTab()<1000){
            DirectoryInfo filter = new DirectoryInfo();
            filter.setParentId(tourTourismProjectTravelNotesDto.getBelongTab());
            filter.setEnable(1);
            List<DirectoryInfo> directoryInfoList = tourismProjectService.getDirectoryInfoList(filter, null);
            tourTourismProjectTravelNotesDto.setBelongTab(null);
            tourTourismProjectTravelNotesDto.setDirectoryInfoIds(directoryInfoList.stream().map(DirectoryInfo::getId).collect(toList()));
        }
        TourTourismProjectTravelNotes tourTourismProjectTravelNotes = MyModelUtil.copyTo(tourTourismProjectTravelNotesDto, TourTourismProjectTravelNotes.class);
        tourTourismProjectTravelNotes.setEnable(1);
        if (tourTourismProjectTravelNotesDto != null) {
            PageMethod.startPage(tourTourismProjectTravelNotesDto.getPageNum(), tourTourismProjectTravelNotesDto.getPageSize(), true);
        }
        List<TourTourismProjectTravelNotes> tourTourismProjectTravelNotesListWithRelation = tourTourTravelNotesService.getTourTourismProjectTravelNotesListWithRelation(tourTourismProjectTravelNotes, null);
        MyPageData<TourTourismProjectTravelNotesVo> tourismProjectVoMyPageData = MyPageUtil.makeResponseData(tourTourismProjectTravelNotesListWithRelation, TourTourismProjectTravelNotesVo.class);
        List<TourTourismProjectTravelNotesVo> dataList = tourismProjectVoMyPageData.getDataList();
        //先把imgUrl由jaon转换为List<FileUrlObject>
        dataList.stream().forEach(item ->
        {
            List<String> arrayList2 = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getHomeHotPicture());
            item.setHomeHotPicturesAfterConvert(arrayList2);
            List<String> arrayList3 = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getTourismUrl());
            item.setTourismUrlsAfterConvert(arrayList3);
        });
        return ResponseResult.success(tourismProjectVoMyPageData);

    }

}
