package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.apache.poi.ss.formula.functions.T;
import shop.alien.entity.store.LifeGroupBuyMain;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import shop.alien.entity.store.vo.LifeGroupBuyMainVo;
import shop.alien.entity.store.vo.LifeGroupBuyThaliVo;

import java.util.List;

/**
* @author youch
* @description 针对表【life_group_buy_main(团购主表)】的数据库操作Mapper
* @createDate 2025-07-28 14:31:30
* @Entity shop.alien.entity.store.LifeGroupBuyMain
*/
@Mapper
public interface LifeGroupBuyMainMapper extends BaseMapper<LifeGroupBuyMain> {

    @Select("select id,group_no,group_type,status,approval_comments,store_id,image_id,group_name,start_time_type,\n" +
            "        start_time_value,end_time,inventory_num,quota_type,quota_value,\n" +
            "        original_price,preferential_price,effective_date_type,effective_date_value,disable_date_type,\n" +
            "        disable_date_value,write_off,reservation_rules,use_rules,applicable_num,\n" +
            "        other_rules,invoice_type,invoice_describe,insured_flag,insured_price,\n" +
            "        coupon_id,delete_flag,created_time,updated_time,created_user_id,\n" +
            "        updated_user_id from life_group_buy_main ${ew.customSqlSegment}")
    IPage<LifeGroupBuyThaliVo> selectPageByThaliVo(IPage<LifeGroupBuyThaliVo> lifeGroupBuyThaliVoIPage,@Param(Constants.WRAPPER) QueryWrapper<LifeGroupBuyThaliVo> queryWrapper);


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

    @Select("SELECT\n" +
            "    g.id AS id,\n" +
            "    g.group_no AS group_no,\n" +
            "    g.group_type AS group_type,\n" +
            "    g.status AS status,\n" +
            "    g.approval_comments AS approval_comments,\n" +
            "    g.store_id AS store_id,\n" +
            "    g.image_id AS image_id,\n" +
            "    g.group_name AS group_name,\n" +
            "    g.start_time_type AS start_time_type,\n" +
            "    g.start_time_value AS start_time_value,\n" +
            "    g.end_time AS end_time,\n" +
            "    g.inventory_num AS inventory_num,\n" +
            "    g.quota_type AS quota_type,\n" +
            "    g.quota_value AS quota_value,\n" +
            "    g.original_price AS original_price,\n" +
            "    g.preferential_price AS preferential_price,\n" +
            "    g.effective_date_type AS effective_date_type,\n" +
            "    g.effective_date_value AS effective_date_value,\n" +
            "    g.disable_date_type AS disable_date_type,\n" +
            "    g.disable_date_value AS disable_date_value,\n" +
            "    g.write_off AS write_off,\n" +
            "    g.reservation_rules AS reservation_rules,\n" +
            "    g.use_rules AS use_rules,\n" +
            "    g.applicable_num AS applicable_num,\n" +
            "    g.other_rules AS other_rules,\n" +
            "    g.invoice_type AS invoice_type,\n" +
            "    g.invoice_describe AS invoice_describe,\n" +
            "    g.insured_flag AS insured_flag,\n" +
            "    g.insured_price AS insured_price,\n" +
            "    g.coupon_id AS coupon_id,\n" +
            "    g.delete_flag AS delete_flag,\n" +
            "    g.created_time AS created_time,\n" +
            "    g.updated_time AS updated_time,\n" +
            "    g.created_user_id AS created_user_id,\n" +
            "    g.updated_user_id AS updated_user_id,\n" +
            "    s.store_name AS store_name,\n" +
            "    s.store_address AS store_address,\n" +
            "    s.store_tel AS store_tel,\n" +
            "    ROUND(s.score_avg) AS scoreAvg,\n" +
            "    s.business_types_name AS businessName,\n" +
            "    s.query_address AS address,\n" +
            "    IFNULL(GROUP_CONCAT(img.img_url SEPARATOR ','), '') AS imagePaths\n" +
            "FROM\n" +
            "    life_group_buy_main g\n" +
            "LEFT JOIN store_info s \n" +
            "    ON g.store_id = s.id \n" +
            "    AND s.delete_flag = 0 \n" +
            "    AND g.status = 5 \n" +
            "LEFT JOIN store_img img \n" +
            "    ON FIND_IN_SET(img.id, g.image_id) > 0 \n" +
            "    AND img.delete_flag = 0\t\t \n" +
            "WHERE\n" +
            "    g.delete_flag = 0\n" +
            "GROUP BY\n" +
            "    g.id")
    List<LifeGroupBuyMainVo> getLifeGroupInfo(@Param(Constants.WRAPPER) LambdaQueryWrapper<LifeGroupBuyMainVo> queryWrapper);
}




