package com.tuoheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.common.common.BaseQuery;
import com.tuoheng.common.common.BaseServiceImpl;
import com.tuoheng.system.entity.Dept;
import com.tuoheng.system.mapper.DeptMapper;
import com.tuoheng.system.query.DeptQuery;
import com.tuoheng.system.service.IDeptService;
import com.tuoheng.system.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author 拓恒
 * @since 2020-11-03
 */
@Service
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private ITenantService tenantService;
    /**
     * 获取部门列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        DeptQuery deptQuery = (DeptQuery) query;
        // 查询条件
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        // 部门名称
        if (!StringUtils.isEmpty(deptQuery.getName())) {
            queryWrapper.like("name", deptQuery.getName());
        }
        queryWrapper.eq("tenant_id", tenantService.getCurrentTenantId());
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");

        // 查询分页数据
        List<Dept> deptList = deptMapper.selectList(queryWrapper);
        return JsonResult.success(deptList);
    }

    /**
     * 获取部门列表
     *
     * @return
     */
    @Override
    public JsonResult getDeptList() {
        // 查询条件
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mark", 1);
        queryWrapper.eq("tenant_id", tenantService.getCurrentTenantId());
        queryWrapper.orderByAsc("sort");

        // 查询分页数据
        List<Dept> deptList = deptMapper.selectList(queryWrapper);
        return JsonResult.success(deptList);
    }
}
