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.UserRole;
import ${basePackage}.module.system.mpr.UserRoleMapper;
import ${basePackage}.module.system.req.UserRoleCreateRequest;
import ${basePackage}.module.system.req.UserRoleDeleteRequest;
import ${basePackage}.module.system.req.UserRoleFindRequest;
import ${basePackage}.module.system.req.UserRoleGetRequest;
import ${basePackage}.module.system.req.UserRoleUpdateRequest;
import ${basePackage}.module.system.rsp.UserRoleCreateResponse;
import ${basePackage}.module.system.rsp.UserRoleDeleteResponse;
import ${basePackage}.module.system.rsp.UserRoleFindResponse;
import ${basePackage}.module.system.rsp.UserRoleGetResponse;
import ${basePackage}.module.system.rsp.UserRoleUpdateResponse;

/**
 * USER_ROLE - 用户角色授权
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class UserRoleManagerImpl implements UserRoleManager {

    @Autowired
    private UserRoleMapper userRoleMapper;

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

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

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

        long result = userRoleMapper.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 UserRoleDeleteResponse delete(UserRoleDeleteRequest request, Token token) {
        UserRoleDeleteResponse response = new UserRoleDeleteResponse();

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

        long result = userRoleMapper.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 UserRoleUpdateResponse update(UserRoleUpdateRequest request, Token token) {
        UserRoleUpdateResponse response = new UserRoleUpdateResponse();

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

        long result = userRoleMapper.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 UserRoleFindResponse find(UserRoleFindRequest request, Token token) {
        UserRoleFindResponse response = new UserRoleFindResponse();

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

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

        return response;
    }

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

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

        UserRole po = userRoleMapper.get(request, token);

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

        return response;
    }
}
