package com.docus.server.infrastructure.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.db.dao.impl.BaseDaoImpl;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.entity.segmentation.OcrBasicTest;
import com.docus.server.infrastructure.dao.IOcrBasicTestDao;
import com.docus.server.infrastructure.mapper.OcrBasicTestMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 患者信息表-调试 数据访问实现类
 *
 * @author AutoGenerator
 * @since 2023-08-29
 */
@Repository
public class OcrBasicTestDaoImpl extends BaseDaoImpl<OcrBasicTestMapper, OcrBasicTest> implements IOcrBasicTestDao {

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public OcrBasicTest findById(Long id) {
        return super.findById(id);
    }

    /**
     * 新增
     *
     * @param ocrBasicTest 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean add(OcrBasicTest ocrBasicTest) {
        return super.save(ocrBasicTest);
    }

    /**
     * 编辑
     *
     * @param ocrBasicTest 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean edit(OcrBasicTest ocrBasicTest) {
        return super.updateById(ocrBasicTest);
    }

    /**
     * 批量删除
     *
     * @param ids 主键ids
     * @return 成功或失败
     */
    @Override
    public int delete(List<Long> ids) {
        return super.deleteByIds(ids);
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<OcrBasicTest> search(SearchDTO searchDTO) {
        //分页
        PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        LambdaQueryWrapper<OcrBasicTest> query = Wrappers.lambdaQuery();
        //时间范围，默认按createTime
//        if (searchDTO.getBeginTime() != null) {
//            query.ge(OcrBasicTest::getCreateTime, searchDTO.getBeginTime());
//        }
//        if (searchDTO.getEndTime() != null) {
//            Date endTime = searchDTO.getEndTime();
//            query.le(OcrBasicTest::getCreateTime, endTime);
//        }
        Object patientId = searchDTO.getParams("patientId");
        Object inpatientNo = searchDTO.getParams("inpatientNo");
        Object name = searchDTO.getParams("name");
        Object ocrStatue = searchDTO.getParams("ocrStatue");

        if (Func.isNotEmpty(patientId)) {
            query.like(OcrBasicTest::getPatientId,String.format("%s%s%s","%", patientId,"%"));
        }
        if (Func.isNotEmpty(inpatientNo)) {
            query.like(OcrBasicTest::getInpatientNo, String.format("%s%s%s","%", inpatientNo,"%"));
        }
        if (Func.isNotEmpty(name)) {
            query.like(OcrBasicTest::getName, String.format("%s%s%s","%", name,"%"));
        }
        if (Func.isNotEmpty(ocrStatue)) {
            query.eq(OcrBasicTest::getOcrStatue, ocrStatue);
        }


        query.orderByDesc(OcrBasicTest::getCreateTime);
        List<OcrBasicTest> list = super.find(query);
        return new PageResult<>(list);
    }

    /**
     * 名称不重复
     *
     * @param id   主键
     * @param name 名称
     * @return 名称重复数量
     */
    @Override
    public int findByIdAndName(Long id, String name) {
        LambdaQueryWrapper<OcrBasicTest> query = Wrappers.lambdaQuery();
        if (id != null) {
            query.ne(OcrBasicTest::getId, id);
        }
        //if (Func.isNotBlank(name)) {
        //   query.eq(OcrBasicTest::getName, name);
        //}

        return baseMapper.selectCount(query);
    }
}
