package com.gxwebsoft.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.clinic.mapper.ClinicDoctorUserMapper;
import com.gxwebsoft.clinic.service.ClinicDoctorUserService;
import com.gxwebsoft.clinic.entity.ClinicDoctorUser;
import com.gxwebsoft.clinic.param.ClinicDoctorUserParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分销商用户记录表Service实现
 *
 * @author 科技小王子
 * @since 2025-10-19 09:27:04
 */
@Service
public class ClinicDoctorUserServiceImpl extends ServiceImpl<ClinicDoctorUserMapper, ClinicDoctorUser> implements ClinicDoctorUserService {

    @Override
    public PageResult<ClinicDoctorUser> pageRel(ClinicDoctorUserParam param) {
        PageParam<ClinicDoctorUser, ClinicDoctorUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<ClinicDoctorUser> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ClinicDoctorUser> listRel(ClinicDoctorUserParam param) {
        List<ClinicDoctorUser> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ClinicDoctorUser, ClinicDoctorUserParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ClinicDoctorUser getByIdRel(Integer id) {
        ClinicDoctorUserParam param = new ClinicDoctorUserParam();
        param.setUserId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public ClinicDoctorUser getByUserId(Integer userId) {
        return getOne(
                new LambdaQueryWrapper<ClinicDoctorUser>()
                        .eq(ClinicDoctorUser::getUserId, userId)
                        .last("limit 1")
        );
    }

}
