package thyyxxk.webserver.dao.his.zygl;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.executeItem.ZyLedgerFile;
import thyyxxk.webserver.entity.zygl.ZyDayclosingRecord;

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

/**
 * 住院日结报表数据层
 */
public interface ZyDailySettlementDao {
    /**
     * 根据用户查询最后一次结算时间
     *
     * @param opId
     * @return
     */
    @Select("select max ( date_end ) from zy_dayclosing_record where op_id =#{opId} ")
    Date getLastDailyDateForUser(@Param("opId") String opId);

    /**
     * 查询结算总金额，发票金额和医保记账总金额
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select " +
            "totalCharge = sum(total_charge)," +
            "receiptAmount = sum(deposit-settle)," +
            "jzAmount =sum(total_charge -(deposit - settle))," +
            "fpCount=count(distinct receipt_no) " +
            "from   zy_ledger_file  " +
            "where  cash_date > #{beginTime}  and cash_date <= #{endTime} and  cash_id = #{opId} ")
    Map<String, Object> selectTotalChargeFromLegerFile(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    @Select("select rtrim(inpatient_no) inpatient_no ,admiss_times,ledger_sn from zy_ledger_file  where  cash_date > #{beginTime}  and cash_date <= #{endTime} and  cash_id = #{opId}")
    List<ZyLedgerFile> selectCashPatientByOpId(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    /**
     * 查询预交金信息
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select " +
            " depositCashAmount = sum(case when (window_no='1' and status  ='1' and depo_type='A' )then depo_amount else 0 end)," +
            " depositCashNum = sum(case when (window_no='1' and status  ='1' and depo_type='A' ) then 1 else 0 end)," +
            " depositWxAmount = sum(case when (window_no='1' and status  ='1' and depo_type='E' ) then depo_amount else 0 end)," +
            " depositWxNum = sum(case when (window_no='1' and status  ='1' and depo_type='E' ) then 1 else 0 end)," +
            " depositZfbAmount = sum(case when(window_no='1' and status  ='1' and depo_type='F' ) then depo_amount else 0 end)," +
            " depositZfbNum = sum(case when (window_no='1' and status  ='1' and depo_type='F' ) then 1 else 0 end)," +
            " depositYbAmount = sum(case when (window_no='1' and status  ='1' and depo_type in ('6','7'))  then depo_amount else 0 end)," +
            " depositYbNum = sum(case when(window_no='1' and status  ='1' and depo_type in ('6','7')) then 1 else 0 end)," +
            " depositYlkAmount = sum(case when (window_no='1' and status  ='1' and depo_type='3' )  then depo_amount else 0 end)," +
            " depositYlkNum = sum(case when (window_no='1' and status  ='1' and depo_type='3' )  then 1 else 0 end)," +
            " depositQtAmount = sum(case when (window_no='1' and status  ='1' and depo_type='0' )  then depo_amount else 0 end)," +
            " depositQtNum = sum(case when (window_no='1' and status  ='1' and depo_type='0' )  then 1 else 0 end)," +
            " depositJhzfAmount= sum(case when (window_no='1' and status  ='1' and depo_type='O' )  then depo_amount else 0 end), " +
            " depositJhzfNum = sum(case when (window_no='1' and status  ='1' and depo_type='O' )  then 1 else 0 end)," +
            " depositByjzAmount= sum(case when (window_no='1' and depo_type='Y' )  then depo_amount else 0 end), " +
            " returnCashAmount = sum(case when (window_no='1' and status  ='2' and depo_type='A' )then depo_amount else 0 end)," +
            " returnCashNum = sum(case when (window_no='1' and status  ='2' and depo_type='A' ) then 1 else 0 end)," +
            " returnWxAmount = sum(case when (window_no='1' and status  ='2' and depo_type='E' ) then depo_amount else 0 end)," +
            " returnWxNum = sum(case when (window_no='1' and status  ='2' and depo_type='E' ) then 1 else 0 end)," +
            " returnZfbAmount = sum(case when(window_no='1' and status  ='2' and depo_type='F' ) then depo_amount else 0 end)," +
            " returnZfbNum = sum(case when (window_no='1' and status  ='2' and depo_type='F' ) then 1 else 0 end)," +
            " returnYbAmount = sum(case when (window_no='1' and status  ='2' and depo_type in ('6','7'))  then depo_amount else 0 end)," +
            " returnYbNum = sum(case when(window_no='1' and status  ='2' and depo_type in ('6','7')) then 1 else 0 end)," +
            " returnYlkAmount = sum(case when (window_no='1' and status  ='2' and depo_type='3' )  then depo_amount else 0 end)," +
            " returnYlkNum = sum(case when (window_no='1' and status  ='2' and depo_type='3' )  then 1 else 0 end)," +
            " returnQtAmount = sum(case when (window_no='1' and status  ='2' and depo_type='0' )  then depo_amount else 0 end)," +
            " returnQtNum = sum(case when (window_no='1' and status  ='2' and depo_type='0' )  then 1 else 0 end)," +
            " returnJhzfAmount = sum(case when (window_no='1' and status  ='2' and depo_type='O' )  then depo_amount else 0 end)," +
            " returnJhzfNum = sum(case when (window_no='1' and status  ='2' and depo_type='O' )  then 1 else 0 end)," +
            " jsbkCashAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and depo_type='A') then depo_amount else 0 end)," +
            " jsbkWxAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and depo_type='E') then depo_amount else 0 end)," +
            " jsbkZfbAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and depo_type='F') then depo_amount else 0 end)," +
            " jsbkYbAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and  depo_type in ('6','7'))  then depo_amount else 0 end)," +
            " jsbkYlkAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and depo_type='3')  then depo_amount else 0 end)," +
            " jsbkQtAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and  depo_type in ('0','J')) then depo_amount else 0 end), " +
            " jsbkJhzfAmount=sum(case when (window_no='2' and status in ('1','3','4','5') and depo_type='O') then depo_amount else 0 end)," +
            " jstkCashAmount=sum(case when ( window_no='2' and status ='2' and depo_type='A') then depo_amount else 0 end)," +
            " jstkWxAmount=sum(case when ( window_no='2' and status ='2' and depo_type='E') then depo_amount else 0 end)," +
            " jstkZfbAmount=sum(case when ( window_no='2' and status ='2' and depo_type='F') then depo_amount else 0 end)," +
            " jstkYbAmount=sum(case when ( window_no='2' and status ='2' and depo_type in ('6','7')) then depo_amount else 0 end)," +
            " jstkYlkAmount=sum(case when ( window_no='2' and status ='2' and depo_type='3') then depo_amount else 0 end)," +
            " jstkQtAmount=sum(case when ( window_no='2' and status ='2' and depo_type in ('0','J')) then depo_amount else 0 end)," +
            " jstkJhzfAmount=sum(case when ( window_no='2' and status ='2' and depo_type='O') then depo_amount else 0 end)," +
            " jsjzMzyfAmount=sum(case when ( window_no='2' and status ='4' and upper(depo_type)='D') then depo_amount else 0 end)," +
            " jsCfbzAmount=sum(case when ( window_no='2' and status ='4' and upper(depo_type)='C') then depo_amount else 0 end)," +
            " totalByybdf=sum(case when ( window_no='2' and status ='2' and upper(depo_type)='J') then depo_amount else 0 end) " +
            " from zy_deposit_file " +
            " where " +
            " depo_date >  #{beginTime}  and" +
            " depo_date <= #{endTime} and " +
            " op_id_code = #{opId}")
    Map<String, Object> selectYjjFromDepositFile(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    /**
     * 回收押金
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select hsJe=sum(depo_amount)," +
            "       hsCount=count(distinct zy_deposit_file.receipt_no)" +
            "from   zy_deposit_file " +
            "where  hsr =#{opId} and " +
            "       hs_time > #{beginTime}  and " +
            "       hs_time <= #{endTime} and " +
            "       status  in ( '1','2') and " +
            "       window_no='1'")
    Map<String, Object> selectHsJeAndHsCount(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    /**
     * 查询所有缴费的预交金明细
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select(" SELECT zy_deposit_file.inpatient_no,  " +
            " zy_actpatient.name," +
            " zy_actpatient.ward, " +
            "         zy_deposit_file.receipt_no,   " +
            "         amount=zy_deposit_file.depo_amount,   " +
            "         zy_deposit_file.depo_type  ," +
            "         status," +
            " zy_deposit_file.op_id_code" +
            "    FROM zy_deposit_file ,zy_actpatient " +
            " where zy_deposit_file.depo_date >#{beginTime} and " +
            " zy_deposit_file.depo_date<=#{endTime} and " +
            "         zy_deposit_file.inpatient_no=zy_actpatient.inpatient_no and " +
            "         zy_deposit_file.admiss_times=zy_actpatient.admiss_times and         " +
            " (zy_deposit_file.status='1'  ) and " +
            " (zy_deposit_file.window_no='1') and " +
            " zy_deposit_file.op_id_code =#{opId} " +
            " union " +
            " SELECT zy_deposit_file.inpatient_no,  " +
            " zy_inactpatient.name," +
            " zy_inactpatient.ward,  " +
            "         zy_deposit_file.receipt_no,   " +
            "         depo_amount=zy_deposit_file.depo_amount ,   " +
            "         zy_deposit_file.depo_type  ," +
            "         status," +
            " zy_deposit_file.op_id_code" +
            "    FROM zy_deposit_file ,zy_inactpatient " +
            " where zy_deposit_file.depo_date > #{beginTime} and " +
            " zy_deposit_file.depo_date <=#{endTime} and" +
            "         zy_deposit_file.inpatient_no=zy_inactpatient.inpatient_no and " +
            "         zy_deposit_file.admiss_times=zy_inactpatient.admiss_times and   " +
            " (zy_deposit_file.status='1' ) and" +
            " (zy_deposit_file.window_no='1' ) and " +
            " zy_deposit_file.op_id_code =#{opId} " +
            " order by zy_deposit_file.receipt_no ASC")
    List<Map<String, Object>> selectChargeZyDepositFile(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);


    /**
     * 查询所有退费的预交金明细
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select(" SELECT zy_deposit_file.inpatient_no,  " +
            "zy_actpatient.name," +
            "zy_actpatient.ward, " +
            "         zy_deposit_file.receipt_no,   " +
            "         amount=zy_deposit_file.depo_amount,   " +
            "         zy_deposit_file.depo_type  ," +
            "         status," +
            " zy_deposit_file.op_id_code," +
            " zy_deposit_file.hs_time  " +
            "    FROM zy_deposit_file ,zy_actpatient " +
            " where zy_deposit_file.depo_date >#{beginTime} and " +
            " zy_deposit_file.depo_date<=#{endTime} and" +
            "         zy_deposit_file.inpatient_no=zy_actpatient.inpatient_no and " +
            "         zy_deposit_file.admiss_times=zy_actpatient.admiss_times and         " +
            " (zy_deposit_file.status='2'  ) and" +
            " (zy_deposit_file.window_no='1') and" +
            " zy_deposit_file.op_id_code =#{opId} " +
            " union " +
            " SELECT zy_deposit_file.inpatient_no,  " +
            " zy_inactpatient.name," +
            " zy_inactpatient.ward,  " +
            "         zy_deposit_file.receipt_no,   " +
            "         depo_amount=zy_deposit_file.depo_amount ,   " +
            "         zy_deposit_file.depo_type  ," +
            "         status," +
            " zy_deposit_file.op_id_code," +
            " zy_deposit_file.hs_time  " +
            "    FROM zy_deposit_file ,zy_inactpatient " +
            " where zy_deposit_file.depo_date > #{beginTime} and " +
            " zy_deposit_file.depo_date <=#{endTime} and " +
            "         zy_deposit_file.inpatient_no=zy_inactpatient.inpatient_no and " +
            "         zy_deposit_file.admiss_times=zy_inactpatient.admiss_times and   " +
            "(zy_deposit_file.status='2'  ) and " +
            "(zy_deposit_file.window_no='1' ) and " +
            " zy_deposit_file.op_id_code =#{opId} " +
            " order by zy_deposit_file.receipt_no ASC")
    List<Map<String, Object>> selectRefunfZyDepositFile(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    /**
     * 查询作废的预交金号
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("  SELECT zy_deposit_file.inpatient_no," +
            "         zy_deposit_file.receipt_no,   " +
            "         amount=zy_deposit_file.depo_amount," +
            "         zy_deposit_file.depo_type," +
            "         zy_deposit_file.hs_time,deleted " +
            "    FROM zy_deposit_file   " +
            " where   deleted='1'and " +
            "   depo_date > #{beginTime} and " +
            " depo_date <= #{endTime} and   " +
            " op_id_code =#{opId}")
    List<Map<String, Object>> selectDeleteZyChargeDetail(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    /**
     * 查询结算病人明细
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select(" SELECT a.inpatient_no," +
            "         a.admiss_times," +
            "         a.ledger_sn   ," +
            "         total_charge=max(a.total_charge),   " +
            //"         charge1=sum(b.charge1 + b.charge21), " +
            "         charge1=sum(b.charge1), " +
            "         charge2=sum(b.charge2),   " +
            "         charge3=sum(b.charge3),   " +
            "         charge4=sum(b.charge4),   " +
            "         charge5=sum(b.charge5),   " +
            "         charge6=sum(b.charge6),   " +
            "         charge7=sum(b.charge7),   " +
            "         charge8=sum(b.charge8),   " +
            "         charge9=sum(b.charge9),   " +
            "         charge10=sum(b.charge10),   " +
            "         charge11=sum(b.charge11),   " +
            "         charge12=sum(b.charge12),   " +
            "         charge13=sum(b.charge13),   " +
            "         charge14=sum(b.charge14),   " +
            "         charge15=sum(b.charge15),   " +
            "         charge16=sum(b.charge16),   " +
            "         charge17=sum(b.charge17),   " +
            "         charge18=sum(b.charge18),   " +
            "         charge19=sum(b.charge19),   " +
            "         charge20=sum(b.charge20)," +
            "         charge21=sum(b.charge21)," +
            "         charge22=sum(b.charge22)," +
            "         charge23=sum(b.charge23)," +
            "         charge24=sum(b.charge24),    " +
            "         charge25=sum(b.charge25), " +
            "         charge26=sum(b.charge26)," +
            "         charge27=sum(b.charge27)," +
            "         charge28=sum(b.charge28),  " +
            "         name=max(c.name)," +
            "         receipt_no=max(b.receipt_no) " +
            "    FROM zy_ledger_file a join zy_receipt b on a.inpatient_no = b.inpatient_no and a.admiss_times = b.admiss_times and a.ledger_sn=b.ledger_sn " +
            "     left join  a_patient_mi c on a.inpatient_no = c.inpatient_no " +
            "    WHERE a.cash_date >  #{beginTime} and " +
            " a.cash_date <= #{endTime}  and " +
            "          (a.cash_id =#{opId} or a.cash_id is null)   " +
            " group by a.inpatient_no,a.admiss_times ,a.ledger_sn " +
            " order by receipt_no,a.inpatient_no")
    List<Map<String, Object>> selectZyChargeDetail(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);


    /**
     * 结算预交金明细
     *
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("SELECT  a.inpatient_no,  " +
            "         c.name,   " +
            "         a.admiss_times,    " +
            "         depo_amount=case when a.status ='2' and a.depo_amount>0  then 0 - a.depo_amount else a.depo_amount end,    " +
            "         a.receipt_no, " +
            "   a.op_id_code   " +
            "    FROM zy_deposit_file a  left join a_patient_mi c on a.inpatient_no=c.inpatient_no " +
            "   WHERE   " +
            " " +
            "   a.depo_date >  #{beginTime}  and  " +
            "    a.depo_date <= #{endTime} and " +
            "          a.op_id_code =#{opId} and " +
            "     a.status in ('1','2') and " +
            "           window_no=1 ")
    List<Map<String, Object>> selectJsYjjDetail(@Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);


    /**
     * 结算费用登记报表
     *
     * @param tableName
     * @param opId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select z.*,y.name,y.sex,y.admiss_date,y.responce_type from (" +
            "select   ward        =max(a.ward_code)," +
            "        in_no=a.inpatient_no," +
            "        times=a.admiss_times," +
            "        total_charge=max(a.total_charge ), " +
            "        charge_self =sum(a.deposit-a.settle)," +
            "        charge_jz   =sum(a.total_charge - (a.deposit - a.settle))," +
            "        charge_bk   =isnull(max(d.charge_bk),0) ," +
            "        charge_tk   =isnull(max(d.charge_tk),0) ," +
            "        deposit     =case when a.ledger_sn<0 then 0 - abs(max(a.deposit)) else max(a.deposit) end  ," +
            "        receipt_no  =case when max(isnull(c.dzfp_receipt_no,'')) !='' then max(c.dzfp_receipt_no) else max(c.receipt_no) end ," +
            " ledger_sn=a.ledger_sn " +
            "          from zy_ledger_file a join zy_receipt c on a.inpatient_no=c.inpatient_no and a.admiss_times=c.admiss_times and a.ledger_sn = c.ledger_sn " +
            "    left join    (select inpatient_no,admiss_times,ledger_sn," +
            "                charge_bk=sum(case when status in ('1','3','4','5') and depo_type <>'2' then depo_amount else 0 end)," +
            "                charge_tk=sum(case when status='2' then depo_amount else 0 end) " +
            "         from zy_deposit_file " +
            "         where  window_no='2' and depo_date > #{beginTime} and depo_date <=#{endTime} and status  in ('1','2','3','4','5') " +
            "         group by inpatient_no,admiss_times,ledger_sn) as d on a.inpatient_no = d.inpatient_no and a.admiss_times = d.admiss_times and a.ledger_sn=d.ledger_sn  " +
            " where   a.cash_date > #{beginTime}   and " +
            " a.cash_date <= #{endTime}   and " +
            "        isnull(a.cash_id,a.op_id_code) =#{opId}  " +
            " and  c.receipt_sn<>4 " +
            " group by a.inpatient_no,a.admiss_times,a.ledger_sn ) as z,${tableName} y " +
            " where z.in_no=y.inpatient_no and " +
            "        z.times=y.admiss_times " +
            " order by  z.receipt_no")
    List<Map<String, Object>> selectJsFydjbb(@Param("tableName") String tableName, @Param("opId") String opId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);

    /**
     * 查询最后一次日结的结束时间
     * @param opId
     * @return
     */
    @Select("select top 1 date_end from zy_dayclosing_record where op_id =#{opId} order by closing_date desc ")
    Date selectLastCloseDate(@Param("opId") String opId);

    /**
     * 保存住院日结记录
     * @param lastCloseDate
     * @param dateEnd
     * @param closeDate
     * @param opId
     * @return
     */
    @Insert("insert into zy_dayclosing_record ( op_id , closing_date , date_start , date_end , yjj_flag , bt_flag , account_patient_flag , account_ward_flag , ward_income_flag , account_deposit_flag , flag , input_id ) values " +
            "( #{opId} , #{closeDate} , #{lastCloseDate} , #{dateEnd} , '1' , '1' , '1' , '1' , '1' , '1' , '1' , #{opId} ) ")
    int insertZyDayclosingRecord(@Param("closeDate") Date closeDate,@Param("lastCloseDate") Date lastCloseDate, @Param("dateEnd") Date dateEnd,  @Param("opId") String opId);

    /**
     * 更新日结记录
     * @param closeDate
     * @param lastCloseDate
     * @return
     */
    @Update("update zy_ledger_file set dcount_no ='1' , dcount_date =#{closeDate} where cash_date > #{lastCloseDate} and cash_date <= #{dateEnd} and cash_id = #{opId} ")
    int updateZyLedgerFile(@Param("closeDate") Date closeDate, @Param("lastCloseDate") Date lastCloseDate,@Param("dateEnd") Date dateEnd, @Param("opId") String opId);

    /**
     * @description: 查询结账日志
     * @author: lihong
     * @date: 2025/6/12 8:44
     * @param: opId
     * @return: java.util.List<thyyxxk.webserver.entity.zygl.ZyDayclosingRecord>
     **/
    @Select("<script>" +
            " select op_id,closing_date,date_start,date_end,(select case when op_id='99999' then '自助' else name end from a_employee_mi where code = op_id) op_name " +
            " from zy_dayclosing_record " +
            "<where>" +
            "<if test=\" opId !=null and opId !='' \">" +
            " and op_id = #{opId} " +
            "</if>" +
            "<if test=\" dateStart !=null  \">" +
            " and closing_date &gt;= #{dateStart,jdbcType=TIMESTAMP} " +
            "</if>" +
            "<if test=\" dateEnd !=null  \">" +
            " and closing_date &lt;= #{dateEnd,jdbcType=TIMESTAMP} " +
            "</if>" +
            "</where>" +
            " order by closing_date desc" +
            "</script>")
    List<ZyDayclosingRecord> selectZyDayclosingRecord(ZyDayclosingRecord zyDayclosingRecord);
    @Select(" select b.name  " +
            " from dj_user_role a  " +
            "         join dj_dict_role b on a.role_id = b.id  " +
            "         where a.user_code=#{userCode} ")
    List<String> selectRoleNamesByUserCode(String userCode);
    @Select(" select dzfp_receipt_no from zy_receipt where inpatient_no=#{inpatientNo} and admiss_times= #{admissTimes} and ledger_sn = #{ledgerSn} and receipt_sn = 1")
    String selectzyReceiptDzfp(ZyLedgerFile zyLedgerFile);
    @Select(" select count(*) from zy_receipt where  inpatient_no=#{inpatientNo} and admiss_times= #{admissTimes} and ledger_sn = #{ledgerSn} and receipt_sn = 4")
    Integer selectCountZyReceiptDzfpBaby(ZyLedgerFile zyLedgerFile);
}
