package com.gxwebsoft.clinic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.clinic.entity.ClinicPrescription;
import com.gxwebsoft.clinic.entity.ClinicPrescriptionItem;
import com.gxwebsoft.clinic.mapper.ClinicPrescriptionMapper;
import com.gxwebsoft.clinic.param.ClinicPrescriptionItemParam;
import com.gxwebsoft.clinic.param.ClinicPrescriptionParam;
import com.gxwebsoft.clinic.service.ClinicPrescriptionItemService;
import com.gxwebsoft.clinic.service.ClinicPrescriptionService;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.entity.ShopOrderGoods;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 处方主表
Service实现
 *
 * @author 科技小王子
 * @since 2025-10-22 02:01:13
 */
@Service
public class ClinicPrescriptionServiceImpl extends ServiceImpl<ClinicPrescriptionMapper, ClinicPrescription> implements ClinicPrescriptionService {

    @Resource
    private ClinicPrescriptionItemService clinicPrescriptionItemService;
    @Override
    public PageResult<ClinicPrescription> pageRel(ClinicPrescriptionParam param) {
        PageParam<ClinicPrescription, ClinicPrescriptionParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<ClinicPrescription> list = baseMapper.selectPageRel(page, param);
        // 查询处方明细
        Set<Integer> collectIds = list.stream().map(ClinicPrescription::getId).collect(Collectors.toSet());
        final ClinicPrescriptionItemParam itemParam = new ClinicPrescriptionItemParam();
        itemParam.setPrescriptionIds(collectIds);
        final List<ClinicPrescriptionItem> clinicPrescriptionItems = clinicPrescriptionItemService.listRel(itemParam);
        final Map<Integer, List<ClinicPrescriptionItem>> collect = clinicPrescriptionItems.stream().collect(Collectors.groupingBy(ClinicPrescriptionItem::getPrescriptionId));
        list.forEach(d -> {
          d.setItems(collect.get(d.getId()));
        });
        return new PageResult<>(list, page.getTotal());
    }

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

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

    @Override
    public ClinicPrescription getByLastId(ClinicPrescription clinicPrescription) {
        return getOne(new LambdaQueryWrapper<ClinicPrescription>().orderByDesc(ClinicPrescription::getId).eq(ClinicPrescription::getUserId, clinicPrescription.getUserId()).last("limit 1"));
    }

}
