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.SentenceArticleMapper;
import com.qxgmat.data.dao.SentenceCodeMapper;
import com.qxgmat.data.dao.entity.SentenceArticle;
import com.qxgmat.data.dao.entity.SentenceCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Resource
    private SentenceCodeMapper sentenceCodeMapper;

    /**
     * 查询并激活
     * @param userId
     * @param code
     * @return
     */
    @Transactional
    public SentenceCode active(Integer userId, String code){
        Example example = new Example(SentenceCode.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("code", code)
        );
        SentenceCode entity = one(sentenceCodeMapper, example);
        if (entity==null){
            throw new ParameterException("code错误");
        }
        if (entity.getUserId() > 0){
            throw new ParameterException("code已被使用");
        }
        entity.setUserId(userId);
        edit(entity);
        return entity;
    }

    /**
     * 获取用户code激活
     * @param userId
     * @return
     */
    public SentenceCode isActive(Integer userId){
        Example example = new Example(SentenceCode.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
        );
        return one(sentenceCodeMapper, example);
    }

    public SentenceCode add(SentenceCode code){
        int result = insert(sentenceCodeMapper, code);
        code = one(sentenceCodeMapper, code.getId());
        if(code == null){
            throw new SystemException("code添加失败");
        }
        return code;
    }

    public SentenceCode edit(SentenceCode code){
        SentenceCode in = one(sentenceCodeMapper, code.getId());
        if(in == null){
            throw new ParameterException("code不存在");
        }
        int result = update(sentenceCodeMapper, code);
        return code;
    }

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

    public SentenceCode get(Number id){
        SentenceCode in = one(sentenceCodeMapper, id);

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

    public Page<SentenceCode> select(int page, int pageSize){
        return select(sentenceCodeMapper, page, pageSize);
    }

    public List<SentenceCode> select(Collection ids){
        return select(sentenceCodeMapper, ids);
    }

}
