package com.interplug.qcast.biz.displayItem;

import java.util.List;
import org.springframework.stereotype.Service;
import com.interplug.qcast.biz.displayItem.dto.BomSyncResponse;
import com.interplug.qcast.biz.displayItem.dto.DisplayItemRequest;
import com.interplug.qcast.biz.displayItem.dto.ItemDetailResponse;
import com.interplug.qcast.biz.displayItem.dto.ItemResponse;
import com.interplug.qcast.biz.displayItem.dto.ItemSyncResponse;
import com.interplug.qcast.biz.displayItem.dto.PriceItemSyncResponse;
import com.interplug.qcast.biz.estimate.EstimateMapper;
import com.interplug.qcast.biz.estimate.dto.NoteRequest;
import com.interplug.qcast.biz.estimate.dto.NoteResponse;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class DisplayItemService {

  private final EstimateMapper estimateMapper;

  private final DisplayItemMapper displayItemMapper;

  /**
   * 판매점 노출 아이템 정보 저장
   *
   * @param displayItemRequest
   * @throws Exception
   */
  public void setStoreDisplayItemSave(DisplayItemRequest displayItemRequest) throws Exception {
    displayItemMapper.setStoreDisplayItemSave(displayItemRequest);
  }

  /**
   * 아이템 목록 조회
   *
   * @param saleStoreId
   * @return
   */
  public List<ItemResponse> getItemList(String saleStoreId) {
    return displayItemMapper.getItemList(saleStoreId);
  }

  /**
   * 아이템 상세 정보 조회
   *
   * @param itemId
   * @return
   */
  public ItemDetailResponse getItemDetail(String itemId) {

    ItemDetailResponse itemDetailResponse = displayItemMapper.getItemDetail(itemId);

    if (itemDetailResponse != null) {
      // BOM 헤더 아이템인 경우 BOM List를 내려준다.
      if ("ERLA".equals(itemDetailResponse.getItemCtgGr())) {
        List<ItemResponse> itemBomList = displayItemMapper.selectItemBomList(itemId);

        itemDetailResponse.setItemBomList(itemBomList);
      }

      // 견적특이사항 관련 데이터 셋팅
      String[] arrItemId = {itemId};

      NoteRequest noteRequest = new NoteRequest();
      noteRequest.setArrItemId(arrItemId);
      noteRequest.setSchSpnTypeCd("PROD");
      List<NoteResponse> noteItemList = estimateMapper.selectEstimateNoteItemList(noteRequest);

      String spnAttrCds = "";
      for (NoteResponse noteResponse : noteItemList) {
        spnAttrCds += !StringUtils.isEmpty(spnAttrCds) ? "、" : "";
        spnAttrCds += noteResponse.getCode();
      }

      itemDetailResponse.setSpnAttrCds(spnAttrCds);
    }

    return itemDetailResponse;
  }

  /**
   * 아이템 정보 동기화
   *
   * @param itemSyncList 아이템 목록
   * @return
   * @throws Exception
   */
  public int setItemSyncSave(List<ItemSyncResponse> itemSyncList) throws Exception {
    int cnt = 0;
    for (ItemSyncResponse itemSyncData : itemSyncList) {
      cnt += displayItemMapper.setItemSyncSave(itemSyncData);
    }
    return cnt;
  }

  /**
   * 아이템 정보 동기화
   *
   * @param bomSyncList 아이템 목록
   * @return
   * @throws Exception
   */
  public int setBomSyncSave(List<BomSyncResponse> bomSyncList) throws Exception {
    int cnt = 0;
    for (BomSyncResponse bomSyncData : bomSyncList) {
      if ("D".equals(bomSyncData.getStatCd())) {
        // Bom 아이템 삭제 처리
        cnt += displayItemMapper.deleteBomSync(bomSyncData);
      } else {
        // Bom 아이템 수정/등록 처리
        cnt += displayItemMapper.setBomSyncSave(bomSyncData);
      }
    }
    return cnt;
  }

  /**
   * 아이템 가격 정보 동기화
   *
   * @param priceItemSyncList 가격 목록
   * @return
   * @throws Exception
   */
  public int setPriceSyncSave(List<PriceItemSyncResponse> priceItemSyncList) throws Exception {
    int cnt = 0;
    for (PriceItemSyncResponse priceItemSyncResponse : priceItemSyncList) {
      cnt += displayItemMapper.setPriceItemSyncSave(priceItemSyncResponse);
    }
    return cnt;
  }

  /**
   * 노출 아이템 동기화 - Batch
   * 
   * @param displayItemList
   * @throws Exception
   */
  public void setStoreDispItemBatch(List<DisplayItemRequest> displayItemList) throws Exception {
    // 노출 아이템 동기화
    for (DisplayItemRequest displayItemRequest : displayItemList) {
      try {
        displayItemMapper.setStoreDisplayItemSave(displayItemRequest);
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }

}
