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.PrescriptionOrderItemMapper;
import com.gxwebsoft.clinic.service.PrescriptionOrderItemService;
import com.gxwebsoft.clinic.entity.PrescriptionOrderItem;
import com.gxwebsoft.clinic.param.PrescriptionOrderItemParam;
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 LX
 * @since 2025-12-10 11:28:03
 */
@Service
public class PrescriptionOrderItemServiceImpl extends ServiceImpl<PrescriptionOrderItemMapper, PrescriptionOrderItem> implements PrescriptionOrderItemService {

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

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

    @Override
    public List<PrescriptionOrderItem> getByOrderId(Integer orderId) {
        return list(
                new LambdaQueryWrapper<PrescriptionOrderItem>()
                        .eq(PrescriptionOrderItem::getPrescriptionId, orderId)
        );
    }

    @Override
    public PrescriptionOrderItem getByIdRel(Integer id) {
        PrescriptionOrderItemParam param = new PrescriptionOrderItemParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
