package com.oqpo.api.service.system;

import com.oqpo.api.entity.ActionEntity;
import com.oqpo.api.entity.oper.MemberEntity;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.system.ActionMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.act.SaveActionRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.act.ActionResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
public class ActionService extends CommonService {
    @Autowired
    private ActionMapper actionMapper ;

    //@Cacheable(value = "actionList", key = "'OQKeyIs' + #userId")
    public List<ActionEntity> actionList(String user_id, MemberEntity memberEntity) throws Exception {

        List<ActionEntity> entities = actionMapper.selectUserActionList(memberEntity);
        /*
        List<ActionEntity> entities = actionMapper.selectActionList(userId);
        entities.forEach(entity -> {
            entity.setActionBtnList(actionMapper.selectActionBtnList(entity.getMenuNo()));
        });
        */
        return entities;
    }

    /*
      액션 상세 리스트 조회
     */
    public GridResponse actDtlGridList(Integer menuNo, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        int gridRecords = actionMapper.selectActionDtlGridListCnt(menuNo);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<ActionEntity> entities = actionMapper.selectActionGridList(menuNo, gridRequest);

        List<Object> gridRows = entities.stream()
                .map(m -> ActionResponse.builder()
                        .viewCd("R")
                        .menuNo(m.getMenuNo())
                        .actId(m.getActId())
                        .actTpCd(m.getActTpCd())
                        .actTpNm(m.getActTpNm())
                        .actNm(m.getActNm())
                        .useYn(m.getUseYn())
                        .connUrl(m.getConnUrl())
                        .writeYn(m.getWriteYn())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }


    /*
      액션관리 - 저장
     */
    @CacheEvict(value = {"actionList"}, allEntries = true)
    @Transactional
    public void saveAction(String userId, SaveActionRequest saveActionRequest) throws Exception {
        try {

            // 기본정보 저장
            Integer menuNo = saveActionRequest.getMenuNo();
            //  액션 상세 정보 저장
            // 신규건
            List<ActionEntity> gridInsertData = saveActionRequest.toCodeEntities(saveActionRequest.getGridInsertData());
            if (gridInsertData.size() > 0) {
                actionMapper.insertActInfo(userId, gridInsertData) ;
            }
            // 수정건
            List<ActionEntity> gridUpdateData = saveActionRequest.toCodeEntities(saveActionRequest.getGridUpdateData());
            if (gridUpdateData.size() > 0) {
                actionMapper.updateActInfo(userId, gridUpdateData) ;
            }
            // 삭제건
            List<ActionEntity> gridDeleteData = saveActionRequest.toCodeEntities(saveActionRequest.getGridDeleteData());
            if (gridDeleteData.size() > 0) {
                actionMapper.deleteActInfo(menuNo, gridDeleteData) ;
            }
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }
}
