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

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobileuser.MobileUserSqlMapDAO;
import com.madeu.dao.mobile.mobiledevice.MobileDeviceSqlMapDAO;
import com.madeu.service.mobile.mobiledevice.MobileDeviceService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service("MobileDeviceService")
public class MobileDeviceServiceImpl implements MobileDeviceService {
    
    @Autowired
    private MobileDeviceSqlMapDAO mobileDeviceSqlMapDAO;

    @Autowired
    private MobileUserSqlMapDAO mobileUserSqlMapDAO;

    /**
     * 디바이스 푸시 알림 여부 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectDevicePushYn(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileDeviceServiceImpl selectDevicePushYn START");

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

        log.debug("muUserId       :"+paramMap.get("muUserId"));
        log.debug("deviceUuid     :"+paramMap.get("deviceUuid"));

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = mobileUserSqlMapDAO.checkUser(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

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

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

    /**
     * 고객 1:1 문의 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateDevicePushYn(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("MobileDeviceServiceImpl updateDevicePushYn START");

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

        log.debug("muUserId       :"+paramMap.get("muUserId"));
        log.debug("deviceUuid     :"+paramMap.get("deviceUuid"));
        log.debug("type           :"+paramMap.get("type"));
        log.debug("pushYn         :"+paramMap.get("pushYn"));

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = mobileUserSqlMapDAO.checkUser(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                mobileDeviceSqlMapDAO.updateDevicePushYn(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileDeviceServiceImpl updateDevicePushYn END");
        return map;
    }
}
