package com.handong.smartservice.service;

import com.handong.smartservice._AG;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.DbLogger;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.mapper.ErrorHistoryMapper;
import com.handong.smartservice.mapper.VocMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ErrorHistoryService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ErrorHistoryMapper errorHistoryMapper;


    public ErrorHistoryService(ErrorHistoryMapper errorHistoryMapper) {
        this.errorHistoryMapper = errorHistoryMapper;
    }

    public Map<String, Object> getErrorHistoryList(Boolean is_excel, Long offset, Long limit, String date_start, String date_end,
                String uid1, Long uid1_type, String slot) {
        Map<String, Object> mapResult = new HashMap<>();

        List<Map<String, Object>> listMap = (List<Map<String, Object>>)errorHistoryMapper.selectErrorHistory(false, is_excel, offset, limit, 
            date_start, date_end, uid1, uid1_type, slot);
        mapResult.put("list", listMap);
        
        Long totalCount = _AG.getNumberFromQuery("COUNT(*)", errorHistoryMapper.selectErrorHistory(true, is_excel, offset, limit, 
            date_start, date_end, uid1, uid1_type, slot));
        mapResult.put("totalCount", totalCount);
        mapResult.put("offset", offset);
        return mapResult;
    }

    public Map<String, Object> getMiss(Long state) {
        Map<String, Object> mapResult = new HashMap<>();

        List<Map<String, Object>> listMap = errorHistoryMapper.selectErrorHistoryByState(1L);
        mapResult.put("list", listMap);
        
        return mapResult;
    }

    public CtResponse missSlot(String uid1, Long uid1_type, String slot) {
        CtResponse result = new CtResponse();

        errorHistoryMapper.insertErrorHistory(uid1, uid1_type, slot);
        DbLogger.insert(1L, "물품 미투출 TID: " + uid1 + ", 컬럼번호: " + slot, 2L);
        
        return result;
    }
}
