package com.tourism.webadmin.app.website.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.constant.ErrorCodeEnum;
import com.tourism.common.core.object.MyOrderParam;
import com.tourism.common.core.object.MyPageData;
import com.tourism.common.core.object.MyRelationParam;
import com.tourism.common.core.object.ResponseResult;
import com.tourism.common.core.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.common.dict.model.GlobalDictItem;
import com.tourism.common.dict.service.GlobalDictItemService;
import com.tourism.common.dict.service.GlobalDictService;
import com.tourism.common.dict.util.GlobalDictOperationHelper;
import com.tourism.webadmin.app.website.service.HouseRentService;
import com.tourism.webadmin.app.website.service.HouseSaleService;

import com.tourism.webadmin.app.website.vo.HouseFilterListsVo;
import com.tourism.webadmin.app.website.vo.HouseMenuVo;
import com.tourism.webadmin.app.website.vo.HouseSaleDetailsVo;
import com.tourism.webadmin.app.wechat.vo.tourism.IndexBannerTravelNoteVo;
import com.tourism.webadmin.back.dto.TourHouseSaleInfoDto;
import com.tourism.webadmin.back.model.*;
import com.tourism.webadmin.back.service.*;
import com.tourism.webadmin.back.vo.TourHoeseKnowledgeInfoVo;
import com.tourism.webadmin.back.vo.TourHouseAdvisorInfoVo;
import com.tourism.webadmin.back.vo.TourHouseFilesVo;
import com.tourism.webadmin.back.vo.TourHouseSaleInfoVo;
import lombok.extern.slf4j.Slf4j;

import org.intellij.lang.annotations.PrintFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 门户房屋买卖Service
 *
 * @author tourism
 */
@Slf4j
@Service("HouseSaleService")
public class HouseSaleServiceImpl implements HouseSaleService {

    @Autowired
    private TourHouseSaleInfoService tourHouseSaleInfoService;
    @Autowired
    private GlobalDictItemService globalDictItemService;
    @Autowired
    private TourHouseAreaMenuService tourHouseAreaMenuService;
    @Autowired
    private TourHouseFilesService tourHouseFilesService;
    @Autowired
    private TourHouseAdvisorInfoService tourHouseAdvisorInfoService;
    @Autowired
    private ApplicationConfig applicationConfig;
    @Autowired
    private TourHoeseKnowledgeInfoService tourHoeseKnowledgeInfoService;

    @Override
    public HouseFilterListsVo getHouseFilterLists() throws Exception {

        HouseFilterListsVo vo = new HouseFilterListsVo();

        //区域条件
        List<HouseMenuVo> houseMenuTree = tourHouseAreaMenuService.getHouseMenuTree();
        vo.setHouseMenuTree(houseMenuTree);

        //其余条件
        List<String> list = new ArrayList<>();
        list.add("OfficeType");
        list.add("HouseType");
        list.add("Orientation");
        list.add("Decoration");
        List<GlobalDictItem> dictItems = globalDictItemService.list(new LambdaQueryWrapper<GlobalDictItem>()
                .in(GlobalDictItem::getDictCode,list)
                .orderByAsc(GlobalDictItem::getShowOrder)
        );
        Map<String, List<GlobalDictItem>> elseMap = dictItems.stream().collect(Collectors.groupingBy(GlobalDictItem::getDictCode));
//        HashMap<String, List<GlobalDictItem>> elseMap = new HashMap<>();
//        for (String dictCode : list) {
//            List<GlobalDictItem> dictList = globalDictItemService.getGlobalDictItemListByDictCode(dictCode);
//            elseMap.put(dictCode, dictList);
//        }

        vo.setHouseMenuTree(houseMenuTree);
        vo.setElseMap(elseMap);

        return vo;
    }

    @Override
    public HouseSaleDetailsVo getHousingDetailsById(Long id) {

        //判断
        if (id==null) return null;
        TourHouseSaleInfo info = tourHouseSaleInfoService.getByIdWithRelation(id, MyRelationParam.full());
        if (info==null) return null;
        TourHouseSaleInfoVo vo = MyModelUtil.copyTo(info, TourHouseSaleInfoVo.class);

        if (vo.getCoverUrl()!=null){
            vo.setCoverUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), vo.getCoverUrl()));
        }
        if (vo.getCode()!=null){
            vo.setCode(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), vo.getCode()));
        }



        //房屋信息
        TourHouseFiles filter = new TourHouseFiles();
        filter.setTourHouseId(id);
        List<TourHouseFiles> list = tourHouseFilesService.getTourHouseFilesList(filter, null);
        List<TourHouseFilesVo> vos = MyModelUtil.copyCollectionTo(list, TourHouseFilesVo.class);
        HouseSaleDetailsVo detailsVo = new HouseSaleDetailsVo();
        detailsVo.setTourHouseSaleInfoVo(vo);

        List<String> housingAtlasLst = new ArrayList<>();
        List<String> surroundingAtlas = new ArrayList<>();
        List<String> communityIntroductionAtlas = new ArrayList<>();

        //详情图片
        housingAtlasLst.addAll(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), vo.getHouseUrl()));
        surroundingAtlas.addAll(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), vo.getSurroundingsUrl()));
        communityIntroductionAtlas.addAll(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), vo.getIntroductionUrl()));
        detailsVo.setHousingAtlasLst(housingAtlasLst);
        detailsVo.setSurroundingAtlas(surroundingAtlas);
        detailsVo.setCommunityIntroductionAtlas(communityIntroductionAtlas);


        TourHouseFiles filesFilter = new TourHouseFiles();
        filesFilter.setTourHouseId(id);
        List<TourHouseFiles> tourHouseFilesList = tourHouseFilesService.getTourHouseFilesList(filesFilter, null);
        List<TourHouseFilesVo> tourHouseFilesVos = MyModelUtil.copyCollectionTo(tourHouseFilesList, TourHouseFilesVo.class);
        tourHouseFilesVos.forEach(e->{
            e.setFileUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), e.getFileUrl()));
        });
        detailsVo.setHousingTypeAtlas(tourHouseFilesVos);

        TourHouseAdvisorRelation relation = new TourHouseAdvisorRelation();
        relation.setHouseId(id);
        List<TourHouseAdvisorInfo> tourHouseAdvisorInfoList =
                tourHouseAdvisorInfoService.getTourHouseAdvisorInfoListByHouseId(id, null, null, null);
        List<TourHouseAdvisorInfoVo> tourHouseAdvisorInfoVos = MyModelUtil.copyCollectionTo(tourHouseAdvisorInfoList, TourHouseAdvisorInfoVo.class);
        tourHouseAdvisorInfoVos.forEach(e->{
            e.setAvatarUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), e.getAvatarUrl()));
        });
        detailsVo.setHouseAdvisorList(tourHouseAdvisorInfoVos);


//        //图集信息
//        if (vos!=null &&!vos.isEmpty()){
//
//            for (TourHouseFilesVo file : vos) {
//                if (file.getFileUrl()!=null){
//                    List<String> strings = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), file.getFileUrl());
//                    file.setFileUrlsAfterConvert(strings.get(0));
//                    if (file.getFileType()==0){
//                        housingAtlasLst.add(file);
//                    }else if (file.getFileType()==1){
//                        housingTypeAtlas.add(file);
//                    }else if (file.getFileType()==2){
//                        surroundingAtlas.add(file);
//                    }else if (file.getFileType()==3){
//                        communityIntroductionAtlas.add(file);
//                    }
//                }
//            }


//        }

//        //顾问
//        if (StringUtils.isNotBlank(vo.getAdvisorIds())){
//        String[] ids = vo.getAdvisorIds().split(",");
//        List<TourHouseAdvisorInfoVo> infoList = new ArrayList<TourHouseAdvisorInfoVo>();
//        for (String s : ids) {
//            TourHouseAdvisorInfo filter1 = new TourHouseAdvisorInfo();
//            filter1.setEnable(1);
//            filter1.setId(Long.parseLong(s));
//            List<TourHouseAdvisorInfo> advisorInfos = tourHouseAdvisorInfoService.getTourHouseAdvisorInfoList(filter1, null);
//            if (advisorInfos!=null&&!advisorInfos.isEmpty()){
//                TourHouseAdvisorInfo info1 = advisorInfos.get(0);
//                TourHouseAdvisorInfoVo infoVo = MyModelUtil.copyTo(info1, TourHouseAdvisorInfoVo.class);
//                    if (infoVo.getAvatarUrl()!=null){
//                        String string = UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), infoVo.getAvatarUrl());
//                        infoVo.setAvatarUrl(string);
//                    }
//                infoList.add(infoVo);
//            }
//        }
//            detailsVo.setHouseAdvisorList(infoList);
//        }
        return detailsVo;
    }

    @Override
    public Map<String, List<TourHoeseKnowledgeInfoVo>> getHouseKnowLedgeList() {

        //房屋知识类型
        List<GlobalDictItem> fullResultList = globalDictItemService.getGlobalDictItemListByDictCode("HouseKnowledgeType");

        HashMap<String, List<TourHoeseKnowledgeInfoVo>> map = new HashMap<>();

        TourHoeseKnowledgeInfo filter = new TourHoeseKnowledgeInfo();
        filter.setEnable(1);
        List<TourHoeseKnowledgeInfo> list = tourHoeseKnowledgeInfoService.getTourHoeseKnowledgeInfoList(filter, null);
        List<TourHoeseKnowledgeInfoVo> vos = MyModelUtil.copyCollectionTo(list, TourHoeseKnowledgeInfoVo.class);

        for (GlobalDictItem item : fullResultList) {
            ArrayList<TourHoeseKnowledgeInfoVo> infoVos = new ArrayList<>();
            for (TourHoeseKnowledgeInfoVo vo : vos) {
                if (Long.parseLong(item.getItemId())==vo.getType()){
                    infoVos.add(vo);
                }
            }
            map.put(item.getItemName(),infoVos);
        }
        return map;
        }

    @Override
    public MyPageData<TourHouseSaleInfoVo> getHouseListAfterFilter(TourHouseSaleInfo filter,String order) {
        List<TourHouseSaleInfo> list = tourHouseSaleInfoService.getTourHouseSaleInfoListWithArea(filter,order);
        MyPageData<TourHouseSaleInfoVo> pageData = MyPageUtil.makeResponseData(list, TourHouseSaleInfoVo.class);
        if (pageData!=null){
            for (TourHouseSaleInfoVo o : pageData.getDataList()) {
                if (o.getCoverUrl()!=null){
                    o.setCoverUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), o.getCoverUrl()));
                }
                if (o.getCode()!=null){
                    o.setCode(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), o.getCode()));
                }
            }
        }
        return pageData;
    }

    @Override
    public List<TourHouseSaleInfoVo> viewNewHouseList() {
        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("openingTime", false, null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, TourHouseSaleInfo.class);
        PageMethod.startPage(1,4,true);
        TourHouseSaleInfo filter = new TourHouseSaleInfo();
        filter.setHouseSourceType(0);
        List<TourHouseSaleInfo> list = tourHouseSaleInfoService.getTourHouseSaleInfoList(filter, orderBy);
        List<TourHouseSaleInfoVo> dataList = MyPageUtil.makeResponseData(list, TourHouseSaleInfoVo.class).getDataList();
        dataList.stream().forEach(o->{
            o.setCoverUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), o.getCoverUrl()));
            o.setCode(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), o.getCode()));
        });
        return dataList;
    }
}
