package cn.hnthyy.thmz.mapper.thmz;

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

public interface WindowsMapper {
    /**
     * 新增窗口记录
     *
     * @param windows
     * @return
     */
    @Insert("INSERT INTO t_windows(dept_no,windows_no,print_index,zy_print_index,cg_print_index,user_id_code,ip_address, create_date,update_date) VALUES " +
            "(#{deptNo,jdbcType=VARCHAR},#{windowsNo,jdbcType=INTEGER},#{printIndex,jdbcType=INTEGER},#{zyPrintIndex,jdbcType=INTEGER},#{cgPrintIndex,jdbcType=INTEGER},#{userIdCode,jdbcType=VARCHAR},#{ipAddress,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, #{updateDate,jdbcType=TIMESTAMP})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertUser(Windows windows);

    /**
     * 查询用户的最后一个在使用中的窗口号
     *
     * @param userIdCode
     * @return
     */
    @Select({"<script>",
            "select id,dept_no,windows_no,print_index,zy_print_index,cg_print_index,user_id_code,ip_address, create_date,update_date from t_windows where user_id_code= #{userIdCode,jdbcType=VARCHAR} " +
            "<when test='ipAddress!=null'>",
            " or ip_address= #{ipAddress,jdbcType=VARCHAR} ",
            "</when>",
            "order by create_date desc limit 1",
            "</script>"})
    Windows selectLastWindowsByUserIdCode(@Param("userIdCode") String userIdCode, @Param("ipAddress") String ipAddress);

    /**
     * 更新设置
     *
     * @param windows
     * @return
     */
    @Update({"<script>",
            "update t_windows",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id=#{id,jdbcType=BIGINT}' >",
            "<when test='deptNo!=null'>",
            "dept_no=#{deptNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='windowsNo!=null'>",
            ",windows_no=#{windowsNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='printIndex!=null'>",
            ",print_index=#{printIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='zyPrintIndex!=null'>",
            ",zy_print_index=#{zyPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='cgPrintIndex!=null'>",
            ",cg_print_index=#{cgPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='updateDate!=null'>",
            ",update_date=#{updateDate,jdbcType=TIMESTAMP}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateWindows(Windows windows);
}
