package com.nursinghome.mapper;

import com.nursinghome.entity.Bill;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BillMapper {
    @Insert("INSERT INTO bill(elder_id, month, bed_fee, care_fee, meal_fee, other_fee, total, status, created_at) VALUES(#{elderId}, #{month}, #{bedFee}, #{careFee}, #{mealFee}, #{otherFee}, #{total}, #{status}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Bill bill);

    @Select("SELECT * FROM bill ORDER BY month DESC")
    List<Bill> listAll();

    @Select("SELECT * FROM bill WHERE id = #{id}")
    Bill findById(Long id);

    @Select("SELECT * FROM bill WHERE elder_id = #{elderId} ORDER BY month DESC")
    List<Bill> listByElderId(Long elderId);

    @Select("<script>SELECT * FROM bill WHERE elder_id IN <foreach collection='elderIds' item='id' open='(' separator=',' close=')'>#{id}</foreach> ORDER BY month DESC</script>")
    List<Bill> listByElderIds(@Param("elderIds") List<Long> elderIds);

    @Update("UPDATE bill SET status=#{status}, paid_at=#{paidAt} WHERE id=#{id}")
    int updateStatus(Bill bill);

    @Update("UPDATE bill SET elder_id=#{elderId}, month=#{month}, bed_fee=#{bedFee}, care_fee=#{careFee}, meal_fee=#{mealFee}, other_fee=#{otherFee}, total=#{total}, status=#{status} WHERE id=#{id}")
    int update(Bill bill);

    @Delete("DELETE FROM bill WHERE id=#{id}")
    int delete(Long id);
}
