package ${domain}.module.wsys.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 ${domain}.frame.auth.LocalData;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.auth.Token;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.LogErr;
import ${domain}.module.wsys.mpr.LogErrMapper;
import ${domain}.module.wsys.req.LogErrCreateRequest;
import ${domain}.module.wsys.req.LogErrDeleteRequest;
import ${domain}.module.wsys.req.LogErrFindRequest;
import ${domain}.module.wsys.req.LogErrUpdateRequest;
import ${domain}.module.wsys.rsp.LogErrCreateResponse;
import ${domain}.module.wsys.rsp.LogErrDeleteResponse;
import ${domain}.module.wsys.rsp.LogErrFindResponse;
import ${domain}.module.wsys.rsp.LogErrUpdateResponse;

/**
 * LOG_ERR - 错误日志
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class LogErrManagerImpl implements 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.getId(), 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;
        }

        LogErr entity = logErrMapper.getById(request.getId(), token);
        if (entity == null) {
        response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
        return response;
        }

        MapperUtil.map(request, entity);
        long result = logErrMapper.update(entity, 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 LogErrType SYS_ERR、BIZ_ERR
     * @param logTitle 标题
     * @param logNote 描述
     * @return
     */
    public boolean addErr(String LogErrType, String logTitle, String logNote){
        LogErr entity = new LogErr();
        entity.setId(IDgenerator.nextId());
        entity.setLogTitle(logTitle);
        entity.setLogNote(logNote);
        entity.setLogErrType(LogErrType);
        entity.setLogErrResult("0");

        long result = logErrMapper.insert(entity, LocalData.getSysToken());
        if (1L != result) {
            return false;
        }
        return true;
    }
}
