package com.oqpo.api.service.stockmng;


import com.oqpo.api.entity.stockmng.*;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.stockmng.WhsMngMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.stockmng.SaveWhsInfoRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.stockmng.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WhsMngService extends CommonService {

    @Autowired
    private WhsMngMapper whsMngMapper;

    /*
      창고관리 그리드 리스트 조회
     */
    public GridResponse selectWhsMngGridList(String sBrandId, String sStoreId, String sWhsDvsn, String sWhsStCd,
                                             String sWhsNm, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        int gridRecords = whsMngMapper.selectWhsMngGridCnt(sBrandId, sStoreId, sWhsDvsn, sWhsStCd, sWhsNm);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<WhsMgntBaseInfoEntity> entities = whsMngMapper.selectWhsMngGridList(sBrandId, sStoreId, sWhsDvsn, sWhsStCd, sWhsNm, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> WhsMngListResponse.builder()
                        .viewCd("R")
                        .brandId(m.getBrandId())
                        .brandNm(m.getBrandNm())
                        .whsId(m.getWhsId())
                        .whsNm(m.getWhsNm())
                        .whsDvsn(m.getWhsDvsn())
                        .whsDvsnNm(m.getWhsDvsnNm())
                        .mgrNm(m.getMgrNm())
                        .mgrTelNo(m.getMgrTelNo())
                        .whsStCd(m.getWhsStCd())
                        .whsStNm(m.getWhsStNm())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    /* 창고관리 정보 조회 */
    public WhsMngInfoResponse selectWhsMngInfo(String brandId, String whsId) throws Exception {
        WhsMgntBaseInfoEntity entity = whsMngMapper.selectWhsMgntBaseInfo(brandId, whsId);
        List<WhsMgntBaseLocEntity> locEntityList = whsMngMapper.selectWhsLocGridList(brandId, whsId);
        return WhsMngInfoResponse.toDTO(entity, locEntityList);
    }

    /* 창고정보 등록 */
    @Transactional
    public void addWhsInfo(String userId, SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        try {
            // 창고관리기본정보
            WhsMgntBaseInfoEntity entity = new WhsMgntBaseInfoEntity();
            entity.setBrandId(saveWhsInfoRequest.getBrandId());
            entity.setWhsId(fnGetDealNo(28, "")); // 창고아이디 채번
            entity.setWhsNm(saveWhsInfoRequest.getWhsNm());
            entity.setWhsDvsn(saveWhsInfoRequest.getWhsDvsn());
            entity.setMgrNm(saveWhsInfoRequest.getMgrNm());
            entity.setMgrTelNo(saveWhsInfoRequest.getMgrTelNo());
            entity.setZipNo(saveWhsInfoRequest.getZipNo());
            entity.setAddr1(saveWhsInfoRequest.getAddr1());
            entity.setAddr2(saveWhsInfoRequest.getAddr2());
            entity.setWhsStCd(saveWhsInfoRequest.getWhsStCd());
            entity.setStoreId(saveWhsInfoRequest.getStoreId());
            whsMngMapper.insertWhsMgntBaseInfo(userId, entity);

            // 창고로케이션정보
            List<WhsMgntBaseLocEntity> locEntities = saveWhsInfoRequest.toEntities(saveWhsInfoRequest.getGridInsertData());
            for (WhsMgntBaseLocEntity locEntity : locEntities) {
                whsMngMapper.insertWhsMgntBaseLoc(userId, locEntity);
            }
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /* 창고정보 수정 */
    @Transactional
    public void modifyWhsInfo(String userId, SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        try {
            // 창고관리기본정보
            WhsMgntBaseInfoEntity entity = new WhsMgntBaseInfoEntity();
            entity.setBrandId(saveWhsInfoRequest.getBrandId());
            entity.setWhsId(saveWhsInfoRequest.getWhsId());
            entity.setWhsNm(saveWhsInfoRequest.getWhsNm());
            entity.setWhsDvsn(saveWhsInfoRequest.getWhsDvsn());
            entity.setMgrNm(saveWhsInfoRequest.getMgrNm());
            entity.setMgrTelNo(saveWhsInfoRequest.getMgrTelNo());
            entity.setZipNo(saveWhsInfoRequest.getZipNo());
            entity.setAddr1(saveWhsInfoRequest.getAddr1());
            entity.setAddr2(saveWhsInfoRequest.getAddr2());
            entity.setWhsStCd(saveWhsInfoRequest.getWhsStCd());
            entity.setStoreId(saveWhsInfoRequest.getStoreId());
            whsMngMapper.updateWhsMgntBaseInfo(userId, entity);

            // 창고로케이션정보
            // 삭제
            List<WhsMgntBaseLocEntity> locEntities = saveWhsInfoRequest.toEntities(saveWhsInfoRequest.getGridDeleteData());
            for (WhsMgntBaseLocEntity locEntity : locEntities) {
                whsMngMapper.deleteWhsMgntBaseLoc(userId, locEntity);
            }
            // 수정
            locEntities = saveWhsInfoRequest.toEntities(saveWhsInfoRequest.getGridUpdateData());
            for (WhsMgntBaseLocEntity locEntity : locEntities) {
                whsMngMapper.updateWhsMgntBaseLoc(userId, locEntity);
            }
            // 등록
            locEntities = saveWhsInfoRequest.toEntities(saveWhsInfoRequest.getGridInsertData());
            for (WhsMgntBaseLocEntity locEntity : locEntities) {
                whsMngMapper.insertWhsMgntBaseLoc(userId, locEntity);
            }
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /* 창고정보 삭제 */
    @Transactional
    public void removeWhsInfo(String userId, SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        try {
            // 창고관리기본정보
            WhsMgntBaseInfoEntity entity = new WhsMgntBaseInfoEntity();
            entity.setBrandId(saveWhsInfoRequest.getBrandId());
            entity.setWhsId(saveWhsInfoRequest.getWhsId());
            whsMngMapper.deleteWhsMgntBaseInfo(userId, entity);

            // 창고로케이션정보
            whsMngMapper.deleteWhsMgntBaseLoc4Info(userId, entity);
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

}
