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.StringUtils;
import com.tourism.common.additional.utils.UrlConvertUtils;
import com.tourism.common.core.annotation.DisableDataFilter;
import com.tourism.common.core.annotation.MyRequestBody;
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.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.webadmin.app.wechat.dto.BestNewShopDto;
import com.tourism.webadmin.app.wechat.dto.PageInfo;
import com.tourism.webadmin.app.wechat.dto.ShopFoodDto;
import com.tourism.webadmin.app.wechat.vo.shopfood.*;
import com.tourism.webadmin.back.dto.RestaurantInfoDto;
import com.tourism.webadmin.back.model.JobProject;
import com.tourism.webadmin.back.model.RestaurantFoodInfo;
import com.tourism.webadmin.back.model.RestaurantInfo;
import com.tourism.webadmin.back.service.RestaurantFoodInfoService;
import com.tourism.webadmin.back.service.RestaurantInfoService;
import com.tourism.webadmin.back.vo.RestaurantFoodInfoVo;
import com.tourism.webadmin.back.vo.RestaurantInfoVo;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * 外卖美食主页接口。
 *
 * @author huangwen
 * @date 2024/10/22
 */
@Tag(name = "外卖美食主页接口")
@Slf4j
@RestController
@SaIgnore
@DisableDataFilter
@RequestMapping("/wechat/food")
public class WechatFoodIndexController {
    @Autowired
    private RestaurantInfoService restaurantInfoService;

    @Autowired
    private RestaurantFoodInfoService restaurantFoodInfoService;

    @Autowired
    private ApplicationConfig applicationConfig;


    /**
     * 外卖美食首页banner展示。
     *
     * @return 应答结果对象。
     */
    @GetMapping("/queryIndexBannerRestaurant")
    public ResponseResult<List<WechatQueryIndexBannerRestaurantVo>> queryIndexBannerRestaurant() {
        RestaurantInfo filter = new RestaurantInfo();
        filter.setDataState(1);
        filter.setEnable(1);
        filter.setIsRecommendedShop(1);
        PageMethod.startPage(1, 4, true);

        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("recommendationRate",false,null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, RestaurantInfo.class);

        List<RestaurantInfo> infoList = restaurantInfoService.getRestaurantInfoList(filter, orderBy);

        List<WechatQueryIndexBannerRestaurantVo> list = MyModelUtil.copyCollectionTo(infoList,WechatQueryIndexBannerRestaurantVo.class);
        list.stream().forEach(o-> {
            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getRestaurantBanner());
            if (!strings.isEmpty()){
                o.setRestaurantBannerAfterConvert(strings.get(0));
            }
            });
        return ResponseResult.success(list);
    }


    /**
     * 超火美食首页4条
     * @return 应答结果对象。
     */
    @GetMapping("/queryIndexHotFood")
    public ResponseResult<List<RestaurantFoodInfoVo>> queryIndexHotFood() {
        RestaurantFoodInfo filter = new RestaurantFoodInfo();
        filter.setDataState(1);
        filter.setEnable(1);
        filter.setIsHotFood(1);

//        MyOrderParam myOrderParam = new MyOrderParam();
//        myOrderParam.add(new MyOrderParam.OrderInfo("totalSales",false,null));
//        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, RestaurantInfo.class);

        PageMethod.startPage(1, 4, true);
        List<RestaurantFoodInfo> foodInfoList = restaurantFoodInfoService.getRestaurantFoodInfoList(filter, null);
        MyPageData<RestaurantFoodInfoVo> pageData = MyPageUtil.makeResponseData(foodInfoList, RestaurantFoodInfoVo.class);
        List<RestaurantFoodInfoVo> list = pageData.getDataList();
        list.stream().forEach(o->{
            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
            o.setUrlAfterConvert(strings);
        });
        return ResponseResult.success(list);
    }

//    /**
//     * 超火美食列表
//     * @return 应答结果对象。
//     */
//    @GetMapping("/queryHotFoodList")
//    public ResponseResult<MyPageData<RestaurantFoodInfoVo>> queryHotFoodList( PageInfo pageInfo) {
//        RestaurantFoodInfo filter = new RestaurantFoodInfo();
//        filter.setDataState(1);
//        filter.setEnable(1);
//        filter.setIsHotFood(1);
//        if (pageInfo.getPageNum() != null && pageInfo.getPageSize() != null && pageInfo.getCount() != null) {
//            PageMethod.startPage(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getCount());
//        }
//        List<RestaurantFoodInfo> foodInfoList = restaurantFoodInfoService.getRestaurantFoodInfoList(filter, null);
//        MyPageData<RestaurantFoodInfoVo> pageData = MyPageUtil.makeResponseData(foodInfoList, RestaurantFoodInfoVo.class);
//        List<RestaurantFoodInfoVo> list = pageData.getDataList();
//        list.stream().forEach(o->{
//            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
//            o.setUrlAfterConvert(strings);
//        });
//        return  ResponseResult.success(pageData);
//    }

    /**
     * 美味店铺首页
     *
     * @return 应答结果对象。
     */
    @GetMapping("/queryIndexDeliciousShop")
    public ResponseResult<List<RestaurantInfoVo>> queryIndexDeliciousShop() {
        PageMethod.startPage(1,3,null);
        RestaurantInfo filter = new RestaurantInfo();
        filter.setDataState(1);
        filter.setEnable(1);
        filter.setIsDeliciousShop(1);

        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("recommendationRate",false,null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, RestaurantInfo.class);

        List<RestaurantInfo> list = restaurantInfoService.getRestaurantInfoList(filter, orderBy);
        List<RestaurantInfoVo> voList = MyPageUtil.makeResponseData(list,RestaurantInfoVo.class).getDataList();
        voList.stream().forEach(o-> {
            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
            o.setUrlAfterConvert(strings);
        });
        return ResponseResult.success(voList);
    }

    /**
     * 精选好店首页
     *
     * @return 应答结果对象。
     */
    @GetMapping("/queryIndexSelectShop")
    public ResponseResult<MyPageData<WechatSelectShopVo>> queryIndexSelectShop(BestNewShopDto bestNewShopVoDto) {

        RestaurantInfo filter = new RestaurantInfo();
        filter.setDataState(1);
        filter.setEnable(1);

        Integer orderNum = 0;
        if (bestNewShopVoDto.getIsSelectShop()!=null &&bestNewShopVoDto.getIsSelectShop()==1){
            filter.setIsSelectShop(1);
            orderNum = 1;
        }
        if (bestNewShopVoDto.getIsBestNewShop()!=null &&bestNewShopVoDto.getIsBestNewShop()==1){
            filter.setIsBestNewShop(1);
            orderNum = 2;
        }

        //店铺
        if (bestNewShopVoDto.getPageNum() != null && bestNewShopVoDto.getPageSize() != null && bestNewShopVoDto.getCount() != null) {
            PageMethod.startPage(bestNewShopVoDto.getPageNum(), bestNewShopVoDto.getPageSize(), bestNewShopVoDto.getCount());
        }

        MyOrderParam myOrderParam = new MyOrderParam();
        if (orderNum==1) myOrderParam.add(new MyOrderParam.OrderInfo("totalSales",false,null));
        if (orderNum==2) myOrderParam.add(new MyOrderParam.OrderInfo("createTime",false,null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, RestaurantInfo.class);

        List<RestaurantInfo> list = restaurantInfoService.getRestaurantInfoList(filter, orderBy);
        MyPageData<WechatSelectShopVo> pageData = MyPageUtil.makeResponseData(list, WechatSelectShopVo.class);
        if (pageData.getDataList() == null || pageData.getDataList().isEmpty()) {
            return ResponseResult.success(null);
        }
        List<WechatSelectShopVo> dataList = pageData.getDataList();
        for (WechatSelectShopVo shopVo : dataList) {
            if(shopVo.getTag()!=null||!shopVo.getTag().equals("")){
                String[] tags = shopVo.getTag().split("&");
                shopVo.setTags(tags);
            }
            //json->图片
            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), shopVo.getUrl());
            shopVo.setUrlAfterConvert(strings);
            //美食列表
            PageMethod.startPage(1,3,null);
            RestaurantFoodInfo filter1 = new RestaurantFoodInfo();
            filter1.setEnable(1);
            filter1.setDataState(1);
            filter1.setRestaurantId(shopVo.getId());
            List<RestaurantFoodInfo> infoList = restaurantFoodInfoService.getRestaurantFoodInfoList(filter1, null);
            List<SelectShopFoodVo> foodVos = MyPageUtil.makeResponseData(infoList, SelectShopFoodVo.class).getDataList();

            foodVos.stream().forEach(o->{

                List<String> strings1 = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
                o.setUrlAfterConvert(strings1);
            });
            foodVos.sort(new Comparator<SelectShopFoodVo>() {
                @Override
                public int compare(SelectShopFoodVo o1, SelectShopFoodVo o2) {
                    return o2.getSales().compareTo(o1.getSales());
                }
            });
            shopVo.setFoodList(foodVos);
        }

        return ResponseResult.success(pageData);
    }


    /**
     * 优选新店四个
     *
     * @return 应答结果对象。
     */
    @GetMapping("/queryBestNewShop")
    public ResponseResult<List<WechatBestNewShopVo>> queryBestNewShop() {

        RestaurantInfo filter = new RestaurantInfo();
        filter.setDataState(1);
        filter.setEnable(1);
        filter.setIsBestNewShop(1);

        PageMethod.startPage(1,4,null);
        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("createTime",false,null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, RestaurantInfo.class);

        List<RestaurantInfo> infoList = restaurantInfoService.getRestaurantInfoList(filter, orderBy);
        List<WechatBestNewShopVo> shopVos = MyPageUtil.makeResponseData(infoList, WechatBestNewShopVo.class).getDataList();
        shopVos.stream().forEach(o->{
            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
            o.setUrlAfterConvert(strings);
        });
        shopVos.sort(new Comparator<WechatBestNewShopVo>() {

            @Override
            public int compare(WechatBestNewShopVo o1, WechatBestNewShopVo o2) {
                if (Objects.isNull(o2.getTotalSales()) || Objects.isNull(o1.getTotalSales())) {
                    return 1;
                }
                return o2.getTotalSales().compareTo(o1.getTotalSales());
            }
        });
        return ResponseResult.success(shopVos);
    }

    /**
     * 优选新店Go 优选新店列表时间倒序
     *
     * @return 应答结果对象。
     */
    @GetMapping("/queryBestNewShopList")
    public ResponseResult<MyPageData<WechatBestNewShopVo>> queryBestNewShopList(PageInfo pageInfo) {
        RestaurantInfo filter = new RestaurantInfo();
        filter.setDataState(1);
        filter.setEnable(1);
        filter.setIsBestNewShop(1);
        if (pageInfo.getPageNum() != null && pageInfo.getPageSize() != null && pageInfo.getCount() != null) {
            PageMethod.startPage(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getCount());
        }

        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("createTime",false,null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, RestaurantInfo.class);

        List<RestaurantInfo> infoList = restaurantInfoService.getRestaurantInfoList(filter, orderBy);
        MyPageData<WechatBestNewShopVo> pageData = MyPageUtil.makeResponseData(infoList, WechatBestNewShopVo.class);
        List<WechatBestNewShopVo> shopVos = pageData.getDataList();

        shopVos.stream().forEach(o->{
            List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
            o.setUrlAfterConvert(strings);
        });
        return ResponseResult.success(pageData);
    }

//    /**
//     * 优选新店Go id拿优选新店详情和美食列表
//     *
//     * @return 应答结果对象。
//     */
//    @GetMapping("/queryBestNewShopAndFoodList")
//    public ResponseResult<WechatShopAndFoodsVo> queryBestNewShopAndFoodList(ShopFoodDto shopFoodDto) {
//
//        RestaurantInfo filter = new RestaurantInfo();
//        filter.setEnable(1);
//        filter.setDataState(1);
//        filter.setId(shopFoodDto.getRestaurantId());
//        RestaurantInfo restaurantInfo = restaurantInfoService.getRestaurantInfoList(filter, null).get(0);
//        RestaurantInfoVo restaurantInfoVo = MyModelUtil.copyTo(restaurantInfo, RestaurantInfoVo.class);
//        List<String> strings1 = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), restaurantInfoVo.getUrl());
//        restaurantInfoVo.setUrlAfterConvert(strings1);
//
//        RestaurantFoodInfo filter1 = MyModelUtil.copyTo(shopFoodDto, RestaurantFoodInfo.class);
//        filter1.setDataState(1);
//        filter1.setEnable(1);
//        filter1.setRestaurantId(shopFoodDto.getRestaurantId());
//
//        if (shopFoodDto.getPageNum() != null && shopFoodDto.getPageSize() != null && shopFoodDto.getCount() != null) {
//            PageMethod.startPage(shopFoodDto.getPageNum(), shopFoodDto.getPageSize(), shopFoodDto.getCount());
//        }
//
//        List<RestaurantFoodInfo> infoList = restaurantFoodInfoService.getRestaurantFoodInfoList(filter1, null);
//        MyPageData<RestaurantFoodInfoVo> pageData = MyPageUtil.makeResponseData(infoList, RestaurantFoodInfoVo.class);
//        List<RestaurantFoodInfoVo> shopVos = pageData.getDataList();
//
//        shopVos.stream().forEach(o->{
//            if(StringUtils.isNotEmpty(o.getUrl())) {
//                List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
//                o.setUrlAfterConvert(strings);
//            }
//        });
//
//        WechatShopAndFoodsVo vo = new WechatShopAndFoodsVo();
//        vo.setRestaurantFoodInfoVoMyPageData(pageData);
//        vo.setRestaurantInfoVo(restaurantInfoVo);
//
//        return ResponseResult.success(vo);
//    }


    /**
     * id拿美食列表
     *
     * @return 应答结果对象。
     */
    @GetMapping("/queryRestaurantFoodListById")
    public ResponseResult<MyPageData<RestaurantFoodInfoVo>> queryBestNewShopAndFoodList(ShopFoodDto shopFoodDto) {

        RestaurantFoodInfo filter1 = MyModelUtil.copyTo(shopFoodDto, RestaurantFoodInfo.class);
        filter1.setDataState(1);
        filter1.setEnable(1);
        filter1.setRestaurantId(shopFoodDto.getRestaurantId());

        if (shopFoodDto.getPageNum() != null && shopFoodDto.getPageSize() != null) {
            PageMethod.startPage(shopFoodDto.getPageNum(), shopFoodDto.getPageSize(), true);
        }

        List<RestaurantFoodInfo> infoList = restaurantFoodInfoService.getRestaurantFoodInfoList(filter1, null);
        MyPageData<RestaurantFoodInfoVo> pageData = MyPageUtil.makeResponseData(infoList, RestaurantFoodInfoVo.class);
        List<RestaurantFoodInfoVo> infoVos = pageData.getDataList();

        infoVos.stream().forEach(o->{
            if(StringUtils.isNotEmpty(o.getUrl())) {
                List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), o.getUrl());
                o.setUrlAfterConvert(strings);
            }
        });

        return ResponseResult.success(pageData);
    }

}
