package thyyxxk.webserver.dao.his.examinations;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.examinations.mixlabel.MixLabel;
import thyyxxk.webserver.entity.examinations.mixlabel.MixLabelRange;

import java.util.List;

@Mapper
public interface MixLabelPrintDao {

    @Update("create table dbo.${table} (label varchar(32) not null constraint ${table}_pk primary key, " +
            "status tinyint default 0 not null, reprint tinyint default 0 not null, " +
            "print_staff varchar(12), print_datetime datetime, " +
            "reprint_staff varchar(12), reprint_datetime datetime);" +
            "exec sp_addextendedproperty 'MS_Description', '核酸混采标签打印', 'SCHEMA', 'dbo', 'TABLE', '${table}';" +
            "exec sp_addextendedproperty 'MS_Description', '标签编号', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'label';" +
            "exec sp_addextendedproperty 'MS_Description', '状态：0-未打印，1-已打印', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'status';" +
            "exec sp_addextendedproperty 'MS_Description', '是否重复打印过：0-否，1-是', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'reprint';" +
            "exec sp_addextendedproperty 'MS_Description', '打印操作人', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'print_staff';" +
            "exec sp_addextendedproperty 'MS_Description', '打印时间', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'print_datetime';" +
            "exec sp_addextendedproperty 'MS_Description', '重复打印操作人', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'reprint_staff';" +
            "exec sp_addextendedproperty 'MS_Description', '重复打印操作时间', 'SCHEMA', 'dbo', 'TABLE', '${table}', 'COLUMN', 'reprint_datetime';")
    void createTable(@Param("table") String table);

    @Insert("<script>" +
            "insert into ${table} (label,status,reprint) values " +
            "<foreach collection='list' item='label' open='' separator=',' close=''>" +
            "(#{label}, 0, 0)" +
            "</foreach>" +
            "</script>")
    void insertNewLabels(@Param("table") String table, @Param("list") List<String> list);

    @Insert("insert into t_mix_label_ranges(table_name, min_label, max_label, prefix, slice_index, end_number) values " +
            "(#{tableName},#{minLabel},#{maxLabel},#{prefix},#{sliceIndex},#{endNumber})")
    void insertNewLabelRange(MixLabelRange labelRange);

    @Select("select * from t_mix_label_ranges")
    List<MixLabelRange> selectLabelRanges();

    @Select("select top 1 label from ${table} where status=0 order by label")
    String selectMinUnprintedLabel(@Param("table") String table);

    @Select("select * from ${table} where label=#{label}")
    MixLabel selectSingleLabel(@Param("table") String table, @Param("label") String label);

    @Update("update ${tableName} set status=1,print_staff=#{printStaff},print_datetime=#{printDatetime} where label=#{label}")
    void updatePrinted(MixLabel printer);

    @Update("update ${tableName} set reprint=1,reprint_staff=#{reprintStaff}, " +
            "reprint_datetime=#{reprintDatetime} where label=#{label}")
    void updateReprinted(MixLabel printer);

    @Select("select count(1) from ${table} where status=0")
    Integer selectNotPrintedCount(@Param("table") String table);

    @Update("update t_mix_label_ranges set finish_flag=1 where table_name=#{tableName}")
    void updateAllPrinted(@Param("table") String table);
}
