package thyyxxk.webserver.dao.his.reports;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.inpatient.patient.Patient;

import java.util.List;

@Mapper
public interface PatientDistributionAddressDao {
    // province_code = (select name from t_region where code = province_code),
    // city_code     = (select name from t_region where code = city_code),
    // district_code = (select name from t_region where code = district_code),
    // a_patient_mi 省市区

    @Select("select a.inpatient_no, " +
            "       home_street, " +
            "       latitude, " +
            "       longitude," +
            "       a.name " +
            "from a_patient_mi a," +
            "     ${tableName} b " +
            "where addr_transed_flag = 0 and a.inpatient_no = b.inpatient_no " +
            "and ${ew.sqlSegment} ")
    List<Patient> inpatientAddress(@Param("tableName") String tableName,
                                   @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);


    @Select("select latitude,\n" +
            "       longitude,\n" +
            "       name,\n" +
            "       rtrim(patient_id) as inpatient_no\n" +
            "from mz_patient_mi\n" +
            "where addr_transed_flag = 0\n" +
            "  and lv_date >= #{startTime}\n" +
            "  and lv_date <= #{endTime} ")
    List<Patient> mzAddress(@Param("startTime") String startTime,
                            @Param("endTime") String endTime);


    @Select("select top 5000 inpatient_no," +
            "                home_street   " +
            "from a_patient_mi\n" +
            "where isnull(addr_transed_flag, 3) <> 1\n" +
            "  and LEFT(inpatient_no, 1) NOT IN ('S', 'T', 'J')\n" +
            "order by inpatient_no desc")
    List<Patient> timedTaskPatientData();


    @Select("select top 5000 patient_id as inpatient_no,\n" +
            "                adress     as home_street\n" +
            "from mz_patient_mi\n" +
            "where nullif(adress, '') is not null\n" +
            "order by patient_id desc")
    List<Patient> mzTimedTaskPatientData();


    @Update("<script>" +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\";\">" +
            "update ${tableName} " +
            "set latitude = #{item.latitude,jdbcType=REAL}," +
            "    longitude = #{item.longitude,jdbcType=REAL}, " +
            "    addr_transed_flag = #{item.addrTransedFlag,jdbcType=INTEGER}  " +
            "where ${patNo} = #{item.inpatientNo} " +
            "</foreach>" +
            "</script>")
    void updatePatientAddress(@Param("list") List<Patient> patients,
                              @Param("tableName") String tableName,
                              @Param("patNo") String panNo);

}
