package cn.common.repository.repository;
import cn.common.common.repository.BaseRepository;
import cn.common.common.config.RedisEntityCacheHandler;
import cn.common.repository.entity.PlatformDict;
import cn.common.repository.result.BusinessDictResult;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author Singer create by singer email:singer-coder@qq.com
 * @packageName cn.singer.repository.entity
 * @Description: 业务字典Repository
 * @date 2019-08-12
 */
@Mapper
@CacheNamespace(implementation = RedisEntityCacheHandler.class,flushInterval = 60000)
public interface PlatformDictRepository extends BaseRepository<PlatformDict> {


    /**
     * 根据系统业务字典主键ID系统字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-29
     * @param deleteStatus 是否删除的状态
     * @param platformDictIdList 系统业务字典主键ID集合
     * @return
     */
    @Update("<script>"+
            "UPDATE platform_dict SET delete_status = #{deleteStatus} WHERE 1=1 and platform_dict_id in " +
            " <foreach item='item' index='index' collection='platformDictIdList' open='(' separator=',' close=')'>   " +
            "     #{item}  " +
            " </foreach> " +
            "</script>"
    )
    void updateDeleteStatusByIdListIn(@Param("deleteStatus") Integer deleteStatus, @Param("platformDictIdList") List<String> platformDictIdList);

    /**
     * 根据业务主键ID查询字典的值
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-13
     * @param deleteStatus 是否删除 1 删除 2 未删除
     * @param platformDictId 业务主键ID
     * @return List
     */
    @Select( "<script>" +
            " select dict.dict_value as dictValue " +
            " from platform_dict dict  " +
            " where 1=1 and platform_dict_id = #{platformDictId} " +
            " and dict.delete_status = #{deleteStatus} " +
            "</script>"
    )
    String queryValueByMainId(@Param("deleteStatus") Integer deleteStatus, @Param("platformDictId") String platformDictId);

    /**
     * 根据主键查询系统业务字典
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/4/22
     * @param deleteStatus  是否删除 1删除 2未删除
     * @return
     */
    @Select( "<script>" +
            " select " +
            " item.platform_dict_id AS platformDictId, " +
            " item.dict_type AS dictType,item.dict_key AS dictKey," +
            " item.dict_value AS dictValue," +
            " item.sort_index AS sortIndex, " +
            " item.dict_remark AS dictRemark " +
            " FROM platform_dict item WHERE 1=1 " +
            " and item.delete_status = #{deleteStatus} " +
            "</script>"
    )
    @Results(
            {
                    @Result(column = "platformDictId", property = "platformDictId", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictType", property = "dictType", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictKey", property = "dictKey", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictValue", property = "dictValue", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "sortIndex", property = "sortIndex", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictRemark", property = "dictRemark", jdbcType = JdbcType.VARCHAR),

            }
    )
    @Options()
    List<BusinessDictResult> queryAllDict(@Param("deleteStatus") Integer deleteStatus);

    /**
      * 根据主键查询系统业务字典
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/4/22
      * @param platformDictId 业务主键ID
      * @param deleteStatus  是否删除 1删除 2未删除
      * @return
      */
    @Select( "<script>" +
            " select " +
            " item.platform_dict_id AS platformDictId, " +
            " item.dict_type AS dictType," +
            " item.dict_key AS dictKey," +
            " item.dict_value AS dictValue," +
            " item.sort_index AS sortIndex, " +
            " item.dict_remark AS dictRemark " +
            " from platform_dict item  " +
            " where 1=1 and item.platform_dict_id = #{platformDictId} " +
            " and item.delete_status = #{deleteStatus} " +
            "</script>"
    )
    @Results(
            {
                    @Result(column = "platformDictId", property = "platformDictId", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictType", property = "dictType", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictKey", property = "dictKey", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictValue", property = "dictValue", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "sortIndex", property = "sortIndex", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictRemark", property = "dictRemark", jdbcType = JdbcType.VARCHAR),

            }
    )
    @Options()
    BusinessDictResult queryByMainId(@Param("deleteStatus") Integer deleteStatus,
                                     @Param("platformDictId") String platformDictId);


    /**
     * 根据主键ID集合查询系统业务字典
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/4/22
     * @param mainIdList 业务主键ID集合
     * @param deleteStatus  是否删除 1删除 2未删除
     * @return
     */
    @Select( "<script>" +
            " select " +
            " item.platform_dict_id AS platformDictId, " +
            " item.dict_type AS dictType,item.dict_key AS dictKey," +
            " item.dict_value AS dictValue," +
            " item.sort_index AS sortIndex, " +
            " item.dict_remark AS dictRemark " +
            " from platform_dict item  " +
            " where 1=1 and item.platform_dict_id IN " +
            " <foreach item='item' index='index' collection='mainIdList' open='(' separator=',' close=')'>   " +
            "     #{item}  " +
            " </foreach> " +
            " and item.delete_status = #{deleteStatus} " +
            "</script>"
    )
    @Results(
            {
                    @Result(column = "platformDictId", property = "platformDictId", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictType", property = "dictType", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictKey", property = "dictKey", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictValue", property = "dictValue", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "sortIndex", property = "sortIndex", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "dictRemark", property = "dictRemark", jdbcType = JdbcType.VARCHAR),

            }
    )
    @Options()
    List<BusinessDictResult> queryByMainIdListIn(@Param("deleteStatus") Integer deleteStatus,
                                     @Param("mainIdList") List<String> mainIdList);


    /**
     * 根据字典值查询系统业务字典业务主键ID
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/4/22
     * @param dctValue 字典值
     * @param deleteStatus  是否删除 1删除 2未删除
     * @return
     */
    @Select( "<script>" +
            " select " +
            " item.platform_dict_id AS platformDictId " +
            " from platform_dict item  " +
            " where 1=1 and item.dict_value LIKE CONCAT('%',#{dctValue},'%') " +
            " and item.delete_status = #{deleteStatus} " +
            "</script>"
    )
    @Options()
    List<String> queryMainIdValueLike(@Param("deleteStatus") Integer deleteStatus,
                                                 @Param("dctValue") String dctValue);


}
