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

/**
 * 房屋买卖控制器类。
 *
 * @author huangwen
 * @date 2024/11/20
 */

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.annotation.DisableDataFilter;
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.common.dict.service.GlobalDictItemService;
import com.tourism.webadmin.app.website.service.HouseSaleService;
import com.tourism.webadmin.app.website.vo.HouseFilterListsVo;
import com.tourism.webadmin.app.website.vo.HouseSaleDetailsVo;
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.TourHouseSaleInfoVo;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Tag(name = "房屋买卖管理接口")
@Slf4j
@RestController
@SaIgnore
@DisableDataFilter
@RequestMapping("/website/house/sale")
public class HouseSaleController {

    @Autowired
    private TourHouseSaleInfoService tourHouseSaleInfoService;

    @Autowired
    private HouseSaleService houseSaleService;

    @Autowired
    private ApplicationConfig applicationConfig;

    /**
     * 返回房屋过滤条件。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/viewHouseFilterLists")
    public ResponseResult<HouseFilterListsVo> viewHouseFilterLists() throws Exception {

        HouseFilterListsVo vo = houseSaleService.getHouseFilterLists();

        return ResponseResult.success(vo);
    }

    /**
     * 根据过滤条件返回房屋list。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/viewHouseListAfterFilter")
    public ResponseResult<MyPageData<TourHouseSaleInfoVo>> viewHouseListAfterFilter(TourHouseSaleInfoDto dto){

        TourHouseSaleInfo tourHouseSaleInfo = MyModelUtil.copyTo(dto, TourHouseSaleInfo.class);
        if(dto.getAreaIds() != null){
            tourHouseSaleInfo.setAreaIds(List.of(dto.getAreaIds().split(",")));
        }

        MyOrderParam myOrderParam = new MyOrderParam();
        if (dto.getSort()==null){
            myOrderParam.add(new MyOrderParam.OrderInfo("hotValue", false, null));
        }else if (dto.getSort()==1){
            myOrderParam.add(new MyOrderParam.OrderInfo("openingTime", false, null));
        }else if (dto.getSort()==2){
            myOrderParam.add(new MyOrderParam.OrderInfo("openingTime", true, null));
        }else if (dto.getSort()==3){
            myOrderParam.add(new MyOrderParam.OrderInfo("averagePrice", false, null));
        }else if (dto.getSort()==4){
            myOrderParam.add(new MyOrderParam.OrderInfo("averagePrice", true, null));
        }
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, TourHouseSaleInfo.class);
        TourHouseSaleInfo filter = MyModelUtil.copyTo(tourHouseSaleInfo, TourHouseSaleInfo.class);
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            PageMethod.startPage(dto.getPageNum(), dto.getPageSize());
        }
        MyPageData<TourHouseSaleInfoVo> pageData= houseSaleService.getHouseListAfterFilter(filter,orderBy);
        return ResponseResult.success(pageData);
    }



    /**
     * 根据id查询房屋详情。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/viewHousingDetailsById")
    public ResponseResult<HouseSaleDetailsVo> viewHousingDetailsById(@RequestParam Long id){

        HouseSaleDetailsVo detailsVo = houseSaleService.getHousingDetailsById(id);

        return ResponseResult.success(detailsVo);
    }


    /**
     * 返回房屋知识列表。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/viewHouseKnowLedgeList")
    public ResponseResult<Map<String,List<TourHoeseKnowledgeInfoVo>>> viewHouseKnowLedgeList(){

        Map<String,List<TourHoeseKnowledgeInfoVo>> map =  houseSaleService.getHouseKnowLedgeList();

        return ResponseResult.success(map);
    }


    /**
     * 新开楼盘列表。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/viewNewHouseList")
    public ResponseResult<List<TourHouseSaleInfoVo>> viewNewHouseList(){

        List<TourHouseSaleInfoVo> vos = houseSaleService.viewNewHouseList();

       return ResponseResult.success(vos);
    }


}
