package ${domain}.module.wsvr.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.auth.Token;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsvr.ent.Logerr;
import ${domain}.module.wsvr.mpr.LogerrMapper;
import ${domain}.module.wsvr.req.LogerrCreateRequest;
import ${domain}.module.wsvr.req.LogerrDeleteRequest;
import ${domain}.module.wsvr.req.LogerrFindRequest;
import ${domain}.module.wsvr.req.LogerrUpdateRequest;
import ${domain}.module.wsvr.rsp.LogerrCreateResponse;
import ${domain}.module.wsvr.rsp.LogerrDeleteResponse;
import ${domain}.module.wsvr.rsp.LogerrFindResponse;
import ${domain}.module.wsvr.rsp.LogerrUpdateResponse;

/**
 * LOGERR - 错误日志
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-07-06
 */
@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();
        ${domain}.module.wsvr.ent.Logerr entity = MapperUtil.map(request, ${domain}.module.wsvr.ent.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;
        }

        ${domain}.module.wsvr.ent.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<${domain}.module.wsvr.ent.Logerr> pageInfo = new PageInfo<>(logerrMapper.find(request, token));

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

        return response;
    }

    /**
     * 快速添加错误日志
     *
     * @param logType 日志类型
     * @param logTitle 标题
     * @param logNote 描述
     * @return
     */
    public boolean addErr(String logType, String logTitle, String logNote){
        Logerr entity = new Logerr();
        entity.setId(IDgenerator.nextId());
        entity.setLogTitle(logTitle);
        entity.setLogNote(logNote);
        entity.setLogType(logType);
        entity.setLogState("0");

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