package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.CollectionService;
import org.jeecg.modules.workorderBanner.service.IWorkorderBannerService;
import org.jeecg.modules.workorderCollection.entity.WorkorderCollection;
import org.jeecg.modules.workorderCollection.service.IWorkorderCollectionService;
import org.jeecg.modules.workorderNews.entity.WorkorderNews;
import org.jeecg.modules.workorderStatus.entity.WorkorderStatus;
import org.jeecg.modules.workorderStatus.service.IWorkorderStatusService;
import org.jeecg.modules.workorderTemplate.entity.WorkorderTemplate;
import org.jeecg.modules.workorderTemplate.service.IWorkorderTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    //收藏表信息
    @Resource
    private IWorkorderCollectionService workorderCollectionService;

    //工单表信息
    @Resource
    private IWorkorderTemplateService workorderTemplateService;

    //工单状态表信息
    @Resource
    private IWorkorderStatusService workorderStatusService;
    /*************************************************************************************/

    //收藏信息-我的收藏
    @Override
    public Result<?> queryCollectionList(String userId, PageBean pageBean) {

        //分页
        Page<WorkorderCollection> page = new Page<WorkorderCollection>(pageBean.getPageNo(), pageBean.getPageSize());
        LambdaQueryChainWrapper<WorkorderCollection> query = workorderCollectionService
                .lambdaQuery();

        //组装查询条件
        if(null != userId){
            //工单状态
            query.eq(WorkorderCollection::getUserId, userId);
        }

        //获取收藏表信息
        Page<WorkorderCollection> pageList = query.page(page);

        //工单状态列表
        List<WorkorderStatus> workorderStatus = workorderStatusService
                .lambdaQuery()
                .list();

        //将收藏工单信息放入收藏表中
        for (WorkorderCollection record : pageList.getRecords()) {
            WorkorderTemplate template = workorderTemplateService.getById(record.getTemplateId());
            //添加工单状态说明
            for (WorkorderStatus status : workorderStatus) {
                if(template.getStatusId().equals(status.getId())){
                    template.setStatusDescribe(status.getName());
                    break;
                }
            }
            record.setWorkorderTemplate(template);
        }

        return Result.OK("我的收藏列表", pageList);
    }
}
