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

import cn.dev33.satoken.annotation.SaCheckPermission;
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.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.webadmin.app.website.dto.TourCarCategoryToWebDto;
import com.tourism.webadmin.app.website.dto.TourCarModelToWebDto;
import com.tourism.webadmin.back.model.TourCarCategory;
import com.tourism.webadmin.back.model.TourCarDriver;
import com.tourism.webadmin.back.model.TourCarModel;
import com.tourism.webadmin.back.service.TourCarCategoryService;
import com.tourism.webadmin.back.service.TourCarContractMenuService;
import com.tourism.webadmin.back.service.TourCarModelService;
import com.tourism.webadmin.back.util.QueryConditionProccessUtils;
import com.tourism.webadmin.back.vo.TourCarCategoryVo;
import com.tourism.webadmin.back.vo.TourCarContractMenuVo;
import com.tourism.webadmin.back.vo.TourCarModelVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * 包车车型操作控制器类。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Tag(name = "门户包车车型接口")
@Slf4j
@RestController
@DisableDataFilter
@RequestMapping("/website/app/tourCarCategory")
public class WebsiteCarCategoryController {
    @Autowired
    private TourCarCategoryService tourCarCategoryService;
    @Autowired
    private TourCarContractMenuService tourCarContractMenuService;
    @Autowired
    private TourCarModelService tourCarModelService;
    @Autowired
    private ApplicationConfig config;

    /**
     * 查询全部车辆类型。
     *
     * @param filter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaIgnore
    @GetMapping("/carModelList")
    public ResponseResult<MyPageData<TourCarModelVo>> list(TourCarModelToWebDto filter) {
        if (filter != null) {
            PageMethod.startPage(filter.getPageNum(), filter.getPageSize(), filter.getCount());
        }
        TourCarModel tourCarModelFilter = MyModelUtil.copyTo(filter, TourCarModel.class);

        // 根据排序字段进行正序排列
        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("showOrder", true, null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, TourCarModel.class);
        List<TourCarModel> tourCarModelList =
                tourCarModelService.getTourCarModelListWithRelation(tourCarModelFilter, orderBy);
        // 图片URL转换
        tourCarModelList.forEach(e->{
            e.setTypeUrl(UrlConvertUtils.urlConvertSingle(config.getHostIpPort(), e.getTypeUrl()));
        });
        return ResponseResult.success(MyPageUtil.makeResponseData(tourCarModelList, TourCarModelVo.class));
    }

    /**
     * 返回包车树。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @SaIgnore
    @GetMapping("/carContractTree")
    public ResponseResult<List<TourCarContractMenuVo>> viewHouseFilterLists() throws Exception {
        return ResponseResult.success(
                tourCarContractMenuService.getCarContractMenueTree()
        );
    }

    /**
     * 列出符合过滤条件的包车车型列表。
     *
     * @param filter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaIgnore
    @GetMapping("/list")
    public ResponseResult<MyPageData<TourCarCategoryVo>> list(TourCarCategoryToWebDto filter) {
        if (filter != null) {
            PageMethod.startPage(filter.getPageNum(), filter.getPageSize(), filter.getCount());
        }
        TourCarCategory tourCarCategoryFilter = MyModelUtil.copyTo(filter, TourCarCategory.class);
        // 如果没有传入查询条件，默认按照热力值倒序排列
        if(ObjectUtils.isEmpty(filter.getOrderParam())) {
            MyOrderParam myOrderParam = new MyOrderParam();
            myOrderParam.add(new MyOrderParam.OrderInfo("hotValue", false, null));
            filter.setOrderParam(myOrderParam);
        }
        String orderBy = MyOrderParam.buildOrderBy(filter.getOrderParam(), TourCarCategory.class);

        // 由于三方车辆和司机是一对一关系，为了交互更加友好，如果是三方车辆列表查询，需要关联查询司机信息，如果司机被占用，那么三方车辆也不展示
        TourCarDriver tourCarDriverFilter = null;
        if(filter.getType() != null && filter.getType().equals(2)) {
            tourCarDriverFilter = new TourCarDriver();
            tourCarDriverFilter.setType(filter.getType());
            tourCarDriverFilter.setStartDate(filter.getStartDate());
            // 处理出发，返程时间需要处理成时间段
            if (StringUtils.isNotBlank(filter.getStartDate()) && filter.getDays() != null) {
                tourCarDriverFilter.setEndDate(QueryConditionProccessUtils.getEndDate(filter.getStartDate(), filter.getDays()));
            }
        }

        List<TourCarCategory> tourCarCategoryList =
                tourCarCategoryService.getTourCarCategoryListWithRelation(tourCarCategoryFilter, tourCarDriverFilter, orderBy);
        // 图片URL转换
        tourCarCategoryList.forEach(e->{
            e.setImage(UrlConvertUtils.urlConvertSingle(config.getHostIpPort(), e.getImage()));
        });
        return ResponseResult.success(MyPageUtil.makeResponseData(tourCarCategoryList, TourCarCategoryVo.class));
    }


    /**
     * 查看指定包车车型对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaIgnore
    @GetMapping("/view")
    public ResponseResult<TourCarCategoryVo> view(@RequestParam Long id) {
        TourCarCategory tourCarCategory = tourCarCategoryService.getByIdWithRelation(id, MyRelationParam.full());
        tourCarCategory.setImage(UrlConvertUtils.urlConvertSingle(config.getHostIpPort(), tourCarCategory.getImage()));
        if (tourCarCategory == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        TourCarCategoryVo tourCarCategoryVo = MyModelUtil.copyTo(tourCarCategory, TourCarCategoryVo.class);
        return ResponseResult.success(tourCarCategoryVo);
    }
}
