package ${basePackage}.module.system.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.auth.Token;
import ${basePackage}.frame.utils.IDgenerator;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.utils.Message;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.module.system.ent.LogErr;
import ${basePackage}.module.system.mpr.LogErrMapper;
import ${basePackage}.module.system.req.LogErrCreateRequest;
import ${basePackage}.module.system.req.LogErrDeleteRequest;
import ${basePackage}.module.system.req.LogErrFindRequest;
import ${basePackage}.module.system.req.LogErrGetRequest;
import ${basePackage}.module.system.req.LogErrUpdateRequest;
import ${basePackage}.module.system.rsp.LogErrCreateResponse;
import ${basePackage}.module.system.rsp.LogErrDeleteResponse;
import ${basePackage}.module.system.rsp.LogErrFindResponse;
import ${basePackage}.module.system.rsp.LogErrGetResponse;
import ${basePackage}.module.system.rsp.LogErrUpdateResponse;

/**
 * LOG_ERR - 错误日志
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class LogErrManagerImpl implements ${basePackage}.module.system.mgr.LogErrManager {

    @Autowired
    private LogErrMapper logErrMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public LogErrCreateResponse create(LogErrCreateRequest request, Token token) {
        LogErrCreateResponse response = new LogErrCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long id = IDgenerator.nextId();
        LogErr entity = MapperUtil.map(request, LogErr.class);
        entity.setId(id);

        long result = logErrMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public LogErrDeleteResponse delete(LogErrDeleteRequest request, Token token) {
        LogErrDeleteResponse response = new LogErrDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = logErrMapper.delete(request, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public LogErrUpdateResponse update(LogErrUpdateRequest request, Token token) {
        LogErrUpdateResponse response = new LogErrUpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = logErrMapper.update(request, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public LogErrFindResponse find(LogErrFindRequest request, Token token) {
        LogErrFindResponse response = new LogErrFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<LogErr> pageInfo = new PageInfo<>(logErrMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }

    /**
     * 获得对象
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public LogErrGetResponse get(LogErrGetRequest request, Token token) {
        LogErrGetResponse response = new LogErrGetResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        LogErr po = logErrMapper.get(request, token);

        if (po != null) {
            response.setLogErr(po);
        } else {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
        }

        return response;
    }
}
