package com.madeu.service.mobile.mobilenotice.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobilenotice.MobileNoticeSqlMapDAO;
import com.madeu.service.mobile.mobilenotice.MobileNoticeService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service("MobileNoticeService")
public class MobileNoticeServiceImpl implements MobileNoticeService {
    @Autowired
    private MobileNoticeSqlMapDAO mobileNoticeSqlMapDAO;
    /**
     * 공지사항 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListNotice(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileNoticeServiceImpl selectListNotice START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();

        try{
                List<Map<String, Object>> totalCountListMap = mobileNoticeSqlMapDAO.selectTotalNoticeCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

                if(0 < totalCount) {
                    // 리스트 조회
                    listMap = mobileNoticeSqlMapDAO.selectListNotice(paramMap);
                }

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("totalCount", totalCount);
                map.put("rows",listMap);
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileNoticeServiceImpl selectListNotice END");
        return map;
    }

    /**
     * 공지사항 정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectNotice(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileNoticeServiceImpl selectNotice START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        try{
            boolean check  = true;
            String muNoticeId    = String.valueOf(paramMap.get("muNoticeId"));

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muNoticeId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","공지사항 정보가 없습니다.");
            }

            if(true == check){
                List<Map<String, Object>> listMap = mobileNoticeSqlMapDAO.selectNotice(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileNoticeServiceImpl selectAppNotice END");
        return map;
    }
}
