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

import cn.dev33.satoken.annotation.SaIgnore;
import com.github.pagehelper.page.PageMethod;
import com.tourism.common.additional.utils.UrlConvertUtils;
import com.tourism.common.core.object.MyOrderParam;
import com.tourism.common.core.object.MyPageData;
import com.tourism.common.core.object.ResponseResult;
import com.tourism.common.core.object.TokenData;
import com.tourism.common.core.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.common.log.annotation.OperationLog;
import com.tourism.common.log.model.constant.SysOperationLogType;
import com.tourism.common.satoken.util.SaTokenUtil;
import com.tourism.webadmin.app.website.dto.TourismProjectToWebDto;
import com.tourism.webadmin.back.model.BannerInfo;
import com.tourism.webadmin.back.model.DirectoryInfo;
import com.tourism.webadmin.back.model.Icon;
import com.tourism.webadmin.back.model.TourismProject;
import com.tourism.webadmin.back.service.*;
import com.tourism.webadmin.back.vo.BannerInfoVo;
import com.tourism.webadmin.back.vo.DirectoryInfoVo;
import com.tourism.webadmin.back.vo.IconVo;
import com.tourism.webadmin.back.vo.TourismProjectVo;
import com.tourism.common.additional.config.ApplicationConfig;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

/**
 * 小程序首页接口。
 *
 * @author chenchen
 * @date 2024-09-26
 */
@Tag(name = "小程序首页接口")
@Slf4j
@RestController
@RequestMapping("/wechat/home")
public class WechatHomeController {

    @Autowired
    private BannerInfoService bannerInfoService;
    @Autowired
    private DirectoryInfoService directoryInfoService;
    @Autowired
    private ApplicationConfig applicationConfig;
    @Autowired
    private IconService iconService;
    @Autowired
    private TourismProjectService tourismProjectService;
    @Autowired
    private TourUserService tourUserService;
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/getUserInfo")
    public ResponseResult getUserInfo() {
        TokenData tokenData = TokenData.takeFromRequest();
        return ResponseResult.success(tokenData);
    }
    /**
     * 列出符合过滤条件的banner列表。
     * @return 应答结果对象，包含查询结果集。
     */
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/bannerList")
    @SaIgnore
    public ResponseResult<MyPageData<BannerInfoVo>> bannerInfoList(@NotNull(message = "所属分类(belongTab)不能为空") Long belongTab) {
        BannerInfo bannerInfoFilter = new BannerInfo();
        bannerInfoFilter.setBelongTab(belongTab);
        //首页展示的话，只能展示启用的banner图
        bannerInfoFilter.setEnable(1);
        String orderBy = MyOrderParam.buildOrderBy(new MyOrderParam(), BannerInfo.class);
        List<BannerInfo> bannerInfoList = bannerInfoService.getBannerInfoListWithRelation(bannerInfoFilter, orderBy);
        MyPageData<BannerInfoVo> bannerInfoVoMyPageData = MyPageUtil.makeResponseData(bannerInfoList, BannerInfoVo.class);

        List<BannerInfoVo> dataList = bannerInfoVoMyPageData.getDataList();
        //先把imgUrl由jaon转换为List<FileUrlObject>
        dataList.stream().forEach(item ->
        {
            List<String> arrayList = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getImgUrl());
            item.setImgUrlsAfterConvert(arrayList);
        });
        return ResponseResult.success(bannerInfoVoMyPageData);
    }

    /**
     * 列出符合过滤条件的icon列表。
     * @return 应答结果对象，包含查询结果集。
     */
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/iconList")
    @SaIgnore
    public ResponseResult<MyPageData<IconVo>> iconList(){
        Icon filter = new Icon();
        String orderBy = "";
        List<Icon> icnoList = iconService.getIconList(filter, orderBy);
        MyPageData<IconVo> icnoInfoVoMyPageData = MyPageUtil.makeResponseData(icnoList, IconVo.class);
        List<IconVo> dataList = icnoInfoVoMyPageData.getDataList();
        if(!CollectionUtils.isEmpty(dataList)){
            dataList.stream().forEach(item->{
                List<String> arrayList = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getIcon());
                item.setIconAfterConvert(arrayList);
            });
        };
        return ResponseResult.success(icnoInfoVoMyPageData);
    }
    /**
     * 列出符合过滤条件的旅游项目管理列表。
     * 接口：热门项目(isHotspot:1)
     *
     * @param tourismProjectDtoFilter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/projectList")
    @SaIgnore
    public ResponseResult<MyPageData<TourismProjectVo>> projectList(
            @Valid @ModelAttribute TourismProjectToWebDto tourismProjectDtoFilter) {
//        if(tourismProjectDtoFilter.getBelongTab() == null){
//            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST,"所属分类(belongTab)不能为空!");
//        }
        //如果belongTab小于1000并且大于10.则表明查询的是一级菜单
        if(tourismProjectDtoFilter.getBelongTab() !=null && tourismProjectDtoFilter.getBelongTab()>=10 && tourismProjectDtoFilter.getBelongTab()<1000){
            DirectoryInfo filter = new DirectoryInfo();
            filter.setParentId(tourismProjectDtoFilter.getBelongTab());
            filter.setEnable(1);
            List<DirectoryInfo> directoryInfoList = tourismProjectService.getDirectoryInfoList(filter, null);
            tourismProjectDtoFilter.setBelongTab(null);
            tourismProjectDtoFilter.setDirectoryInfoIds(directoryInfoList.stream().map(DirectoryInfo::getId).collect(toList()));
        }
        TourismProject tourismProjectFilter = MyModelUtil.copyTo(tourismProjectDtoFilter, TourismProject.class);
        //首页展示的为启用的内容
        tourismProjectFilter.setEnable(1);
        String orderBy = MyOrderParam.buildOrderBy(tourismProjectDtoFilter.getOrderParamList(), TourismProject.class);
        if (tourismProjectDtoFilter.getPageNum() != null && tourismProjectDtoFilter.getPageSize() != null && tourismProjectDtoFilter.getCount() != null) {
            PageMethod.startPage(tourismProjectDtoFilter.getPageNum(), tourismProjectDtoFilter.getPageSize(), tourismProjectDtoFilter.getCount());
        }
        List<TourismProject> tourismProjectList =
                tourismProjectService.getTourismProjectListWithRelation(tourismProjectFilter, orderBy);
        MyPageData<TourismProjectVo> tourismProjectVoMyPageData = MyPageUtil.makeResponseData(tourismProjectList, TourismProjectVo.class);

        List<TourismProjectVo> dataList = tourismProjectVoMyPageData.getDataList();
        //先把imgUrl由jaon转换为List<FileUrlObject>
        dataList.stream().forEach(item ->{
            List<String> arrayList = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(),item.getHomeHotPicture());
            item.setHomeHotPicturesAfterConvert(arrayList);
            List<String> arrayList1 = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getTourismUrl());
            item.setTourismUrlsAfterConvert(arrayList1);
        });
        return ResponseResult.success(tourismProjectVoMyPageData);
    }

    /**
     * 列出符合过滤条件的门户菜单管理列表。
     * 接口:热门目的地 (isHotspot:1)
     * @return 应答结果对象，包含查询结果集。
     */
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/directoryList")
    @SaIgnore
    public ResponseResult<MyPageData<DirectoryInfoVo>> directoryInfoList(Long parentId, Integer isHotspot) {
//        if (ObjectUtils.isEmpty(parentId)){
//            return ResponseResult.error("NO-ERROR","父级Id为空");
//        }
        DirectoryInfo directoryInfoFilter = new DirectoryInfo();
        if(parentId != null){
            directoryInfoFilter.setParentId(parentId);
        }
        if(isHotspot != null){
            directoryInfoFilter.setIsHotspot(isHotspot);
        }
        String orderBy = MyOrderParam.buildOrderBy(new MyOrderParam(), DirectoryInfo.class);
        List<DirectoryInfo> directoryInfoList =
                directoryInfoService.getDirectoryInfoListWithRelation(directoryInfoFilter, orderBy);

        MyPageData<DirectoryInfoVo> directoryInfoVoMyPageData = MyPageUtil.makeResponseData(directoryInfoList, DirectoryInfoVo.class);

        List<DirectoryInfoVo> dataList = directoryInfoVoMyPageData.getDataList();
        //先把imgUrl由jaon转换为List<FileUrlObject>
        dataList.stream().forEach(item ->{
            List<String> arrayList1 = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getHotPictureUrl());
            item.setHotPictureUrlsAfterConvert(arrayList1);
        });

        return ResponseResult.success(directoryInfoVoMyPageData);
    }
}
