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.Role;
import ${basePackage}.module.system.mpr.RoleMapper;
import ${basePackage}.module.system.req.RoleCreateRequest;
import ${basePackage}.module.system.req.RoleDeleteRequest;
import ${basePackage}.module.system.req.RoleFindRequest;
import ${basePackage}.module.system.req.RoleGetRequest;
import ${basePackage}.module.system.req.RoleUpdateRequest;
import ${basePackage}.module.system.rsp.RoleCreateResponse;
import ${basePackage}.module.system.rsp.RoleDeleteResponse;
import ${basePackage}.module.system.rsp.RoleFindResponse;
import ${basePackage}.module.system.rsp.RoleGetResponse;
import ${basePackage}.module.system.rsp.RoleUpdateResponse;

/**
 * ROLE - 角色
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class RoleManagerImpl implements RoleManager {

    @Autowired
    private RoleMapper roleMapper;

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

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

        {//检查Code是否重复
            RoleFindRequest roleFindRequest = new RoleFindRequest();
            roleFindRequest.setCode(request.getCode());
            RoleFindResponse roleFindResponse = find(roleFindRequest, token);
            if (roleFindResponse.hasError()) {
                response.addErrors(roleFindResponse.getErrors());
                return response;
            } else if (roleFindResponse.getResult().size() > 0) {
                response.addError(ErrorType.BUSINESS_ERROR, "[" + request.getCode() + "]已存在");
                return response;
            }
        }

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

        long result = roleMapper.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 RoleDeleteResponse delete(RoleDeleteRequest request, Token token) {
        RoleDeleteResponse response = new RoleDeleteResponse();

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

        long result = roleMapper.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 RoleUpdateResponse update(RoleUpdateRequest request, Token token) {
        RoleUpdateResponse response = new RoleUpdateResponse();

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

        long result = roleMapper.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 RoleFindResponse find(RoleFindRequest request, Token token) {
        RoleFindResponse response = new RoleFindResponse();

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

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

        return response;
    }

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

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

        Role po = roleMapper.get(request, token);

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

        return response;
    }
}
