package com.madeu.crm.settings.medicalcategory.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.madeu.constants.Constants;
import com.madeu.crm.settings.medicalcategory.mapper.MedicalDiviProductMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MedicalDiviProductService {

    @Autowired
    private MedicalDiviProductMapper diviProductMapper;

    /**
     * 약품 리스트 조회
     */
    public HashMap<String, Object> getDiviProductList(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();
        List<Map<String, Object>> rows = diviProductMapper.getDiviProductList(paramMap);
        returnMap.put("msgCode", Constants.OK);
        returnMap.put("rows", rows);
        return returnMap;
    }

    /**
     * 약품 등록
     */
    public HashMap<String, Object> insertDiviProduct(HashMap<String, Object> paramMap) throws Exception {
        HashMap<String, Object> returnMap = new HashMap<>();

        int result = diviProductMapper.insertDiviProduct(paramMap);
        if (result > 0) {
            returnMap.put("msgCode", Constants.OK);
            returnMap.put("msgDesc", "약품이 등록되었습니다.");
        } else {
            returnMap.put("msgCode", Constants.FAIL);
            returnMap.put("msgDesc", "등록에 실패했습니다.");
        }
        return returnMap;
    }

    /**
     * 약품 수정
     */
    public HashMap<String, Object> updateDiviProduct(HashMap<String, Object> paramMap) throws Exception {
        HashMap<String, Object> returnMap = new HashMap<>();

        int result = diviProductMapper.updateDiviProduct(paramMap);
        if (result > 0) {
            returnMap.put("msgCode", Constants.OK);
            returnMap.put("msgDesc", "약품 정보가 수정되었습니다.");
        } else {
            returnMap.put("msgCode", Constants.FAIL);
            returnMap.put("msgDesc", "수정에 실패했습니다.");
        }
        return returnMap;
    }

    /**
     * 약품 삭제 (논리 삭제)
     */
    public HashMap<String, Object> deleteDiviProduct(HashMap<String, Object> paramMap) throws Exception {
        HashMap<String, Object> returnMap = new HashMap<>();

        int result = diviProductMapper.deleteDiviProduct(paramMap);
        if (result > 0) {
            returnMap.put("msgCode", Constants.OK);
            returnMap.put("msgDesc", "약품이 삭제되었습니다.");
        } else {
            returnMap.put("msgCode", Constants.FAIL);
            returnMap.put("msgDesc", "삭제에 실패했습니다.");
        }
        return returnMap;
    }

    /**
     * 약품 일괄 저장 (등록/수정/삭제)
     */
    public HashMap<String, Object> saveDiviProductBatch(List<HashMap<String, Object>> list) throws Exception {
        HashMap<String, Object> returnMap = new HashMap<>();
        int successCnt = 0;

        for (HashMap<String, Object> item : list) {
            String action = (String) item.getOrDefault("_action", "");
            if ("insert".equals(action)) {
                diviProductMapper.insertDiviProduct(item);
                successCnt++;
            } else if ("update".equals(action)) {
                diviProductMapper.updateDiviProduct(item);
                successCnt++;
            } else if ("delete".equals(action)) {
                diviProductMapper.deleteDiviProduct(item);
                successCnt++;
            }
        }

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("msgDesc", successCnt + "건이 처리되었습니다.");
        return returnMap;
    }

    /**
     * 제품(약품) 목록 검색
     */
    public HashMap<String, Object> searchProductList(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();
        List<Map<String, Object>> rows = diviProductMapper.searchProductList(paramMap);
        returnMap.put("msgCode", Constants.OK);
        returnMap.put("rows", rows);
        return returnMap;
    }
}
