package thyyxxk.webserver.dao.his.dictionary;

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 org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface BedDeptDictDao {

    @Select("  select a.ward_code as wardCode,   " +
            "         a.dept_code as deptCode,   " +
            "         a.openbed_num as openbedNum,   " +
            "         a.borrowbed_num as borrowBedNum,   " +
            "         a.lentbed_num as lentbedNum,   " +
            "         rtrim(a.ward_name) as wardName,   " +
            "         rtrim(a.dept_name) as deptName,  " +
            "         rtrim(a.ward_py_code) as wardPyCode,   " +
            "         rtrim(a.ward_d_code) as wardDCode,   " +
            "         rtrim(a.dept_py_code) as deptPyCode,   " +
            "         rtrim(a.dept_d_code) as deptDCode,   " +
            "         a.start_day_time_kf as startDayTimeKf,   " +
            "         a.ward_only as wardOnly,   " +
            "         a.deposit_limit as depositLimit,   " +
            "         a.owe_limit as oweLimit  " +
            "    from zy_adtward as a   " +
            "   where (a.dept_py_code like '${text}%' or a.ward_py_code like '${text}%') " +
            "      or (a.dept_d_code like '${text}%' or a.ward_d_code like '${text}%') " +
            "      or (a.dept_code like '${text}%' or a.ward_code like '${text}%') " +
            "      or (a.dept_name like '${text}%' or a.ward_name like '${text}%') " +
            "order by a.dept_code,a.ward_code  ")
    List<Map<String, Object>> selectDeptInfoForBed(@Param("text") String text);

    @Select("  select b.ward_code as wardCode,   " +
            "         b.bed_status as bedStatus, " +
            "         case when b.bed_status = '2' then '占用' else '空闲' end as bedStatusName, " +
            "         b.bed_no as bedNo,   " +
            "         b.room_no as roomNo,   " +
            "         b.dept_code as deptCode,   " +
            "         b.room_rate1 as roomRate1,   " +
            "         b.room_rate2 as roomRate2,   " +
            "         b.eff_date1 as effDate1,   " +
            "         b.eff_date2 as effDate2,   " +
            "         b.heating_fee as heatingFee,   " +
            "         b.aircondition_fee as airConditionFee,   " +
            "         b.heatcold_flag as heatColdFlag,  " +
            "         b.inpatient_no as inpatientNo,   " +
            "         b.admiss_times as admissTimes,   " +
            "         b.sex,   " +
            "         b.percent2,   " +
            "         b.percent3,   " +
            "         b.percent4,   " +
            "         b.percent5,   " +
            "         b.type,   " +
            "         b.percent1,   " +
            "         b.special_charge as specialCharge,   " +
            "         b.remark,   " +
            "         b.zy_serial_no as zySerialNo,  " +
            "         b.charge_code_bed as chargeCodeBed,  " +
            "         rtrim(t.name) as chargeCodeBedName,  " +
            "         b.charge_code_air as chargeCodeAir,  " +
            "         rtrim(s.name) as chargeCodeAirName " +
            "    from zy_bed_mi as b left join zd_charge_item as t " +
            "      on b.charge_code_bed = t.code " +
            "    left join zd_charge_item as s  " +
            "      on b.charge_code_air = s.code " +
            "   where b.ward_code = #{wardCode} " +
            "order by b.bed_no ")
    List<Map<String, Object>> selectBedInfoForDept(@Param("wardCode") String wardCode);

    @Select("  select a.code as value,   " +
            "         a.exec_unit as execUnit,   " +
            "         a.name as label,   " +
            "         a.charge_amount as price,   " +
            "         a.bill_item_zy as billItemCode,   " +
            "         a.py_code as pyCode,   " +
            "         a.d_code as dCode,   " +
            "         isnull(a.suppress_flag,'0') as suppressFlag,   " +
            "         a.bill_item_out as billItemOut,   " +
            "         a.n_code as nCode,   " +
            "         isnull(a.self_flag,'0') as selfFlag,   " +
            "         isnull(a.separate_flag,'0') as separateFlag ," +
            "         a.charge_unit as spec, " +
            "         b.name as execName,   " +
            "         '00' as serial, " +
            "         '00' as groupNo " +
            "    from zd_charge_item a left join zd_unit_code b on a.exec_unit = b.code " +
            "   where isnull(a.del_flag,'') <> '1'  " +
            "     and isnull(a.in_out_flag,'') <> '1'  " +
            "     and (a.py_code like '${text}%'  " +
            "      or a.d_code like '${text}%'   " +
            "      or a.code like '${text}%' " +
            "      or a.name like '${text}%' " +
            "      or a.n_code like '${text}%')  " +
            "order by a.name ")
    List<Map<String, Object>> selectAirItemInfo(@Param("text") String text);

    @Update("<script>" +
            "<foreach item='item' collection='list' >" +
            " update zy_adtward set openbed_num = #{item.openbedNum} " +
            " where ward_code = #{item.deptCode} and dept_code = #{item.deptCode} " +
            "</foreach>" +
            "</script>")
    int updateDeptInfoForBed(@Param("list") List<Map<String, Object>> list);

    @Update("<script>" +
            "<foreach item='item' collection='list' >" +
            " update zy_bed_mi set type = #{item.type}, bed_no = #{item.bedNo}, room_no = #{item.roomNo}, sex = #{item.sex}, " +
            "   room_rate1 = #{item.roomRate1}, charge_code_bed = #{item.chargeCodeBed}, heating_fee = #{item.heatingFee}, " +
            "   aircondition_fee = #{item.airConditionFee}, charge_code_air = #{item.chargeCodeAir}, special_charge = #{item.specialCharge}, " +
            "   room_rate2 = #{item.roomRate2}, heatcold_flag = #{item.heatColdFlag} " +
            " where ward_code = #{item.deptCode} and bed_no = #{item.bedNo} and dept_code = #{item.deptCode} " +
            "</foreach>" +
            "</script>")
    int updateBedInfoForDept(@Param("list") List<Map<String, Object>> list);

    @Select(" select count(*) as num from zy_bed_mi where ward_code = #{wardCode} and dept_code = #{deptCode} and bed_no = #{bedNo} ")
    int selectBedInfoForDeptAndBedNo(@Param("wardCode") String wardCode, @Param("deptCode") String deptCode, @Param("bedNo") String bedNo);

    @Insert("insert into zy_bed_mi (ward_code, bed_no, room_no, dept_code, bed_status, room_rate1, room_rate2, heating_fee," +
            " aircondition_fee, heatcold_flag, sex, type, special_charge, charge_code_bed, charge_code_air) values " +
            " (#{wardCode}, #{bedNo}, #{roomNo}, #{deptCode}, #{bedStatus}, #{roomRate1}, #{roomRate2}, #{heatingFee}," +
            "  #{airConditionFee}, #{heatColdFlag}, #{sex}, #{type}, #{specialCharge}, #{chargeCodeBed}, #{chargeCodeAir}) ")
    int saveBedForDeptAndBedNo(Map<String, Object> bedInfo);

    @Select(" select * from zy_adtward where ward_code = #{wardCode} and dept_code = #{deptCode} ")
    Map<String, Object> selectRoomDeptByCode(@Param("wardCode") String wardCode, @Param("deptCode") String deptCode);

    @Update("<script>" +
            "update zy_adtward " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"wardCode != null and wardCode != '' \">" +
            "ward_code = #{wardCode}, " +
            "</if>" +
            "<if test=\"wardName != null and wardName != '' \">" +
            "ward_name = #{wardName}, " +
            "</if>" +
            "<if test=\"deptCode != null and deptCode != '' \">" +
            "dept_code = #{deptCode}, " +
            "</if>" +
            "<if test=\"deptName != null and deptName != '' \">" +
            "dept_name = #{deptName}, " +
            "</if>" +
            "<if test=\"wardPyCode != null and wardPyCode != '' \">" +
            "ward_py_code = #{wardPyCode}, " +
            "</if>" +
            "<if test=\"wardDCode != null and wardDCode != '' \">" +
            "ward_d_code = #{wardDCode}, " +
            "</if>" +
            "<if test=\"deptPyCode != null and deptPyCode != '' \">" +
            "dept_py_code = #{deptPyCode}, " +
            "</if>" +
            "<if test=\"deptDCode != null and deptDCode != '' \">" +
            "dept_d_code = #{deptDCode}, " +
            "</if>" +
            "<if test=\"openbedNum != null and openbedNum != '' \">" +
            "openbed_num = #{openbedNum}, " +
            "</if>" +
            "<if test=\"oweLimit != null and oweLimit != '' \">" +
            "owe_limit = #{oweLimit}, " +
            "</if>" +
            "</trim>" +
            "where ward_code = #{wardCode} and dept_code = #{deptCode} " +
            "</script>")
    int updateRoomDeptByCode(Map<String, Object> roomDept);

    @Insert(" insert into zy_adtward(ward_code, dept_code, openbed_num, ward_name, dept_name, " +
            "   ward_py_code, ward_d_code, dept_py_code, dept_d_code, owe_limit) " +
            " values (#{wardCode}, #{deptCode}, #{openbedNum}, #{wardName}, #{deptName}, " +
            "   #{wardPyCode}, #{wardDCode}, #{deptPyCode}, #{deptDCode}, #{oweLimit})")
    int insertRoomDept(Map<String, Object> roomDept);

    @Delete(" delete from zy_adtward where ward_code = #{wardCode} and dept_code = #{deptCode} ")
    int deleteRoomDeptByCode(@Param("wardCode") String wardCode, @Param("deptCode") String deptCode);

    @Select("select ad2.dept_code as deptCode, ad1.ward_code as wardCode, " +
            "       ad2.dept_name as deptName, ad1.ward_name as wardName, " +
            "       ad2.dept_py_code as deptPyCode, ad1.ward_py_code as wardPyCode, " +
            "       ad2.dept_d_code as deptDCode, ad1.ward_d_code as wardDCode, " +
            "       al.dept, al.small_dept as smallDept, al.dept_id as deptId  " +
            "  from zd_dept_all as al " +
            " inner join zy_adtward as ad1 on ad1.ward_code = al.dept " +
            " inner join zy_adtward as ad2 on ad2.ward_code = al.small_dept " +
            " where (ad1.dept_py_code like '${text}%' or ad1.ward_py_code like '${text}%' " +
            "    or ad1.dept_d_code like '${text}%' or ad1.ward_d_code like '${text}%' " +
            "    or ad1.dept_code like '${text}%' or ad1.ward_code like '${text}%' " +
            "    or ad1.dept_name like '${text}%' or ad1.ward_name like '${text}%') ")
    List<Map<String, Object>> selectSmallDept(@Param("text") String text);

    @Select(" select * from zd_dept_all where dept_id = #{deptId}  ")
    Map<String, Object> selectSmallDeptByCode(@Param("deptId") Integer deptId);

    @Select(" select count(*) cs from zd_dept_all where dept = #{dept} and small_dept = #{smallDept}  ")
    int selectSmallDeptByDept(@Param("dept") String dept, @Param("smallDept") String smallDept);

    @Update("<script>" +
            "update zd_dept_all " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"dept != null and dept != '' \">" +
            "dept = #{dept}, " +
            "</if>" +
            "<if test=\"smallDept != null and smallDept != '' \">" +
            "small_dept = #{smallDept}, " +
            "</if>" +
            "</trim>" +
            "where dept_id = #{deptId} " +
            "</script>")
    int updateSmallDeptByCode(Map<String, Object> smallMap);

    @Insert(" insert into zd_dept_all(dept, small_dept) values (#{dept}, #{smallDept})")
    int insertSmallDept(Map<String, Object> smallMap);

    @Delete(" delete from zd_dept_all where dept_id = #{deptId} ")
    int deleteSmallDeptByCode(@Param("deptId") String deptId);

}
