package com.tourism.webadmin.app.website.service.impl;

import com.github.pagehelper.page.PageMethod;
import com.tourism.common.core.object.MyOrderParam;
import com.tourism.common.core.object.MyPageData;
import com.tourism.common.core.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.webadmin.app.website.dto.PageDto;
import com.tourism.webadmin.app.website.service.JobRecordService;
import com.tourism.webadmin.back.model.JobProject;
import com.tourism.webadmin.back.model.TourBookInfo;
import com.tourism.webadmin.back.service.JobProjectService;
import com.tourism.webadmin.back.service.TourBookInfoService;
import com.tourism.webadmin.back.vo.JobProjectVo;
import com.tourism.webadmin.back.vo.TourBookInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service("JobRecordService")
public class JobRecordServiceImpl implements JobRecordService {
    @Autowired
    private TourBookInfoService tourBookInfoService;
    @Autowired
    private JobProjectService jobProjectService;

    public MyPageData<TourBookInfoVo> jobRecordList(String mobile, PageDto pageDto){

        TourBookInfo tourBookInfo = new TourBookInfo();
        tourBookInfo.setBookMobile(mobile);
        tourBookInfo.setType(2);

        if(pageDto.getPageSize() != null && pageDto.getPageNum() != null){
            PageMethod.startPage(pageDto.getPageNum(), pageDto.getPageSize(), true);
        }
        MyOrderParam myOrderParam = new MyOrderParam();
        myOrderParam.add(new MyOrderParam.OrderInfo("createTime",false,null));
        String orderBy = MyOrderParam.buildOrderBy(myOrderParam, TourBookInfo.class);
        List<TourBookInfo> tourBookInfoList = tourBookInfoService.getTourBookInfoList(tourBookInfo, orderBy);
        MyPageData<TourBookInfoVo> tourBookInfoVoMyPageData = MyPageUtil.makeResponseData(tourBookInfoList, TourBookInfoVo.class);
        if(tourBookInfoVoMyPageData != null && !CollectionUtils.isEmpty(tourBookInfoVoMyPageData.getDataList())){
            List<TourBookInfoVo> dataList = tourBookInfoVoMyPageData.getDataList();
            dataList.stream().forEach(item->{
                if(item.getProjectId() != null){
                    JobProject jobProject = jobProjectService.getById(item.getProjectId());
                    JobProjectVo jobProjectVo = MyModelUtil.copyTo(jobProject, JobProjectVo.class);
                    item.setJobProjectVo(jobProjectVo);
                }
            });
        }
        return tourBookInfoVoMyPageData;
    }
}
