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.PrescriptionOrderMapper;
import com.gxwebsoft.clinic.service.PrescriptionOrderItemService;
import com.gxwebsoft.clinic.service.PrescriptionOrderService;
import com.gxwebsoft.clinic.entity.PrescriptionOrder;
import com.gxwebsoft.clinic.param.PrescriptionOrderParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 处方主表
Service实现
 *
 * @author LX
 * @since 2025-12-10 11:28:03
 */
@Service
public class PrescriptionOrderServiceImpl extends ServiceImpl<PrescriptionOrderMapper, PrescriptionOrder> implements PrescriptionOrderService {
    @Resource
    private PrescriptionOrderItemService prescriptionOrderItemService;
    @Override
    public PageResult<PrescriptionOrder> pageRel(PrescriptionOrderParam param) {
        PageParam<PrescriptionOrder, PrescriptionOrderParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<PrescriptionOrder> list = baseMapper.selectPageRel(page, param);
        for (PrescriptionOrder prescriptionOrder : list) {
            prescriptionOrder.setItems(prescriptionOrderItemService.getByOrderId(prescriptionOrder.getId()));
        }
        return new PageResult<>(list, page.getTotal());
    }

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

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

    @Override
    public PrescriptionOrder getByOrderNo(String orderNo) {
        return getOne(
                new LambdaQueryWrapper<PrescriptionOrder>()
                        .eq(PrescriptionOrder::getOrderNo, orderNo)
                        .last("limit 1")
        );
    }

}
