package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.Transform;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.module.FeedbackModule;
import com.qxgmat.data.constants.enums.status.FeedbackStatus;
import com.qxgmat.data.dao.UserFeedbackErrorMapper;
import com.qxgmat.data.dao.entity.UserFeedbackError;
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 UserFeedbackErrorService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(UserFeedbackErrorService.class);

    @Resource
    private UserFeedbackErrorMapper userFeedbackErrorMapper;


    public Page<UserFeedbackError> listAdmin(int page, int pageSize, FeedbackModule module, FeedbackStatus status, String keyword){
//        String moduleKey = module != null ? module.key : "";
//        Integer statusIndex = status != null ? status.index : null;
        Example example = new Example(UserFeedbackError.class);
        if (module != null)
            example.and(
                    example.createCriteria()
                    .andEqualTo("module", module.key)
            );
        if (status != null)
            example.and(
                    example.createCriteria()
                            .andEqualTo("status", status.index)
            );
        if(!keyword.isEmpty())
            example.and(
                    example.createCriteria()
                            .andLike("title", "%"+keyword+"%")
            );
        return select(userFeedbackErrorMapper, page, pageSize);
    }

    public UserFeedbackError add(UserFeedbackError feedbackError){
        int result = insert(userFeedbackErrorMapper, feedbackError);
        feedbackError = one(userFeedbackErrorMapper, feedbackError.getId());
        if(feedbackError == null){
            throw new SystemException("勘误添加失败");
        }
        return feedbackError;
    }

    public UserFeedbackError edit(UserFeedbackError feedbackError){
        UserFeedbackError in = one(userFeedbackErrorMapper, feedbackError.getId());
        if(in == null){
            throw new ParameterException("勘误不存在");
        }
        int result = update(userFeedbackErrorMapper, feedbackError);
        return feedbackError;
    }

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

    public UserFeedbackError get(Number id){
        UserFeedbackError in = one(userFeedbackErrorMapper, id);

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

    public Page<UserFeedbackError> select(int page, int pageSize){
        return select(userFeedbackErrorMapper, page, pageSize);
    }

    public List<UserFeedbackError> select(Collection ids){
        return select(userFeedbackErrorMapper, ids);
    }

}
