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.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.TourCarDriverToWebDto;
import com.tourism.webadmin.back.model.TourCarDriver;
import com.tourism.webadmin.back.service.TourCarDriverService;
import com.tourism.webadmin.back.util.QueryConditionProccessUtils;
import com.tourism.webadmin.back.vo.TourCarDriverVo;
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/tourCarDriver")
public class WebsiteCarDriverController {
    @Autowired
    private TourCarDriverService tourCarDriverService;
    @Autowired
    private ApplicationConfig config;

    /**
     * 列出符合过滤条件的包车司机列表。
     *
     * @param filter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaIgnore
    @GetMapping("/list")
    public ResponseResult<MyPageData<TourCarDriverVo>> list(TourCarDriverToWebDto filter) {
        if (filter != null) {
            PageMethod.startPage(filter.getPageNum(), filter.getPageSize(), filter.getCount());
        }

        // 处理年龄查询条件
        if(StringUtils.isNotBlank(filter.getAge())) {
            String[] ages = filter.getAge().split(",");
            filter.setAgeBegin(ages[0]);
            if(ages.length > 1) {
                filter.setAgeEnd(ages[1]);
            }
        }
        TourCarDriver tourCarDriverFilter = MyModelUtil.copyTo(filter, TourCarDriver.class);
        // 处理出发，返程时间需要处理成时间段
        if(StringUtils.isNotBlank(tourCarDriverFilter.getStartDate()) && filter.getDays() != null) {
            tourCarDriverFilter.setEndDate(QueryConditionProccessUtils.getEndDate(tourCarDriverFilter.getStartDate(), filter.getDays()));
        }

        // 如果没有传入查询条件，默认按照热力值倒序排列
        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(), TourCarDriver.class);
        List<TourCarDriver> tourCarDriverList =
                tourCarDriverService.getTourCarDriverListWithRelation(tourCarDriverFilter, orderBy);
        // 图片URL转换
        tourCarDriverList.forEach(e->{
            e.setImage(UrlConvertUtils.urlConvertSingle(config.getHostIpPort(), e.getImage()));
        });
        return ResponseResult.success(MyPageUtil.makeResponseData(tourCarDriverList, TourCarDriverVo.class));
    }


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