package org.dromara.web.service.impl;
import java.sql.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.web.domain.TreatmentUser;
import org.dromara.web.domain.bo.TotalPatientBo;
import org.dromara.web.domain.vo.TotalPatientVo;
import org.dromara.web.mapper.TreatmentUserMapper;
import org.dromara.web.service.TotalPatientService;
import org.springframework.stereotype.Service;

/**
 * @Author: Huanyi
 * @CreateTime: 2025-08-14
 * @Description:
 * @Version: 1.0
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class TotalPatientServiceImpl implements TotalPatientService {

    private final TreatmentUserMapper treatmentUserMapper;

    @Override
    public TableDataInfo<TotalPatientVo> listPage(TotalPatientBo bo, PageQuery pageQuery) {

        LambdaQueryWrapper<TreatmentUser> wrapper = buildQueryWrapper(bo);
        IPage<TreatmentUser> page = treatmentUserMapper.selectPage(pageQuery.build(), wrapper);
        return TableDataInfo.build(page.convert(this::convertToVo));
    }

    private TotalPatientVo convertToVo(TreatmentUser user) {
        TotalPatientVo vo = new TotalPatientVo();
        vo.setId(user.getId());
        vo.setUpdateTime(user.getUpdateTime());
        vo.setName(user.getTreatName());
        vo.setType(user.getType());
        vo.setSex(user.getSex());
        vo.setAge(user.getAge());
        vo.setIdCard(user.getIdCard());
        vo.setMedicalId(user.getTreatNum());
//        vo.setAppointmentTime();
        vo.setPhoneNumber(user.getPhoneNum());
//        vo.setPrincipleAction("");
//        vo.setPresentIllnessHistory("");
//        vo.setPreviousHistory("");
//        vo.setDiagnose("");
        return vo;
    }

    private LambdaQueryWrapper<TreatmentUser> buildQueryWrapper(TotalPatientBo bo) {
        LambdaQueryWrapper<TreatmentUser> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(bo.getType() != null, TreatmentUser::getType, bo.getType())
            .eq(bo.getSex() != null, TreatmentUser::getSex, bo.getSex())
            .like(bo.getMedicalId() != null, TreatmentUser::getTreatNum, bo.getMedicalId())
            .ge(bo.getUpdateTimeEarliest() != null, TreatmentUser::getUpdateTime, bo.getUpdateTimeEarliest())
            .le(bo.getUpdateTimeLatest() != null, TreatmentUser::getUpdateTime, bo.getUpdateTimeLatest());
        return wrapper;
    }
}
