package thyyxxk.webserver.dao.his.dictionary;

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.dictionary.HisWjwMatchEntity;

import java.util.List;

@Mapper
public interface HisWjwMatchDao {

    @Select("<script>" +
            "select code,name,wjw_code,isnull(py_code,'') as pyCode from ${table} " +
            "<if test=\"table == 'zd_unit_code' \">" +
            "where code like '1%' and isnull(del_flag,0)!=1 " +
            "</if>" +
            "</script>")
    List<HisWjwMatchEntity> selectHisData(@Param("table") String table);

    @Select("select code,name,isnull(py_code,'') as pyCode from ${table}")
    List<HisWjwMatchEntity> selectWjwData(@Param("table") String table);

    @Select("select rtrim(code) as code,rtrim(name) as name, " +
            "isnull(rtrim(py_code),'') as pyCode, " +
            "rtrim(yb_code) as wjwCode,rtrim(yb_name) as wjwName from ${table}")
    List<HisWjwMatchEntity> selectYbData(@Param("table") String table);

    @Select("select rtrim(code) as code, rtrim(name) as name, " +
            "isnull(rtrim(py_code),'') as pyCode from ${table} where yb_code is null")
    List<HisWjwMatchEntity> selectNationTempData(@Param("table") String table);

    @Select("select ${codeColumn} as code, ${nameColumn} as name, " +
            "isnull(py_code,'') as pyCode from ${table} where " +
            "${codeColumn} like #{key} ")
    List<HisWjwMatchEntity> selectMedInsData(@Param("table") String table,
                                             @Param("codeColumn") String codeColumn,
                                             @Param("nameColumn") String nameColumn,
                                             @Param("key") String key);

    @Select("select rtrim(code) as code,name,charge_code as wjwCode,charge_name as wjwName, " +
            "isnull(rtrim(py_code),'') as pyCode from zd_icd9_cm3 where del_flag=0")
    List<HisWjwMatchEntity> selectSurgeries();

    @Select("select code,name,isnull(py_code,'') as pyCode from zd_charge_item where del_flag=0")
    List<HisWjwMatchEntity> selectChargeCodes();

    @Update("<script>" +
            "update ${table} set ${targetCodeColumn}=#{wjwCode} " +
            "<if test=\"targetNameColumn != null and targetNameColumn != '' \">" +
            ", ${targetNameColumn}=#{wjwName} " +
            "</if>" +
            "where code=#{code} " +
            "</script>")
    void executeMatchAction(HisWjwMatchEntity entity);

    @Update("update zd_icd9_cm3 set match_datetime=getdate(),match_oper=#{oper} where code=#{code}")
    void updateDateAndOper(String code, String oper);
}
