package com.handong.smartservice.service;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.handong.smartservice._AG;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.handong.smartservice.mapper.TransactionMapper;
import com.handong.smartservice.mapper.BizGroupMapper;
import com.handong.smartservice.mapper.GoodsMapper;

@Service
public class SalesService {

    private final TransactionMapper transactionMapper;
    private final BizGroupMapper bizGroupMapper;

    public SalesService(TransactionMapper transactionMapper,  BizGroupMapper bizGroupMapper) {
        this.transactionMapper = transactionMapper;
        this.bizGroupMapper = bizGroupMapper;
    }

    public Map<String, Object> getSalesList(Boolean is_excel, Long offset, Long limit, Boolean is_group_access, Long biz_group_id, String date_start, String date_end, 
        String uid1, String device_name, Long approval_type, Long date_type) {
        Map<String, Object> mapResult = new HashMap<>();

        if (is_group_access == false) {
            List<Map<String, Object>> listForTopId = bizGroupMapper.selectBizGroup(biz_group_id, false, 0L, 1L, null, null, null, null, null, null, null);
            if (listForTopId != null && listForTopId.size() > 0) {
                biz_group_id = (Long)listForTopId.get(0).get("top_group_id");
                if (biz_group_id == null)
                    biz_group_id = (Long)listForTopId.get(0).get("biz_group_id");
            }
        }

        List<Map<String, Object>> listMap = (List<Map<String, Object>>)transactionMapper.selectTransaction3(false, is_excel, offset, limit, is_group_access,
            biz_group_id, date_start, date_end, uid1, device_name, approval_type, date_type);
        mapResult.put("list", listMap);

        Long totalCount = _AG.getNumberFromQuery("COUNT(*)", transactionMapper.selectTransaction3(true, is_excel, offset, limit, is_group_access,
            biz_group_id, date_start, date_end, uid1, device_name, approval_type, date_type));
        mapResult.put("totalCount", totalCount);
        mapResult.put("offset", offset);
        return mapResult;
    }
}
