package com.tuoheng.service.information.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.common.CommonConfig;
import com.tuoheng.enums.MarkTypeEnum;
import com.tuoheng.model.entity.Inspection;
import com.tuoheng.model.entity.InspectionFile;
import com.tuoheng.model.query.InspectionFileQuery;
import com.tuoheng.service.information.InspectionFileService;
import com.tuoheng.mapper.information.InspectionFileMapper;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 小影
 * @description 针对表【t_inspection_file】的数据库操作Service实现
 * @createDate 2023-04-24 09:12:58
 */
@Service
public class InspectionFileServiceImpl extends ServiceImpl<InspectionFileMapper, InspectionFile>
        implements InspectionFileService {

    @Autowired
    private InspectionFileMapper inspectionFileMapper;


    /**
     * 查询问题列表
     *
     * @return
     */
    @Override
    public JsonResult index(InspectionFileQuery query) {
        //分页校验
        query.checkParam();
        //分页
        IPage<InspectionFile> page = new Page<>(query.getPage(), query.getLimit());

        IPage<InspectionFile> pageDate = inspectionFileMapper.selectPage(page, Wrappers.<InspectionFile>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(query.getQuestionName()), InspectionFile::getQuestionName, query.getQuestionName())
                .eq(ObjectUtil.isNotNull(query.getStatus()), InspectionFile::getStatus, query.getStatus())
                .eq(ObjectUtil.isNotEmpty(query.getClientId()), InspectionFile::getClientId, query.getClientId())
                .eq(InspectionFile::getMark, MarkTypeEnum.VALID.getCode())
                .orderByDesc(InspectionFile::getCreateTime));
        //问题图片带域名
        pageDate.convert(item -> {
            InspectionFile inspectionFile = Convert.convert(InspectionFile.class, item);
            if (ObjectUtil.isNotEmpty(item.getFileOriginal())) {
                inspectionFile.setFileOriginal(CommonConfig.imageURL + item.getFileOriginal());
            }
            if (ObjectUtil.isNotEmpty(item.getFileImage())) {
                inspectionFile.setFileImage(CommonConfig.imageURL + item.getFileImage());
            }
            if (ObjectUtil.isNotEmpty(item.getFileThumbnail())) {
                inspectionFile.setFileThumbnail(CommonConfig.imageURL + item.getFileThumbnail());
            }
            return inspectionFile;
        });

        return JsonResult.success(pageDate);
    }
}




