package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.TCommonParams;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TCommonParamsMapper {
    /**
     * 新增通用参数
     *
     * @param tCommonParams
     * @return
     */
    @Insert("INSERT INTO t_common_params(params_name,py_code, wb_code,params_type,create_id_code,create_time,params_status,update_id_code,update_time,used_times,params_code,params_content) VALUES " +
            "(#{paramsName}, #{pyCode},#{wbCode},#{paramsType},#{createIdCode},#{createTime}," +
            "#{paramsStatus}, #{updateIdCode}, #{updateTime}, #{usedTimes},#{paramsCode},#{paramsContent})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertTCommonParams(TCommonParams tCommonParams);


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Select("select id,params_name,py_code, wb_code,params_type,create_id_code,create_time,params_status,update_id_code,update_time,used_times,params_code,params_content from t_common_params where id=#{id}")
    TCommonParams selectById(@Param("id") Long id);

    /**
     * 根据paramsCode查询
     *
     * @param paramsCode
     * @return
     */
    @Select("select id,params_name,py_code, wb_code,params_type,create_id_code,create_time,params_status,update_id_code,update_time,used_times,params_code,params_content from t_common_params where params_code=#{paramsCode}")
    TCommonParams selectByParamsCode(@Param("paramsCode") String paramsCode);

    /**
     * 根据参数名和参数类型以及创建人id查询
     *
     * @param paramsName
     * @param paramsType
     * @param createIdCode
     * @return
     */
    @Select("select id,params_name,py_code, wb_code,params_type,create_id_code,create_time,params_status,update_id_code,update_time,used_times,params_code,params_content from t_common_params where params_name=#{paramsName} and params_type=#{paramsType} and (create_id_code=#{createIdCode} or create_id_code='00000') and params_status=0 ")
    TCommonParams selectByParamsName(@Param("paramsName") String paramsName, @Param("paramsType") Integer paramsType, @Param("createIdCode") String createIdCode);

    /**
     * 根据参数类型查询参数列表
     * @param paramsType 参数类型
     * @param commonParams 通用参数查询参数
     * @param pageSize 返回数据条数
     * @param createIdCode 创建人id编码
     * @return
     */
    @Select({"<script>",
            "select id,params_name,py_code, wb_code,params_type,create_id_code,create_time,params_status,update_id_code,update_time,used_times,params_code,params_content from t_common_params where params_type=#{paramsType}  and params_status=0 " ,
            "<when test='createIdCode!=null'>",
            " and (create_id_code=#{createIdCode} or create_id_code='00000') ",
            "</when>",
            "<when test='commonParams!=null'>",
            " and (params_name like #{commonParams} or py_code like #{commonParams} or wb_code like #{commonParams} or params_code like #{commonParams})",
            "</when>",
                    " order by used_times desc",
            "<when test='pageSize!=null'>",
            " limit ${pageSize}",
            "</when>",
            "</script>"})
    List<TCommonParams> selectTCommonParamsByParamsType(@Param("paramsType") Integer paramsType, @Param("commonParams") String commonParams, @Param("pageSize") Integer pageSize, @Param("createIdCode") String createIdCode);

    /**
     * 根据参数编码集查询参数
     *
     * @param paramsCodes
     * @return
     */
    @Select({"<script>",
            "select id,params_name,py_code, wb_code,params_type,create_id_code,create_time,params_status,update_id_code,update_time,used_times,params_code,params_content from t_common_params where params_code in " +
            "<foreach item='item' index='index' collection='paramsCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<TCommonParams> selectByParamsCodes(@Param("paramsCodes") List<String> paramsCodes);

    /**
     * 更新用户信息
     *
     * @param tCommonParams
     * @return
     */
    @Update({"<script>",
            "update t_common_params",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id = #{id} '>",
            "<when test='paramsStatus!=null'>",
            "params_status =#{paramsStatus}",
            "</when>",
            "<when test='usedTimes!=null'>",
            "used_times =#{usedTimes}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateTCommonParams(TCommonParams tCommonParams);
}
