package com.fs.hospital580.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.hospital580.dto.Medicine;
import com.fs.hospital580.dto.MedicineDto;
import com.fs.hospital580.dto.Result580;
import com.fs.hospital580.entity.Hospital580ProductPushScrmEntity;
import com.fs.hospital580.entity.Hospital580ScrmEntity;
import com.fs.hospital580.mapper.Hospital580ScrmMapper;
import com.fs.hospital580.mapper.MedicineMapper;
import com.fs.hospital580.service.Hospital580ProductPushScrmService;
import com.fs.hospital580.service.Hospital580ScrmService;
import com.fs.hospital580.service.Hospital580Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 请求580医院记录表 服务实现类
 * </p>
 *
 * @author zhangqin
 * @since 2025-09-25
 */
@Service
public class Hospital580ScrmServiceImpl extends ServiceImpl<Hospital580ScrmMapper, Hospital580ScrmEntity> implements Hospital580ScrmService {

    @Autowired
    private Hospital580Service hospital580Service;
    @Autowired
    private Hospital580ProductPushScrmService pushScrmService;
    @Autowired
    private IFsStoreProductScrmService fsStoreProductScrmService;

/*    @Override
    public void SynDrugToHospital580(FsStoreProductScrm productScrm) {
        MedicineDto medicineDto = new MedicineDto();
        // 使用MapStruct自动映射
        Medicine medicine = MedicineMapper.INSTANCE.toMedicine(productScrm);
        medicineDto.setMedicineList(Arrays.asList(medicine));
        syncMedicine(medicineDto);
    }*/

    @Override
    public void SynDrugToHospital580(Long[] productIds) {
        List<FsStoreProductScrm> fsStoreProductScrms = fsStoreProductScrmService.selectProductByIds(productIds);
        MedicineDto medicineDto = new MedicineDto();
        medicineDto.setMedicineList(convertToMedicines(fsStoreProductScrms));
        syncMedicine(medicineDto);
    }

    private void syncMedicine(MedicineDto medicineDto) {
        Result580 result580 = hospital580Service.syncMedicine(medicineDto);
        if (result580.getErr() == 0) {
            List<Hospital580ProductPushScrmEntity> collect = medicineDto.getMedicineList().stream().map(item -> {
                Hospital580ProductPushScrmEntity productPushScrmEntity = new Hospital580ProductPushScrmEntity();
                productPushScrmEntity.setProductId(Long.valueOf(item.getMedicineId()));
                productPushScrmEntity.setPushStatus((byte) 0);
                productPushScrmEntity.setCreateTime(LocalDateTime.now());
                productPushScrmEntity.setRequestId(result580.getRequestId());
                return productPushScrmEntity;
            }).collect(Collectors.toList());
            pushScrmService.saveBatch(collect);
        }else {
            throw new RuntimeException("同步药品失败"+result580.getErrmsg());
        }
    }

    /**
     * 批量转换FsStoreProductScrm列表到Medicine列表
     */
    public List<Medicine> convertToMedicines(List<FsStoreProductScrm> products) {
        return products.stream()
                .map(MedicineMapper.INSTANCE::toMedicine)
                .collect(Collectors.toList());
    }
}