package pe.fu.rbac.application.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.fu.rbac.application.bean.RbacApplication;
import pe.fu.rbac.application.mapper.RbacApplicationMapper;

/**
 * <h3>应用服务实现类</h3>
 *
 * @author Huang Chen Jun Fu (<a href='mailto:475961393@qq.com'>请联系我^_^</a>)
 * @version 1.0
 * @date 2017年5月11日
 * @since 1.7
 */
@Service("applicationService")
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private RbacApplicationMapper rbacApplicationMapper;

    /**
     * 添加一个应用
     *
     * @param rbacApplication
     * @return
     * @throws Exception
     */
    @Override
    public Boolean addApplication(RbacApplication rbacApplication) throws Exception {
        rbacApplication.setEditTime(new Date());

        int result = rbacApplicationMapper.insert(rbacApplication);
        if (result > 0) {
            return true;
        } else {
            return false;
        }

    }

    /**
     *
     */
    @Override
    public RbacApplication findApplicationByCode(String code) throws Exception {
        List<RbacApplication> applications = rbacApplicationMapper.selectByCode(code);
        if (applications.isEmpty()) {
            return null;
        } else {
            return applications.get(0);
        }
    }

    /**
     * 获取应用列表
     */
    @Override
    public List<RbacApplication> get() throws Exception {
        return rbacApplicationMapper.selectAll();
    }

    /**
     * 删除应用
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Boolean removeApplication(Long id) throws Exception {
        int result = rbacApplicationMapper.deleteByPrimaryKey(id);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

}
