package com.fs.hospital580.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.his.param.FsPrescribeListDParam;
import com.fs.hisStore.mapper.FsStoreOrderScrmMapper;
import com.fs.hospital580.entity.Hospital580PrescriptionScrmEntity;
import com.fs.hospital580.mapper.Hospital580PrescriptionScrmMapper;
import com.fs.hospital580.service.Hospital580PrescriptionScrmService;
import com.fs.hospital580.vo.PrescriptionAdminQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 问诊订单主表 服务实现类
 * </p>
 *
 * @author zhangqin
 * @since 2025-09-25
 */
@Service
public class Hospital580PrescriptionScrmServiceImpl extends ServiceImpl<Hospital580PrescriptionScrmMapper, Hospital580PrescriptionScrmEntity> implements Hospital580PrescriptionScrmService {
    @Autowired
    private FsStoreOrderScrmMapper fsStoreOrderScrmMapper;

    @Override
    public boolean isByStoreOrder(Long storeOrderId) {
        return this.count(new LambdaQueryWrapper<Hospital580PrescriptionScrmEntity>().eq(Hospital580PrescriptionScrmEntity::getStoreOrderId, storeOrderId)) > 0;
    }


    @Override
    @Transactional
    public void prescriptionInvalid() {
        //获取处方信息
        List<Hospital580PrescriptionScrmEntity> PrescriptionList = baseMapper.getExpiredPrescriptionInfo();
        if(!PrescriptionList.isEmpty()){
            //获取订单信息
           List<Long> orderIds=PrescriptionList.stream().map(Hospital580PrescriptionScrmEntity::getStoreOrderId).collect(Collectors.toList());
           //订单状态取消，开放是订单状态为取消即订单失效
            fsStoreOrderScrmMapper.batchUpdateOrderStatusById(orderIds,"-3");
        }
    }

    @Override
    public IPage<Hospital580PrescriptionScrmEntity> getPrescription(FsPrescribeListDParam param) {
        Page<Hospital580PrescriptionScrmEntity> page = new Page<>(param.getPageNum(), param.getPageSize());
        return baseMapper.getPrescription(page,param);
    }

    @Override
    public List<Hospital580PrescriptionScrmEntity> getPrescriptionList(PrescriptionAdminQueryVo param) {
        return baseMapper.getPrescriptionList(param);
    }
}
