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.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.wsys.ent.VisitorRes;
import ${domain}.module.wsys.mpr.VisitorResMapper;
import ${domain}.module.wsys.req.VisitorResCreateRequest;
import ${domain}.module.wsys.req.VisitorResDeleteRequest;
import ${domain}.module.wsys.req.VisitorResFindRequest;
import ${domain}.module.wsys.req.VisitorResUpdateRequest;
import ${domain}.module.wsys.rsp.VisitorResCreateResponse;
import ${domain}.module.wsys.rsp.VisitorResDeleteResponse;
import ${domain}.module.wsys.rsp.VisitorResFindResponse;
import ${domain}.module.wsys.rsp.VisitorResUpdateResponse;

/**
 * VISITOR_RES - 应用资源关系
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-06-25
 */
@Transactional
@Service
public class VisitorResManagerImpl implements VisitorResManager {

    @Autowired
    private VisitorResMapper visitorResMapper;

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

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

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

        long result = visitorResMapper.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 VisitorResDeleteResponse delete(VisitorResDeleteRequest request, Token token) {
        VisitorResDeleteResponse response = new VisitorResDeleteResponse();

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

        long result = visitorResMapper.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 VisitorResUpdateResponse update(VisitorResUpdateRequest request, Token token) {
        VisitorResUpdateResponse response = new VisitorResUpdateResponse();

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

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

        MapperUtil.map(request, entity);
        long result = visitorResMapper.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 VisitorResFindResponse find(VisitorResFindRequest request, Token token) {
        VisitorResFindResponse response = new VisitorResFindResponse();

        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<VisitorRes> pageInfo = new PageInfo<>(visitorResMapper.find(request, token));

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

        return response;
    }
}
