package com.tourism.webadmin.app.website.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.model.FileUrlObject;
import com.tourism.common.additional.utils.JsonUtils;
import com.tourism.common.additional.utils.StringUtils;
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.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.log.annotation.OperationLog;
import com.tourism.common.log.model.constant.SysOperationLogType;
import com.tourism.webadmin.app.website.service.HouseRentService;
import com.tourism.webadmin.app.website.vo.HouseFilterListsVo;
import com.tourism.webadmin.back.dto.TourHouseRentInfoDto;
import com.tourism.webadmin.back.dto.TourHouseSaleInfoDto;
import com.tourism.webadmin.back.model.TourHouseRentInfo;
import com.tourism.webadmin.back.model.TourHouseSaleInfo;
import com.tourism.webadmin.back.model.TourUserLikeTravelNotes;
import com.tourism.webadmin.back.service.TourHouseRentInfoService;
import com.tourism.webadmin.back.vo.TourHouseAdvisorInfoVo;
import com.tourism.webadmin.back.vo.TourHouseEquipmentVo;
import com.tourism.webadmin.back.vo.TourHouseRentInfoVo;
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.List;
import java.util.Map;

/**
 * 租房信息接口。
 *
 * @author chenchen
 * @date 2024-11-22
 */
@Tag(name = "租房信息接口")
@Slf4j
@RestController
@SaIgnore
@RequestMapping("/website/tourism/tourismHouseRent")
public class TourismHouseRentController {

    @Autowired
    private TourHouseRentInfoService tourHouseRentInfoService;
    @Autowired
    private HouseRentService houseRentService;
    @Autowired
    private ApplicationConfig applicationConfig;

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

        HouseFilterListsVo vo = houseRentService.getHouseFilterLists();

        return ResponseResult.success(vo);
    }

    /**
     * 租房列表。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/tourHouseRentInfoList")
    public ResponseResult<MyPageData<TourHouseRentInfoVo>> tourHouseRentInfoList(TourHouseRentInfoDto tourHouseRentInfoDto) {

        TourHouseRentInfo tourHouseRentInfo = MyModelUtil.copyTo(tourHouseRentInfoDto, TourHouseRentInfo.class);
        if(tourHouseRentInfoDto.getAreaIds() != null){
            tourHouseRentInfo.setAreaIds(List.of(tourHouseRentInfoDto.getAreaIds().split(",")));
        }
        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("showOrder", true, null));
        myOrderParam.add(new MyOrderParam.OrderInfo("updateTime", false, null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, TourHouseRentInfo.class);
        if(tourHouseRentInfoDto.getPageNum() != null && tourHouseRentInfoDto.getPageSize() != null){
            PageMethod.startPage(tourHouseRentInfoDto.getPageNum(), tourHouseRentInfoDto.getPageSize(), true);
        }

        List<TourHouseRentInfo> tourHouseRentInfoList = tourHouseRentInfoService.getTourHouseRentInfoListWithArea(tourHouseRentInfo, orderBy);
        MyPageData<TourHouseRentInfoVo> tourHouseRentInfoVoMyPageData = MyPageUtil.makeResponseData(tourHouseRentInfoList, TourHouseRentInfoVo.class);
        List<TourHouseRentInfoVo> dataList = tourHouseRentInfoVoMyPageData.getDataList();
        for(TourHouseRentInfoVo item:dataList){
            if(StringUtils.isNotBlank(item.getCoverUrl())){
                item.setCoverUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), item.getCoverUrl()));
            }
        }
        return ResponseResult.success(tourHouseRentInfoVoMyPageData);
    }

    /**
     * 根据租房id获取租房详情。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @OperationLog(type = SysOperationLogType.LIST)
    @GetMapping("/tourHouseRentInfoById")
    public ResponseResult<TourHouseRentInfoVo> tourHouseRentInfoById(@RequestParam Long id) {

        TourHouseRentInfo tourHouseRentInfo = tourHouseRentInfoService.getByIdWithRelation(id, MyRelationParam.full());
        TourHouseRentInfoVo tourHouseRentInfoVo = MyModelUtil.copyTo(tourHouseRentInfo, TourHouseRentInfoVo.class);
        //转换封面图
        if(StringUtils.isNotBlank(tourHouseRentInfoVo.getCoverUrl())){
            tourHouseRentInfoVo.setCoverUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), tourHouseRentInfoVo.getCoverUrl()));
        }
        if(tourHouseRentInfoVo.getTourHouseEquipment() != null){
            TourHouseEquipmentVo tourHouseEquipment = tourHouseRentInfoVo.getTourHouseEquipment();
            if(StringUtils.isNotBlank(tourHouseEquipment.getHouseDetailImage())){
                tourHouseEquipment.setHouseDetailConvertImage(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), tourHouseEquipment.getHouseDetailImage()));
            }
            if(StringUtils.isNotBlank(tourHouseEquipment.getSurroundingSupportImage())){
                tourHouseEquipment.setSurroundingSupportConvertImage(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), tourHouseEquipment.getSurroundingSupportImage()));
            }
            if(StringUtils.isNotBlank(tourHouseEquipment.getPlotIntroductionImage())){
                tourHouseEquipment.setPlotIntroductionConvertImage(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), tourHouseEquipment.getPlotIntroductionImage()));
            }
        }
        if(tourHouseRentInfoVo.getTourHouseAdvisorInfo() != null){
            TourHouseAdvisorInfoVo tourHouseAdvisorInfo = tourHouseRentInfoVo.getTourHouseAdvisorInfo();
            if(StringUtils.isNotBlank(tourHouseAdvisorInfo.getAvatarUrl())){
                tourHouseAdvisorInfo.setAvatarUrl(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), tourHouseAdvisorInfo.getAvatarUrl()));
            }
            if(StringUtils.isNotBlank(tourHouseAdvisorInfo.getCode())){
                tourHouseAdvisorInfo.setCovertCode(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(), tourHouseAdvisorInfo.getCode()));
            }
        }
        return ResponseResult.success(tourHouseRentInfoVo);
    }
}
