package com.izouma.awesomeadmin.service.impl;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.ChatInfoMapper;
import com.izouma.awesomeadmin.dao.UsersSessionMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.ChatInfo;
import com.izouma.awesomeadmin.model.UsersSession;
import com.izouma.awesomeadmin.service.ChatInfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ChatInfoServiceImpl implements ChatInfoService {

    private static Logger logger = Logger.getLogger(ChatInfoServiceImpl.class);

    @Autowired
    private ChatInfoMapper chatInfoMapper;

    @Autowired
    private UsersSessionMapper usersSessionMapper;


    @Override
    public List<ChatInfo> getChatInfoList() {

        logger.info("getChatInfoList");
        try {

            return chatInfoMapper.queryAllChatInfo();
        } catch (Exception e) {
            logger.error("getChatInfoList", e);
        }
        return null;
    }


    @Override
    public List<ChatInfo> getChatInfoByPage(Page page, ChatInfo chatInfo) {

        logger.info("getChatInfoByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("chatInfo", chatInfo);
            parameter.put(AppConstant.PAGE, page);

            return chatInfoMapper.queryChatInfosByPage(parameter);
        } catch (Exception e) {
            logger.error("getChatInfoByPage", e);
        }
        return null;
    }


    @Override
    public ChatInfo getChatInfoById(String id) {

        logger.info("getChatInfoById");
        try {

            return chatInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getChatInfoById", e);
        }
        return null;
    }


    @Override
    public ChatInfo getChatInfo(ChatInfo chatInfo) {

        logger.info("getChatInfo");
        try {

            return chatInfoMapper.queryChatInfo(chatInfo);
        } catch (Exception e) {
            logger.error("getChatInfo", e);
        }
        return null;
    }


    @Override
    public boolean createChatInfo(ChatInfo chatInfo) {
        logger.info("createChatInfo");
        try {

            int updates = chatInfoMapper.insertSelective(chatInfo);

            UsersSession usersSession = new UsersSession();
            usersSession.setUserId(chatInfo.getUserId());
            usersSession.setStoreId(chatInfo.getStoreId());

            List<UsersSession> usersSessionList = usersSessionMapper.queryUsersSessionList(usersSession);
            if (usersSessionList != null && usersSessionList.size() > 0) {
                if (usersSessionList.size() == 1) {
                    usersSession.setId(null);
                    if (usersSessionList.get(0).getType() == 0) {
                        usersSession.setType(1);
                    } else {
                        usersSession.setType(0);
                    }

                    usersSessionMapper.insertSelective(usersSession);
                }
            } else {
                usersSession.setId(null);
                usersSession.setType(0);
                usersSessionMapper.insertSelective(usersSession);

                usersSession.setId(null);
                usersSession.setType(1);
                usersSessionMapper.insertSelective(usersSession);
            }

            usersSessionMapper.updateByChatInfo(chatInfo);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createChatInfo", e);
        }
        return false;
    }

    @Override
    public boolean deleteChatInfo(String id) {

        logger.info("deleteChatInfo");
        try {

            int updates = chatInfoMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteChatInfo", e);
        }
        return false;
    }


    @Override
    public boolean updateChatInfo(ChatInfo chatInfo) {

        logger.info("updateChatInfo");
        try {
            return chatInfoMapper.updateByPrimaryKeySelective(chatInfo) > 0;
        } catch (Exception e) {
            logger.error("updateChatInfo", e);
        }
        return false;
    }


    @Override
    public boolean readAll(ChatInfo chatInfo) {

        return chatInfoMapper.readAll(chatInfo) > 0;
    }


}
