package thyyxxk.webserver.dao.his.outpatient;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageInquiry;
import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackage;
import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackageItem;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface WxmallPackageDao {

    @Select("select * from t_wxmall_package where create_time>=#{start} " +
            "and create_time<=#{end} ${statement}")
    List<WxmallPackage> getPackagesByCondition(PackageInquiry inquiry);

    @Select("select sum(a.amount*b.charge_amount) from ${table} a," +
            "zd_charge_item b where a.code=#{code} and a.charge_code=b.code")
    BigDecimal getOriginPrice(String table, String code);
    
    @Delete("delete from t_wxmall_package where id=#{id};" +
            "delete from t_wxmall_package_item where package_id=#{id}")
    void deletePackage(String id);

    @Insert("insert into t_wxmall_package " +
            "(id,name,origin_price,discount_price,thumb_path,create_time," +
            "create_staff_code,create_staff_name,description) " +
            "values " +
            "(#{id},#{name},#{originPrice},#{discountPrice},#{thumbPath},#{createTime}," +
            "#{createStaffCode},#{createStaffName},#{description})")
    void insertNewPackage(WxmallPackage wxmallPackage);

    @Insert("<script>" +
            "insert into t_wxmall_package_item " +
            "(package_id,his_code,his_name,type,origin_price,discount_price,quantity,cost_amt," +
            "gender_restriction,min_age_restriction,max_age_restriction) " +
            "values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.packageId},#{item.hisCode},#{item.hisName},#{item.type}," +
            "#{item.originPrice},#{item.discountPrice},#{item.quantity},#{item.costAmt}," +
            "#{item.genderRestriction},#{item.minAgeRestriction},#{item.maxAgeRestriction})" +
            "</foreach>" +
            "</script>")
    void insertPackageItem(List<WxmallPackageItem> list);

    @Update("update t_wxmall_package set state=#{state} where id=#{id}")
    void updatePackageState(WxmallPackage pkg);

    @Select("select * from t_wxmall_package where id=#{id}")
    WxmallPackage getPackageById(String id);

    @Select("select * from t_wxmall_package_item where package_id=#{packageId}")
    List<WxmallPackageItem> getPackageItems(String packageId);
}
