package com.fs.hisStore.service.impl;

import com.fs.hisStore.domain.SysOperLogScrm;
import com.fs.hisStore.mapper.SysOperLogScrmMapper;
import com.fs.hisStore.service.IFsHisStoreAuditLogScrmService;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FsHisStoreAuditLogScrmServiceImpl implements IFsHisStoreAuditLogScrmService {

    @Autowired
    private SysOperLogScrmMapper sysOperLogScrmMapper;
    @Override
    public void addBatchAudit(List<Long> ids, String reason, String attachImage) {
        List<SysOperLogScrm> sysOperLogScrm = ids.stream().map(id -> {
            SysOperLogScrm LogScrm = new SysOperLogScrm();
            LogScrm.setMainId(id);
            LogScrm.setReason(reason);
            LogScrm.setAttachImage(attachImage);
            return LogScrm;
        }).collect(Collectors.toList());
        sysOperLogScrmMapper.insertBatchByIds(sysOperLogScrm);
        //将返回的operId拼接成sql的in字句放入MDC中，方便后续查询
        MDC.put("operIds",
                sysOperLogScrm.stream()
                        .map(SysOperLogScrm::getOperId)
                        .map(String::valueOf)
                        .collect(Collectors.joining(",")));
    }

    @Override
    public void addAudit(Long id, String reason, String attachImage) {
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        addBatchAudit(ids, reason, attachImage);
    }

    @Override
    public void generateOperId() {
        addAudit(null, null, null);
    }

    //selectOperLogByMainId
    @Override
    public List<SysOperLogScrm> selectOperLogByMainId(Long id) {
        return sysOperLogScrmMapper.selectSotreOperLogByMainId(id);
    }


}
