package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.dao.UserMessageMapper;
import com.qxgmat.data.dao.entity.UserMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class UserMessageService  extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(UserMessageService.class);

    @Resource
    private UserMessageMapper userMessageMapper;

    /**
     * 合并用户信息，将old转移至new
     * @param oldUserId
     * @param newUserId
     */
    public void mergeUser(Number oldUserId, Integer newUserId){
        Example example = new Example(UserMessage.class);
        example.and(
                example.createCriteria().andEqualTo("userId", oldUserId)
        );
        update(userMessageMapper, example, UserMessage.builder().userId(newUserId).build());
    }

    /**
     * 清除用户消息状态
     * @param userId
     */
    public void clearAll(Number userId){
        Example example = new Example(UserMessage.class);
        example.and(
                example.createCriteria().andEqualTo("userId", userId)
        );
        update(userMessageMapper, example, UserMessage.builder().isRead(1).build());
    }

    /**
     * 清楚单条消息状态
     * @param userId
     * @param id
     */
    public void clear(Number userId, Integer id){
        Example example = new Example(UserMessage.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("id", id)
        );
        update(userMessageMapper, example, UserMessage.builder().isRead(1).build());
    }

    public UserMessage add(UserMessage message){
        int result = insert(userMessageMapper, message);
        message = one(userMessageMapper, message.getId());
        if(message == null){
            throw new SystemException("消息添加失败");
        }
        return message;
    }

    public UserMessage edit(UserMessage message){
        UserMessage in = one(userMessageMapper, message.getId());
        if(in == null){
            throw new ParameterException("消息不存在");
        }
        int result = update(userMessageMapper, message);
        return message;
    }

    public boolean delete(Number id){
        UserMessage in = one(userMessageMapper, id);
        if(in == null){
            throw new ParameterException("消息不存在");
        }
        int result = delete(userMessageMapper, id);
        return result > 0;
    }

    public UserMessage get(Number id){
        UserMessage in = one(userMessageMapper, id);

        if(in == null){
            throw new ParameterException("消息不存在");
        }
        return in;
    }

    public Page<UserMessage> select(int page, int pageSize){
        return select(userMessageMapper, page, pageSize);
    }

    public Page<UserMessage> select(int page, int pageSize, Number userId, String type, Integer read){
        Example example = new Example(UserMessage.class);
        if (userId != null)
            example.and(
                    example.createCriteria().andEqualTo("userId", userId)
            );
        if (type != null)
            example.and(
                    example.createCriteria().andEqualTo("type", type)
            );
        if (read != null)
            example.and(
                    example.createCriteria().andEqualTo("isRead", read > 0?1:0)
            );
        example.setOrderByClause("id desc");
        return select(userMessageMapper, example, page, pageSize);
    }

    public List<UserMessage> select(Collection ids){
        return select(userMessageMapper, ids);
    }

}
