package cn.hnthyy.thmz.mapper.thmz;

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

public interface DispensingWindowsMapper {
    /**
     * 新增窗口记录
     *
     * @param dispensingWindows
     * @return
     */
    @Insert("INSERT INTO t_dispensing_windows(group_no,windows_no,mz_cf_print_index,ek_cf_print_index,jz_cf_print_index,zsd_print_index,bq_print_index,fsk_print_index,user_id_code,ip_address, create_date,update_date) VALUES " +
            "(#{groupNo,jdbcType=VARCHAR},#{windowsNo,jdbcType=INTEGER},#{mzCfPrintIndex,jdbcType=INTEGER},#{ekCfPrintIndex,jdbcType=INTEGER},#{jzCfPrintIndex,jdbcType=INTEGER}," +
            "#{zsdPrintIndex,jdbcType=INTEGER},#{bqPrintIndex,jdbcType=INTEGER},#{fskPrintIndex,jdbcType=INTEGER},#{userIdCode,jdbcType=VARCHAR},#{ipAddress,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, #{updateDate,jdbcType=TIMESTAMP})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertDispensingUser(DispensingWindows dispensingWindows);

    /**
     * 查询用户的最后一个在使用中的窗口号
     *
     * @param userIdCode
     * @return
     */
    @Select({"<script>",
            "select id,group_no,windows_no,mz_cf_print_index,ek_cf_print_index,jz_cf_print_index,zsd_print_index,bq_print_index,fsk_print_index," +
                    "user_id_code,ip_address, create_date,update_date from t_dispensing_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>"})
    DispensingWindows selectLastDispensingWindowsByUserIdCode(@Param("userIdCode") String userIdCode, @Param("ipAddress") String ipAddress);

    /**
     * 更新设置
     *
     * @param dispensingWindows
     * @return
     */
    @Update({"<script>",
            "update t_dispensing_windows",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id=#{id,jdbcType=BIGINT}' >",
            "<when test='groupNo!=null'>",
            "group_no=#{groupNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='windowsNo!=null'>",
            ",windows_no=#{windowsNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzCfPrintIndex!=null'>",
            ",mz_cf_print_index=#{mzCfPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='ekCfPrintIndex!=null'>",
            ",ek_cf_print_index=#{ekCfPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='jzCfPrintIndex!=null'>",
            ",jz_cf_print_index=#{jzCfPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='zsdPrintIndex!=null'>",
            ",zsd_print_index=#{zsdPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='bqPrintIndex!=null'>",
            ",bq_print_index=#{bqPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='fskPrintIndex!=null'>",
            ",fsk_print_index=#{fskPrintIndex,jdbcType=INTEGER}",
            "</when>",
            "<when test='updateDate!=null'>",
            ",update_date=#{updateDate,jdbcType=TIMESTAMP}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateDispensingWindows(DispensingWindows dispensingWindows);
}
