package com.yingpaipay.business.service.impl;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yingpaipay.business.domain.bo.DocumentQcTaskLogBo;
import com.yingpaipay.business.domain.vo.DocumentQcTaskLogVo;
import com.yingpaipay.business.domain.DocumentQcTaskLog;
import com.yingpaipay.business.mapper.DocumentQcTaskLogMapper;
import com.yingpaipay.business.service.IDocumentQcTaskLogService;

import java.util.Map;

/**
 * 文档质控日志Service业务层处理
 *
 * @author Huanyi
 * @date 2026-01-14
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DocumentQcTaskLogServiceImpl implements IDocumentQcTaskLogService {

    private final DocumentQcTaskLogMapper baseMapper;

    /**
     * 分页查询文档质控日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 文档质控日志分页列表
     */
    @Override
    public TableDataInfo<DocumentQcTaskLogVo> queryPageList(DocumentQcTaskLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DocumentQcTaskLog> lqw = buildQueryWrapper(bo);
        Page<DocumentQcTaskLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    private LambdaQueryWrapper<DocumentQcTaskLog> buildQueryWrapper(DocumentQcTaskLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DocumentQcTaskLog> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(DocumentQcTaskLog::getId);
        lqw.eq(bo.getTaskId() != null, DocumentQcTaskLog::getTaskId, bo.getTaskId());
        lqw.eq(bo.getDetailId() != null, DocumentQcTaskLog::getDetailId, bo.getDetailId());
        lqw.eq(bo.getExecutor() != null, DocumentQcTaskLog::getExecutor, bo.getExecutor());
        lqw.between(params.get("beginExecuteTime") != null && params.get("endExecuteTime") != null,
            DocumentQcTaskLog::getExecuteTime ,params.get("beginExecuteTime"), params.get("endExecuteTime"));
        lqw.eq(bo.getResult() != null, DocumentQcTaskLog::getResult, bo.getResult());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionType()), DocumentQcTaskLog::getQuestionType, bo.getQuestionType());
        lqw.eq(bo.getDesignatedDealer() != null, DocumentQcTaskLog::getDesignatedDealer, bo.getDesignatedDealer());
        lqw.between(params.get("beginDeadline") != null && params.get("endDeadline") != null,
            DocumentQcTaskLog::getDeadline ,params.get("beginDeadline"), params.get("endDeadline"));
        lqw.eq(bo.getActualDealer() != null, DocumentQcTaskLog::getActualDealer, bo.getActualDealer());
        lqw.between(params.get("beginDealTime") != null && params.get("endDealTime") != null,
            DocumentQcTaskLog::getDealTime ,params.get("beginDealTime"), params.get("endDealTime"));
        lqw.eq(bo.getCreateBy() != null, DocumentQcTaskLog::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            DocumentQcTaskLog::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(bo.getUpdateBy() != null, DocumentQcTaskLog::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            DocumentQcTaskLog::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }

}
