package com.rongwei.bscommon.sys.dao;

import com.rongwei.rwadmincommon.system.domain.SysDictDo;
import com.rongwei.rwadmincommon.system.vo.SysOrganizationVo;
import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

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

/**
 * CommonDao class
 *
 * @author XH   LXD
 * @date 2021/05/27
 */
@Mapper
@Repository("commonMapper")
public interface CommonDao {

    @Select("${sqlStr}")
    List<Map<String, Object>> commonQuery(@Param("sqlStr") String sqlStr);

    /**
     * 根据id查询文件路径
     * @param id
     * @return
     */
    Map<String, Object> getFileUrlById(@Param("id") String id);

    /**
     * 获取字典值
     * @param name
     * @param dictType
     * @return
     */
    @Select("select * from sys_dict where NAME = #{name} and DELETED = '0' and DICTTYPE = #{dictType} limit 1")
    SysDictDo getDictValueByNameAndType(@Param("name") String name,@Param("dictType") String dictType);

    /**
     * 获取字典名称
     * @param value
     * @param dictType
     * @return
     */
    @Select("select * from sys_dict where VALUE = #{value} and DELETED = '0' and DICTTYPE = #{dictType} limit 1")
    SysDictDo getDictNameByValueAndType(@Param("value") String value,@Param("dictType") String dictType);

    /**
     * 获取字典数
     * @param name
     * @param dictType
     * @return
     */
    Integer getDictByNameAndDictType(@Param("name") String name,@Param("dictType") String dictType);

    /**
     * 获取字典数
     * @param value
     * @param dictType
     * @return
     */
    Integer getDictByValueAndDictType(@Param("value") String value,@Param("dictType") String dictType);


    /**
     * 根据工厂名称查询id
     * @param factoryName
     * @return
     */
    String getIdByFactoryName(@Param("factoryName") String factoryName);

    /**
     * 根据工厂id和部门名称查询部门id
     * @param dept
     * @param factoryId
     * @return
     */
    String getIdByFactoryIdAndDept(@Param("factoryId") String factoryId,@Param("dept") String dept);

    /**
     * 查询所有工厂名称
     * @return
     */
    List<SysOrganizationVo> getAllFactoryNameAndId();

    /**
     * 查询附件表数据
     * @param id
     * @return
     */
    @Select("select * from sys_file_item where ID = #{id}")
    SysFileItemDo getFileItemByID(@Param("id") String id);

    /**
     * 插入附件表数据
     * @param model
     */
    void insertFile(@Param("model") SysFileItemDo model);

    /**
     * 获取对应角色下指定工厂或者指定车间的人
     * @param
     * @return
     */
    @Select("SELECT su.ID\n" +
            "FROM sys_role sr \n" +
            "LEFT JOIN sys_user_role sur ON sr.ID = sur.ROLEID and sur.DELETED = 0 \n" +
            "LEFT JOIN sys_user su ON sur.USERID = su.ID and su.DELETED = 0 \n" +
            "LEFT JOIN sys_user_org suo ON suo.USERID = su.ID AND suo.DELETED = 0 \n" +
            "LEFT JOIN sys_organization so ON suo.ORGID = so.ID AND so.DELETED = 0 \n" +
            "WHERE sr.CODE in(${roles}) AND ${whr}")
    List<String> getRoleUserIds(@Param("roles") String roles,@Param("whr") String whr);

}
