package com.yihu.jw.dict.service;

import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.dict.dao.BaseDictJobTitleDao;
import com.yihu.jw.entity.base.dict.DictJobTitleDO;
import com.yihu.jw.enums.BaseSystemDictEnum;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BaseDictJobTitleService extends BaseJpaService<DictJobTitleDO, BaseDictJobTitleDao> {

    @Autowired
    private BaseDictJobTitleDao baseDictJobTitleDao;

    /**
     * 查询某一租户下的职称字典信息，如果saadId为空表示当前用户角色为超级管理员，超级管理员可以看到所有数据
     * @param saasId
     * @return
     */
    public JSONObject queryAll(String saasId, Pageable pageable){
        JSONObject jsonObject = new JSONObject();
        long count = 0;
        List<Map<String,Object>> list = new ArrayList<>();
        if(StringUtils.isEmpty(saasId)){
            list = baseDictJobTitleDao.findCodeAndName(pageable);
            count = baseDictJobTitleDao.count();
        }else{
            list = baseDictJobTitleDao.findCodeAndNameBySaasId(saasId,pageable);
            count = baseDictJobTitleDao.countBySaasId(saasId);
        }
        jsonObject.put("count",count);
        jsonObject.put(BaseSystemDictEnum.JobTitleDict.toString(),list);
        return jsonObject;
    }

    public void deleteById(Integer id){
        baseDictJobTitleDao.delete(id);
    }


    public List<DictJobTitleDO> getBySaasId(String saasId){
        return  baseDictJobTitleDao.findBySaasId(saasId);
    }

}
