package thyyxxk.webserver.dao.his.medicalinsurance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.querydata.AutoUploadBill;

import java.util.Date;
import java.util.List;

/**
 * @Description:上传日志
 * @Author:lihong
 * @Date: 2023/1/4
 */
@Mapper
public interface AutoUploadBillDao extends BaseMapper<AutoUploadBill> {
    @Insert("<script>" +
            " insert into t_auto_upload_bill (pat_no, times, ledger_sn, log_text, created_date, upload_date, type_flag, flag, setl_time,upload_code,upload_name)" +
            " values " +
            " <foreach collection='list' item='item'  separator=','>" +
            " (#{item.patNo},#{item.times},#{item.ledgerSn},#{item.logText},#{item.createdDate},#{item.uploadDate},#{item.typeFlag},#{item.flag},#{item.setlTime},#{item.uploadCode},#{item.uploadName}) "+
            " </foreach>" +
            " </script>")
    void batchSaveData(List<AutoUploadBill> list);

    @Delete("<script>" +
            " delete from t_auto_upload_bill where pat_no + cast(times as varchar) + cast(ledger_sn as varchar) " +
            " in (" +
            " <foreach collection='visitIds' item='item'  separator=','>" +
            " #{item}"+
            " </foreach>" +
            " )" +
            "</script>")
    void deleteVisitId(List<String> visitIds);
    @Select("select min(created_date) created_date from t_auto_upload_bill " +
            " where flag='1' and  pat_no =#{patNo} and times =#{times} and ledger_sn =#{ledgerSn}"
            )
    Date getLastUpdateTime(@Param("patNo") String patNo, @Param("times")Integer times, @Param("ledgerSn")Integer ledgerSn);
    @Select("select top ${pageSize} *  from ("+
            " select     row_number() over (order by t1.created_date desc) as rowNumber,  " +
            "  t1.id, t1.pat_no, t1.times, t1.ledger_sn, t1.log_text, t1.created_date, t1.upload_date, t1.type_flag, t1.flag, t1.setl_time, t1.upload_code, t1.upload_name ,t4.insuplc_admdvs,t4.psn_name,t4.med_type,t4.clr_optins,clr_optins_name = (select name from t_region where code=t4.clr_optins), " +
            "    out_dept_name=(select rtrim(name) from zd_unit_code where code = isnull(t2.zk_ward, t2.small_dept))," +
            "                  rtrim(isnull(t2.zk_ward, t2.small_dept)) as out_dept " +
            " from t_auto_upload_bill t1" +
            " join t_si_setlinfo t4 on t1.pat_no = t4.pat_no and t1.times = t4.times and t1.ledger_sn=t4.ledger_sn and t4.revoked =0 " +
            "   ${condition} "+
            "         left join zy_inactpatient t2 on t1.pat_no = t2.inpatient_no and t1.times = t2.admiss_times" +
            " where  ${ew.sqlSegment}"+
            " )temp where rowNumber > ${pageSize} * (${currentPage} - 1)  order by created_date desc "
            )
   List<AutoUploadBill> listPage(@Param("ew")QueryWrapper<AutoUploadBill> query,
                                 @Param("currentPage") long currentPage,
                                 @Param("pageSize") long pageSize,
                                @Param("condition") String condition
    );

    @Select("select count(1)  from t_auto_upload_bill t1  " +
            " join t_si_setlinfo t4 on t1.pat_no = t4.pat_no and t1.times = t4.times and t1.ledger_sn=t4.ledger_sn and t4.revoked =0 " +
            " ${condition} "+
            "                 left join zy_inactpatient t2 on t1.pat_no = t2.inpatient_no and t1.times = t2.admiss_times " +
            "   where  ${ew.sqlSegment}")
    Integer pageTotal(@Param("ew")QueryWrapper<AutoUploadBill> query, @Param("condition") String condition);
}
