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.recovery.RcvBasic;
import com.docus.server.infrastructure.dao.IRcvBasicDao;
import com.docus.server.infrastructure.mapper.RcvBasicMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 患者信息表 数据访问实现类
 *
 * @author AutoGenerator
 * @since 2023-09-13
 */
@Repository
public class RcvBasicDaoImpl extends BaseDaoImpl<RcvBasicMapper, RcvBasic> implements IRcvBasicDao {


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

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

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

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<RcvBasic> search(SearchDTO searchDTO) {
        //分页
        PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        LambdaQueryWrapper<RcvBasic> query = Wrappers.lambdaQuery();

        //回收日期
        //出院日期
        Object revoveryBeginTime = searchDTO.getParams("revoveryBeginTime");
        Object revoveryEndTime = searchDTO.getParams("revoveryEndTime");

        if (Func.isNotEmpty(revoveryBeginTime)) {
            query.ge(RcvBasic::getRevoveryTime, revoveryBeginTime);
        }
        if (Func.isNotEmpty(revoveryEndTime)) {
            query.le(RcvBasic::getRevoveryTime, revoveryEndTime);
        }

        //出院日期
        Object disDateBeginTime = searchDTO.getParams("disDateBeginTime");
        Object disDateEndTime = searchDTO.getParams("disDateEndTime");

        if (Func.isNotEmpty(disDateBeginTime)) {
            query.ge(RcvBasic::getDisDate, disDateBeginTime);
        }
        if (Func.isNotEmpty(disDateEndTime)) {
            query.le(RcvBasic::getDisDate, disDateEndTime);
        }

        //流水号
        Object serialNumber = searchDTO.getParams("serialNumber");
        if (Func.isNotEmpty(serialNumber)) {
            query.eq(RcvBasic::getSerialNumber, serialNumber);
        }

        //住院号
        Object admissId = searchDTO.getParams("admissId");
        if (Func.isNotEmpty(admissId)) {
            query.eq(RcvBasic::getAdmissId, admissId);
        }

        //患者姓名
        Object name = searchDTO.getParams("name");
        if (Func.isNotEmpty(name)) {
            query.like(RcvBasic::getName, String.format("%s%s%s", "%", name, "%"));
        }

        //出院科室
        Object disDept = searchDTO.getParams("disDept");
        if (Func.isNotEmpty(disDept)) {
            query.eq(RcvBasic::getDisDept, disDept);
        }

        //回收人
        Object revoveryUser = searchDTO.getParams("revoveryUser");
        if (Func.isNotEmpty(revoveryUser)) {
            query.eq(RcvBasic::getRevoveryUser, revoveryUser);
        }

        //回收状态 0：待回收 1：已回收
        Object recoveryState = searchDTO.getParams("recoveryState");
        if (Func.isNotEmpty(recoveryState)) {
            query.eq(RcvBasic::getRecoveryState, recoveryState);
        }

        //默认createTime倒序排序
        query.orderByDesc(RcvBasic::getCreateTime);
        List<RcvBasic> list = super.find(query);
        return new PageResult<>(list);
    }

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

        return baseMapper.selectCount(query);
    }
}
