package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
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 shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.vo.LifeCouponVo;

import java.util.List;

@Mapper
public interface LifeCouponMapper extends BaseMapper<LifeCoupon> {

    @Select("select coupon.id,\n" +
            "coupon.store_id,\n" +
            "coupon.coupon_code,\n" +
            "coupon.`name`,\n" +
            "coupon.price,\n" +
            "coupon.offprice,\n" +
            "coupon.expiration_date,\n" +
            "coupon.start_date,\n" +
            "coupon.end_date,\n" +
            "coupon.unused_date,\n" +
            "coupon.single_qty,\n" +
            "coupon.buy_limit,\n" +
            "coupon.stock_qty,\n" +
            "coupon.use_rule,\n" +
            "coupon.min_use_peoples,\n" +
            "coupon.max_use_peoples,\n" +
            "coupon.dietary_preferences,\n" +
            "coupon.cooking_methods,\n" +
            "coupon.applicable_rule,\n" +
            "coupon.`status`,\n" +
            "coupon.store_service,\n" +
            "coupon.fapiao_info,\n" +
            "coupon.type,\n" +
            "coupon.approval_comments,\n" +
            "coupon.delete_flag,\n" +
            "coupon.created_time,\n" +
            "coupon.updated_time,\n" +
            "coupon.created_user_id,\n" +
            "coupon.updated_user_id,\n" +
            "coupon.open_price_protection,\n" +
            "coupon.price_below,\n" +
            "coupon.coupon_comp,\n" +
            "coupon_comp_date,\n" +
            "coupon.uploaded_picList,\n" +
            "coupon.applicable_num,\n" +
            "coupon.write_off,\n" +
            "coupon.reservation_rules,\n" +
            "coupon.rate_score, store.store_name, store.store_address, user.phone, coupon.created_time, coupon.name, coupon.status " +
            "from life_coupon coupon " +
            "left join store_info store on store.id = coupon.store_id and store.delete_flag = 0 " +
            "left join store_user user on store.id = user.store_id and user.delete_flag = 0 ${ew.customSqlSegment}")
    IPage<LifeCouponVo> getGroupPackageList(IPage<LifeCouponVo> page, @Param(Constants.WRAPPER) QueryWrapper<LifeCouponVo> queryWrapper);

    @Select("SELECT lc.store_id,lc.price FROM life_coupon AS lc LEFT JOIN store_info AS si ON lc.store_id = si.id WHERE si.delete_flag = 0 and si.business_section_name = #{businessSectionName}")
    List<LifeCoupon> selectCoupon(String businessSectionName);

    @Select("SELECT * FROM life_coupon ${ew.customSqlSegment}")
    List<LifeCoupon> getList(@Param(Constants.WRAPPER) LambdaQueryWrapper<LifeCoupon> lambdaQueryWrapper);

    @Update(" UPDATE life_coupon\n" +
            "    SET \n" +
            "        status = CASE WHEN (single_qty - #{buyCount}) = 0 \n" +
            "                     THEN #{soldOutStatus} \n" +
            "                     ELSE status END,\n" +
            "        single_qty = single_qty - #{buyCount}\n" +
            "    WHERE \n" +
            "        id = #{couponId} \n" +
            "        AND single_qty >= #{buyCount}")
    int deductInventoryAtomically(
            @Param("couponId") Integer couponId,
            @Param("buyCount") int buyCount,
            @Param("soldOutStatus") int soldOutStatus
    );


    /**
     * 查询代金券详情（连表查询商铺信息）
     *
     * @param id 代金券ID
     * @return LifeCouponVo
     */
    @Select("SELECT " +
            "lc.id, " +
            "lc.data_type, " +
            "lc.buy_use_start_time, " +
            "lc.buy_use_end_time, " +
            "lc.use_festival, " +
            "lc.coupon_code, " +
            "lc.name, " +
            "lc.store_id, " +
            "lc.price, " +
            "lc.offprice, " +
            "lc.expiration_date, " +
            "lc.start_date, " +
            "lc.end_date, " +
            "lc.unused_date, " +
            "lc.single_qty, " +
            "lc.buy_limit, " +
            "lc.stock_qty, " +
            "lc.use_rule, " +
            "lc.min_use_peoples, " +
            "lc.max_use_peoples, " +
            "lc.dietary_preferences, " +
            "lc.cooking_methods, " +
            "lc.applicable_rule, " +
            "lc.status, " +
            "lc.image_path, " +
            "lc.store_service, " +
            "lc.fapiao_info, " +
            "lc.type, " +
            "lc.approval_comments, " +
            "lc.open_price_protection, " +
            "lc.price_below, " +
            "lc.coupon_comp, " +
            "lc.coupon_comp_date, " +
            "lc.rate_score, " +
            "lc.discount_tag, " +
            "lc.discount_tag_name, " +
            "lc.delete_flag, " +
            "lc.status_delete_flag, " +
            "lc.created_time, " +
            "lc.created_user_id, " +
            "lc.updated_time, " +
            "lc.updated_user_id, " +
            "lc.store_type, " +
            "lc.sale_time_str_type, " +
            "lc.sale_time_end_type, " +
            "lc.purchase_limit_code, " +
            "lc.original_price, " +
            "lc.discounted_price, " +
            "lc.business_types, " +
            "lc.housing_type, " +
            "lc.room_type, " +
            "lc.room_type_code, " +
            "lc.kitchen_xq, " +
            "lc.bathroom_xq, " +
            "lc.living_room, " +
            "lc.location_of_the_property, " +
            "lc.total_area, " +
            "lc.entertainment_facilities_code, " +
            "lc.entertainment_facilities, " +
            "lc.convenience_facilities_code, " +
            "lc.convenience_facilities, " +
            "lc.bathroom_facilities_code, " +
            "lc.bathroom_facilities, " +
            "lc.bath_products_code, " +
            "lc.bath_products, " +
            "lc.service_code, " +
            "lc.service, " +
            "lc.safety_code, " +
            "lc.safety, " +
            "lc.reservation_instructions, " +
            "lc.in_time, " +
            "lc.out_time, " +
            "lc.deposit_flag, " +
            "lc.deposit_amount, " +
            "lc.children_flag, " +
            "lc.children_description, " +
            "lc.pet_flag, " +
            "lc.extra_bed_flag, " +
            "lc.bed_price, " +
            "lc.invoice_info, " +
            "lc.invoice_instructions, " +
            "lc.cancellation_policy_type, " +
            "lc.cancellation_policy, " +
            "lc.Cancellation_instructions, " +
            "lc.insured_price, " +
            "lc.validity_period_type, " +
            "lc.box_type_code, " +
            "lc.box_type, " +
            "lc.snacks_code, " +
            "lc.snacks, " +
            "lc.available_duration, " +
            "lc.write_off_type, " +
            "lc.write_off, " +
            "lc.refund_rules_type, " +
            "lc.refund_rules, " +
            "lc.refund_instructions, " +
            "lc.edit_rules_code, " +
            "lc.edit_rules, " +
            "lc.edit_instructions, " +
            "lc.tangchi_code, " +
            "lc.tangchi, " +
            "lc.sweat_sauna_code, " +
            "lc.sweat_sauna, " +
            "lc.pictures_and_text, " +
            "lc.supplement, " +
            "lc.unavailable_date_type, " +
            "lc.reservation_rules, " +
            "lc.applicable_num, " +
            "lc.other_rules, " +
            "lc.sales_type, " +
            "lc.massage_technique_code, " +
            "lc.massage_technique, " +
            "lc.massage_area_code, " +
            "lc.massage_area, " +
            "lc.massage_tools_code, " +
            "lc.massage_tools, " +
            "lc.compress_tools_code, " +
            "lc.compress_tools, " +
            "lc.class_format_code, " +
            "lc.class_format, " +
            "lc.class_duration_code, " +
            "lc.class_duration, " +
            "lc.class_num_code, " +
            "lc.class_num, " +
            "lc.applicable_parts_code, " +
            "lc.applicable_parts, " +
            "lc.hygiene_code, " +
            "lc.hygiene, " +
            "lc.foundation_code, " +
            "lc.foundation, " +
            "lc.bathroom_code, " +
            "lc.bathroom, " +
            "lc.kitchen_code, " +
            "lc.kitchen, " +
            "lc.validity_period_yh_type, " +
            "lc.validity_period_yh, " +
            "lc.course_validity_period_code, " +
            "lc.course_validity_period, " +
            "lc.uploaded_picList, " +
            "lc.validity_period, " +
            "lc.stacking_type, " +
            "lc.general_type, " +
            "lc.single_can_use, " +
            "lc.apply_type, " +
            "lc.apply_desc, " +
            "lc.expiration_type, " +
            "lc.unused_type, " +
            "lc.house_type, " +
            "lc.house_type_b, " +
            "lc.house_type_l, " +
            "lc.house_type_k, " +
            "lc.house_type_t, " +
            "lc.sale_time_str, " +
            "lc.sale_time_end, " +
            "lc.unavai_lable_date, " +
            "lc.room_type_str, " +
            "lc.time_range, " +
            "lc.cancellation_policy_str, " +
            "lc.validity_periodyh_str, " +
            "si.store_name, " +
            "si.store_address, " +
            "si.store_tel AS phone, " +
            "si.business_status, " +
            "si.store_status " +
            "FROM life_coupon lc " +
            "LEFT JOIN store_info si ON si.id = lc.store_id AND si.delete_flag = 0 " +
            "WHERE lc.id = #{id} AND lc.delete_flag = 0")
    LifeCouponVo getNewCouponDetail(@Param("id") String id);
}
