package com.tourism.webadmin.app.wechat.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.StringUtils;
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.ResponseResult;
import com.tourism.common.core.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.webadmin.app.website.dto.TourismProjectToWebDto;
import com.tourism.webadmin.app.wechat.dto.WechatIndexJobProjectDto;
import com.tourism.webadmin.back.model.DirectoryInfo;
import com.tourism.webadmin.back.model.JobProject;
import com.tourism.webadmin.back.service.JobProjectService;
import com.tourism.webadmin.back.service.TourismProjectService;
import com.tourism.webadmin.back.vo.JobProjectVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * WechatJobIndexController接口。
 *
 * @author huangwen
 * @date 2024/11/1
 */
@Tag(name = "全球劳务首页接口")
@Slf4j
@RestController
@SaIgnore
@DisableDataFilter
@RequestMapping("/wechat/job")
public class WechatJobIndexController {
    @Autowired
    private JobProjectService jobProjectService;
    @Autowired
    private TourismProjectService tourismProjectService;
    @Autowired
    private ApplicationConfig applicationConfig;
    /**
     * 列出符合过滤条件的劳务项目管理列表。
     *
     * @param tourismProjectDtoFilter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/list")
    public ResponseResult<MyPageData<JobProjectVo>> list(
             @ModelAttribute WechatIndexJobProjectDto tourismProjectDtoFilter) {

        JobProject jobProjectFilter = MyModelUtil.copyTo(tourismProjectDtoFilter, JobProject.class);
        //首页展示的为启用的内容
        jobProjectFilter.setEnable(1);
        if(tourismProjectDtoFilter.getPageSize() != null && tourismProjectDtoFilter.getPageNum() != null){
            PageMethod.startPage(tourismProjectDtoFilter.getPageNum(), tourismProjectDtoFilter.getPageSize(), true);
        }

        MyOrderParam myOrderParam = new MyOrderParam();
        if (tourismProjectDtoFilter.getIsNew()!=null && tourismProjectDtoFilter.getIsNew()==1){
            myOrderParam.add(new MyOrderParam.OrderInfo("createTime",false,null));
        }else {
            myOrderParam.add(new MyOrderParam.OrderInfo("isHotspot",false,null));
            myOrderParam.add(new MyOrderParam.OrderInfo("showOrder",false,null));
        }
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, JobProject.class);

        List<JobProject> jobProjectList = jobProjectService.getJobProjectListWithRelation(jobProjectFilter, orderBy);

        MyPageData<JobProjectVo> jobProjectVoMyPageData = MyPageUtil.makeResponseData(jobProjectList, JobProjectVo.class);
        List<JobProjectVo> dataList = jobProjectVoMyPageData.getDataList();
        //先把imgUrl由jaon转换为List<FileUrlObject>
        dataList.stream().forEach(item ->
        {
            if (StringUtils.isNotEmpty(item.getJobUrl())) {
                List<String> jobUrl = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getJobUrl());
                item.setJobUrlsAfterConvert(jobUrl);
            }
        });

        return ResponseResult.success(jobProjectVoMyPageData);
    }
}
