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 javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
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(EntityConstants.SYS_STAGING_DATABASE);
               //部门名称
               sj.setSsbm(item.getDepartment());
               //状态 开会讨论状态迁移都为1未入库
               sj.setZjzt(1);
               sj.setJyzt(1);


               sj.setPh(item.getBatchNumber());
//               有效周期：
//               长期有效、
//               待定、
//               有效期、
//               失效期、
//               复测日期
               //
               setYxzq(sj,item);
//               sj.setYxzq();
//               sj.setYxzqdw();
//               sj.setSxr();
               sj.setCcwz(item.getStorageLocation());
               sj.setCctj(item.getStorageConditions());
               sj.setPzrq(TimeUtil.getDate(item.getConfigurationDate()));

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

    private void setYxzq(Sj sj, ReagentStock item) {
        if (item.getValidityPeriod()==null){
            return;
        }
        Date rq = TimeUtil.getDate(item.getValidityDate());
        switch (item.getValidityPeriod()) {
            case "失效期":
                sj.setSxr(rq);
                break;
            case "有效期":
//                sj.setYxzq();
            case "复测日期":

                break;
            case "长期有效":
            case "待定":
            default:
                // 保持 sxr = null
                break;
        }


    }
}
