package com.erp.service;

import com.erp.common.BaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@RequiredArgsConstructor
@Slf4j
public class ProcessWorkStandardService extends BaseService {

    private final CommonService commonService;

    private static final String NS = "processWorkStandard.";

    public Map<String, Object> getProcessWorkStandardItemList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        commonService.applyPagination(params);
        Integer totalObj = sqlSession.selectOne(NS + "getProcessWorkStandardItemListCnt", params);
        int totalCount = totalObj != null ? totalObj : 0;
        List<Map<String, Object>> list = sqlSession.selectList(NS + "getProcessWorkStandardItemList", params);
        return commonService.buildListResponse(list, totalCount, params);
    }

    public List<Map<String, Object>> getProcessWorkStandardRoutingList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList(NS + "getProcessWorkStandardRoutingList", params);
    }

    @Transactional
    public Map<String, Object> setProcessWorkStandardDefaultVersion(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "setProcessWorkStandardDefaultVersion", params);
        return params;
    }

    @Transactional
    public Map<String, Object> unsetProcessWorkStandardDefaultVersion(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "unsetProcessWorkStandardDefaultVersion", params);
        return params;
    }

    public List<Map<String, Object>> getProcessWorkStandardWorkItemList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList(NS + "getProcessWorkStandardWorkItemList", params);
    }

    @Transactional
    public Map<String, Object> insertProcessWorkStandardWorkItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertProcessWorkStandardWorkItem", params);
        return params;
    }

    @Transactional
    public Map<String, Object> updateProcessWorkStandardWorkItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "updateProcessWorkStandardWorkItem", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteProcessWorkStandardWorkItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deleteProcessWorkStandardWorkItem", params);
        return params;
    }

    public List<Map<String, Object>> getProcessWorkStandardWorkItemDetailList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList(NS + "getProcessWorkStandardWorkItemDetailList", params);
    }

    @Transactional
    public Map<String, Object> insertProcessWorkStandardWorkItemDetail(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertProcessWorkStandardWorkItemDetail", params);
        return params;
    }

    @Transactional
    public Map<String, Object> updateProcessWorkStandardWorkItemDetail(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "updateProcessWorkStandardWorkItemDetail", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteProcessWorkStandardWorkItemDetail(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deleteProcessWorkStandardWorkItemDetail", params);
        return params;
    }

    @Transactional
    @SuppressWarnings("unchecked")
    public Map<String, Object> saveAllProcessWorkStandard(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        List<Map<String, Object>> workItems = (List<Map<String, Object>>) params.get("work_items");
        if (workItems != null) {
            for (Map<String, Object> workItem : workItems) {
                workItem.put("company_code", params.get("company_code"));
                if (workItem.get("id") != null) {
                    sqlSession.update(NS + "updateProcessWorkStandardWorkItem", workItem);
                } else {
                    sqlSession.insert(NS + "insertProcessWorkStandardWorkItem", workItem);
                }
                List<Map<String, Object>> details = (List<Map<String, Object>>) workItem.get("details");
                if (details != null) {
                    for (Map<String, Object> detail : details) {
                        detail.put("company_code", params.get("company_code"));
                        detail.put("work_item_id", workItem.get("id"));
                        if (detail.get("id") != null) {
                            sqlSession.update(NS + "updateProcessWorkStandardWorkItemDetail", detail);
                        } else {
                            sqlSession.insert(NS + "insertProcessWorkStandardWorkItemDetail", detail);
                        }
                    }
                }
            }
        }
        return params;
    }

    public List<Map<String, Object>> getProcessWorkStandardRegisteredItemList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList(NS + "getProcessWorkStandardRegisteredItemList", params);
    }

    @Transactional
    public Map<String, Object> insertProcessWorkStandardRegisteredItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertProcessWorkStandardRegisteredItem", params);
        return params;
    }

    @Transactional
    @SuppressWarnings("unchecked")
    public Map<String, Object> insertProcessWorkStandardRegisteredItemBatch(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        List<Map<String, Object>> items = (List<Map<String, Object>>) params.get("items");
        if (items != null) {
            for (Map<String, Object> item : items) {
                item.put("company_code", params.get("company_code"));
            }
        }
        sqlSession.insert(NS + "insertProcessWorkStandardRegisteredItemBatch", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteProcessWorkStandardRegisteredItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deleteProcessWorkStandardRegisteredItem", params);
        return params;
    }
}
