package com.hxhq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxhq.common.EntityConstants;
import com.hxhq.domain.Container;
import com.hxhq.domain.Gsp;
import com.hxhq.domain.SyncLog;
import com.hxhq.domain.Yq;
import com.hxhq.dto.ContainerDTO;
import com.hxhq.mapper.ContainerMapper;
import com.hxhq.service.ContainerService;
import com.hxhq.service.GspService;
import com.hxhq.service.YqService;
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 ContainerServiceImpl extends ServiceImpl<ContainerMapper, Container> implements ContainerService {

    @Resource
    GspService gspService;
    @Resource
    SyncLogServiceImpl syncLogService;
    @Override
    public void dataPush(ContainerDTO dto) {
        Container Container = BeanConvert.convert(dto, Container.class);
        save(Container);
    }

    @Override
    public void dataSync() {
        List<Container> list = baseMapper.notSync();
        if (list == null || list.isEmpty()) {
            return ;
        }
        //供试品
        List<Gsp> gspList = changeToGsp(list);
        gspService.saveList(gspList);
        //日志
        List<SyncLog> syncLogList = getLogList(list);
        saveLogList(syncLogList);
    }
    @Transactional
    public void saveLogList(List<SyncLog> syncLogList) {
        syncLogService.saveBatch(syncLogList);
    }


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

        return syncLogList;
    }
//
    private List<Gsp> changeToGsp(List<Container> list) {
        List<Gsp> gspList = new ArrayList<>();
        list.forEach(item ->{
            Gsp sj = new Gsp();
            sj.setMc(item.getSubstanceName());
            sj.setBh(item.getSubstanceCode());
            sj.setPh(item.getLotNumber());
            sj.setRemark(item.getComments());
            sj.setRemark(item.getComments());
            sj.setKc(item.getCurrentNetAmount().toString());
            sj.setKcdw(item.getCurrentNetAmountUnit());
            sj.setYxq(TimeUtil.getDate(item.getExpirationDate()));
            //状态 开会讨论状态迁移都为1未入库
            sj.setZjzt(1);
            sj.setJyzt(1);
            //容器用途
//            sj.set(item.getContainerUse());
            sj.setJsrq(TimeUtil.getDate(item.getDateTimePharmacyRecv()));
            //交易日期（默认当前时间）
//            sj.set(item.getTransDate());
            //存储位置
//            sj.setCz(item.getStorageLocation());
            //研究编号
//            sj.setCz(item.getStudyNumber());
            //录入时间
//            sj.setLr(TimeUtil.getDate(item.getExpirationDate()));

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