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.auth.Token;
import ${basePackage}.frame.base.ErrorType;
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.Visitor;
import ${basePackage}.module.system.mpr.VisitorMapper;
import ${basePackage}.module.system.req.VisitorCreateRequest;
import ${basePackage}.module.system.req.VisitorDeleteRequest;
import ${basePackage}.module.system.req.VisitorFindRequest;
import ${basePackage}.module.system.req.VisitorUpdateRequest;
import ${basePackage}.module.system.rsp.VisitorCreateResponse;
import ${basePackage}.module.system.rsp.VisitorDeleteResponse;
import ${basePackage}.module.system.rsp.VisitorFindResponse;
import ${basePackage}.module.system.rsp.VisitorUpdateResponse;

/**
 * VISITOR - 应用接入
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-06-25
 */
@Transactional
@Service
public class VisitorManagerImpl implements VisitorManager {

    @Autowired
    private VisitorMapper visitorMapper;

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

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

        {// 应用码唯一检查
            VisitorFindRequest visitorFindRequest = new VisitorFindRequest();
            visitorFindRequest.setAppKey(request.getAppKey());
            VisitorFindResponse visitorFindResponse = this.find(visitorFindRequest, token);
            if (visitorFindResponse.hasError()) {
                response.addErrors(visitorFindResponse.getErrors());
                return response;
            } else if (visitorFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[appKey]应用码已存在，请检查!");
                return response;
            }
        }

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

        long result = visitorMapper.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 VisitorDeleteResponse delete(VisitorDeleteRequest request, Token token) {
        VisitorDeleteResponse response = new VisitorDeleteResponse();

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

        long result = visitorMapper.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 VisitorUpdateResponse update(VisitorUpdateRequest request, Token token) {
        VisitorUpdateResponse response = new VisitorUpdateResponse();

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

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

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

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

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

        return response;
    }
}
