package com.hxhq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxhq.common.EntityConstants;
import com.hxhq.domain.ReagentStock;
import com.hxhq.domain.ReagentStockPush;
import com.hxhq.domain.Sj;
import com.hxhq.domain.SyncLog;
import com.hxhq.dto.ReagentStockPushDTO;
import com.hxhq.mapper.ReagentStockMapper;
import com.hxhq.service.ReagentStockService;
import com.hxhq.service.SjService;
import com.hxhq.util.BeanConvert;
import com.hxhq.util.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangjing
 * @date 2026/01/28 15:50
 * @description
 */
@Service
public class ReagentStockServiceImpl extends ServiceImpl<ReagentStockMapper, ReagentStock> implements ReagentStockService {

    @Resource
    SjService sjService;

    @Resource
    SyncLogServiceImpl syncLogService;

    @Resource
    ReagentStockPushServiceImpl reagentStockPushService;
    @Transactional
    @Override
    public void dataPush(ReagentStockPushDTO dto) {
        ReagentStockPush reagentStock = BeanConvert.convert(dto, ReagentStockPush.class);
        reagentStockPushService.save(reagentStock);
    }



    @Override
    public void dataSync() {
        List<ReagentStock> list = baseMapper.notSync();
        if (list == null || list.isEmpty()) {
            return ;
        }
        //试剂
        List<Sj> sjList = changeToSj(list);
        sjService.saveList(sjList);
        //日志
        List<SyncLog> syncLogList = getLogList(list);
        saveLogList(syncLogList);
    }
    @Transactional
    public void saveLogList(List<SyncLog> syncLogList) {
        syncLogService.saveBatch(syncLogList);
    }


    private List<SyncLog> getLogList(List<ReagentStock> list) {
        List<SyncLog> syncLogList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        list.forEach(item ->{
            SyncLog syncLog = new SyncLog();
            syncLog.setSourceRecordId(item.getReagentStockId());
            syncLog.setTargetSystem(EntityConstants.SYS_HXHQ);
            syncLog.setSourceType(EntityConstants.LOG_REAGENT);
            syncLog.setCreatedAt(now);
            syncLog.setSyncStatus(EntityConstants.SUCCESS);
            syncLogList.add(syncLog);
        });

        return syncLogList;
    }

    private List<Sj> changeToSj(List<ReagentStock> list) {
           List<Sj> sjList = new ArrayList<>();
           list.forEach(item ->{
               Sj sj = new Sj();
               /** 制剂状态 1:入库 5:已锁定 7:待归档 9:归档 11:待解档 */
               /** 借阅状态 1:未借阅 3:待借阅 5:借阅中 */

               sj.setMc(item.getName());
               sj.setBh(item.getSn());
               sj.setNd(item.getPurity()!=null?item.getPurity().toString():null);
               sj.setNddw(item.getPurityUnit());
               sj.setKc(item.getStockCount().toString());
               sj.setKcdw(item.getStockUnit());
               sj.setGg(item.getSpecs());
               //无来源默认为：中间库
               sj.setLy(item.getSource()!=null?item.getSource():EntityConstants.SYS_STAGING_DATABASE);
               //部门名称
               sj.setSsbm(item.getDepartment());
               //状态 开会讨论状态迁移都为1未入库
               sj.setZjzt(1);
               sj.setJyzt(1);


               sj.setPh(item.getBatchNumber());
               sj.setYxzq(item.getValidityPeriod());
               sj.setYxzqdw(item.getValidityPeriodUnit());
               sj.setSxr(TimeUtil.getDate(item.getExpiryDate()));
               sj.setCcwz(item.getStorageLocation());
               sj.setCctj(item.getStorageConditions());
               sj.setPzrq(TimeUtil.getDate(item.getConfigurationDate()));

               sjList.add(sj);
           });
           return sjList;
    }
}
