package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.ManagerMapper;
import com.qxgmat.data.dao.ManagerRoleMapper;
import com.qxgmat.data.dao.entity.ManagerRole;
import com.qxgmat.service.ManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class ManagerRoleService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(ManagerService.class);
    @Resource
    private ManagerRoleMapper managerRoleMapper;

    public ManagerRole add(ManagerRole managerRole){
        int result = insert(managerRoleMapper, managerRole);
        managerRole = one(managerRoleMapper, managerRole.getId());
        if(managerRole == null){
            throw new SystemException("角色添加失败");
        }
        return managerRole;
    }

    public ManagerRole edit(ManagerRole managerRole){
        ManagerRole in = one(managerRoleMapper, managerRole.getId());
        if(in == null){
            throw new ParameterException("角色不存在");
        }
        int result = update(managerRoleMapper, managerRole);
        return managerRole;
    }

    public boolean delete(Number id){
        ManagerRole in = one(managerRoleMapper, id);
        if(in == null){
            throw new ParameterException("角色不存在");
        }
        int result = delete(managerRoleMapper, id);
        return result > 0;
    }

    public ManagerRole get(Number id){
        ManagerRole in = one(managerRoleMapper, id);

        if(in == null){
            throw new ParameterException("角色不存在");
        }
        return in;
    }

    public Page<ManagerRole> select(int page, int pageSize){
        return select(managerRoleMapper, page, pageSize);
    }

    public Page<ManagerRole> select(int page, int pageSize, String order, DirectionStatus direction){
        Example example = new Example(ManagerRole.class);
        if(order.isEmpty()) order = "id";
        switch(direction){
            case ASC:
                example.orderBy(order).asc();
                break;
            case DESC:
            default:
                example.orderBy(order).desc();
        }
        return select(managerRoleMapper, example, page, pageSize);
    }

    public List<ManagerRole> select(Collection ids){
        return select(managerRoleMapper, ids);
    }
}
