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

import cn.dev33.satoken.annotation.SaIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.page.PageMethod;
import com.tourism.common.additional.utils.MapConvertUtils;
import com.tourism.common.additional.utils.StringUtils;
import com.tourism.common.additional.utils.UrlConvertUtils;
import com.tourism.common.core.constant.ErrorCodeEnum;
import com.tourism.common.core.object.*;
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.back.model.DirectoryInfo;
import com.tourism.webadmin.back.model.JobProject;
import com.tourism.webadmin.back.model.TourismFile;
import com.tourism.webadmin.back.service.JobProjectService;
import com.tourism.webadmin.back.service.TourismProjectService;
import com.tourism.webadmin.back.vo.JobProjectVo;
import com.tourism.common.additional.config.ApplicationConfig;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

/**
 * 网页劳务项目管理操作控制器类。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Tag(name = "门户劳务项目管理接口")
@Slf4j
@RestController
@SaIgnore
@RequestMapping("/website/basic/jobProject")
public class JobProjectToWebController {

    @Autowired
    private JobProjectService jobProjectService;
    @Autowired
    private TourismProjectService tourismProjectService;
    @Autowired
    private ApplicationConfig applicationConfig;
    /**
     * 列出符合过滤条件的劳务项目管理列表。
     *
     * @param tourismProjectDtoFilter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @GetMapping("/list")
    public ResponseResult<MyPageData<JobProjectVo>> list(
            @Valid @ModelAttribute TourismProjectToWebDto tourismProjectDtoFilter) {
        if(tourismProjectDtoFilter.getBelongTab() == null){
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST,"所属分类(belongTab)不能为空!");
        }
        //如果belongTab小于1000并且大于10.则表明查询的是一级菜单
        if(tourismProjectDtoFilter.getBelongTab()>=10 && tourismProjectDtoFilter.getBelongTab()<1000){
            DirectoryInfo filter = new DirectoryInfo();
            filter.setParentId(tourismProjectDtoFilter.getBelongTab());
            filter.setEnable(1);
            List<DirectoryInfo> directoryInfoList = tourismProjectService.getDirectoryInfoList(filter, null);
            tourismProjectDtoFilter.setBelongTab(null);
            tourismProjectDtoFilter.setDirectoryInfoIds(directoryInfoList.stream().map(DirectoryInfo::getId).collect(toList()));
        }
        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();
            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 ->{
            List<String> jobUrl = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), item.getJobUrl());
            item.setJobUrlsAfterConvert(jobUrl);
        });

        return ResponseResult.success(jobProjectVoMyPageData);
    }

    /**
     * 查看指定劳务项目管理对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
//    @SaCheckPermission("jobProject.view")
    @GetMapping("/detail")
    public ResponseResult<JobProjectVo> detail(@RequestParam Long id) {
        JobProject jobProject = jobProjectService.getByIdWithRelation(id, MyRelationParam.full());
        if (jobProject == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        JobProjectVo jobProjectVo = MyModelUtil.copyTo(jobProject, JobProjectVo.class);
        List<String> jobUrl = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), jobProjectVo.getJobUrl());
        jobProjectVo.setJobUrlsAfterConvert(jobUrl);
        if(jobProjectVo.getJobFile() != null) {
            // 使用 Jackson 的 ObjectMapper 进行转换
            ObjectMapper objectMapper = new ObjectMapper();
            TourismFile tourismFile = objectMapper.convertValue(jobProjectVo.getJobFile(), TourismFile.class);
            tourismFile.setFileUrlsAfterConvert(UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), tourismFile.getFileUrl()));
            jobProjectVo.setJobFile(MapConvertUtils.convertObjectToMap(tourismFile));
        }
        if(jobProjectVo != null) {
            jobProjectVo.setContactCodeConvert("https://v.xiaoyaotravel.com/image/ContactQRCode/labor_service.png");
        }
        //二维码路径赋值
//        jobProjectVo.setContactCode("[{\"name\":\"微信图片_20241021161423.jpg\",\"downloadUri\":\"/admin/app/jobProject/download\",\"filename\":\"4bfb830a6091456d823db0b441c4edd1.jpg\",\"uploadPath\":\"image/JobProject/contactCode\"}]");
//        List<String> urlConvertList = UrlConvertUtils.urlConvert(applicationConfig.getHostIpPort(), jobProjectVo.getContactCode());
//        if(CollectionUtils.isNotEmpty(urlConvertList)) {
//            jobProjectVo.setContactCodeConvert(urlConvertList.get(0));
//        }
        return ResponseResult.success(jobProjectVo);
    }
}
