package shop.alien.mapper;

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.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import shop.alien.entity.store.LifeUserExpertOrder;
import shop.alien.entity.store.vo.LifeUserExpertOrderVo;

import java.util.List;

/**
 * <p>
 * 达人订单表 Mapper 接口
 * </p>
 *
 * @author ssk
 * @since 2025-02-19
 */
public interface LifeUserExpertOrderMapper extends BaseMapper<LifeUserExpertOrder> {

    @Select("with works as ( " +
            "    select order_id, sum(play_count) playCount " +
            "    from life_user_expert_works " +
            "    where delete_flag = 0 " +
            "    group by order_id " +
            ") " +
            "select orders.id, orders. order_no, store.store_name, orders.order_money, orders.account_money, if (orders.status = '1', '待完成', '已完成') status, " +
            "       orders.order_time, orders.first_payment_time, orders.final_payment_time, orders.complete_time, orders.order_play_count, works.playCount actualPlayCount , " +
            "       orders.order_gmv, orders.actual_gmv " +
            "from life_user_expert_order orders " +
            "left join store_info store on store.id = orders.order_no " +
            "left join works on works.order_id = orders.id ")
    IPage<LifeUserExpertOrderVo> getExpertOrderList(IPage<LifeUserExpertOrderVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeUserExpertOrderVo> wrapper);


    @Select("select SUM(lueo.account_money) AS accountMoney, " +
            "SUM(COALESCE(lueo.order_money, 0)) - SUM(COALESCE(lueo.account_money, 0)) AS pendingPerformanceIncome, " +
            "SUM(CASE WHEN lueo.STATUS = 2 THEN 1 ELSE 0 END) AS completedOrdersNumber, " +
            "SUM(CASE WHEN lueo.STATUS = 1 THEN 1 ELSE 0 END) AS incompleteOrdersNumber " +
            "FROM life_user_expert_order lueo " +
            " WHERE expert_id = #{expertId}")
    List<LifeUserExpertOrderVo> getExpertOrderDetails(@Param("expertId") Long expertId);


    @Select("select orders.id, orders.order_no, store.store_name, orders.order_money, package.name,orders.account_money,orders.first_funds_button,orders.end_funds_button, if (orders.status = '1', '待完成', '已完成') statusName, " +
            "       orders.order_time, orders.complete_time  " +
            "from life_user_expert_order orders " +
            "left join store_info store on store.id = orders.store_id " +
            "left join life_promotion_package package on package.id = orders.package_id " +
            "WHERE orders.expert_id = #{expertId}")
    List<LifeUserExpertOrderVo> getPromotionOrderInfoList(@Param("expertId") Long expertId);


    @Select("WITH works AS (SELECT order_id, SUM(play_count) playCount FROM life_user_expert_works WHERE delete_flag = 0 GROUP BY order_id), " +
            "earliest_works AS (SELECT order_id, MIN(created_time) AS earliest_created_time FROM life_user_expert_works GROUP BY order_id), " +
            "purchase AS (SELECT order_id, SUM(CASE WHEN STATUS = 1 THEN 1 ELSE 0 END) completedOrdersNumber, SUM(purchase_amount) amount FROM life_user_expert_purchase WHERE delete_flag = 0 GROUP BY order_id), " +
            "record AS (SELECT order_id, COUNT(order_id) storeVisitViewCount FROM life_browse_record WHERE delete_flag = 0 GROUP BY order_id) " +
            "SELECT orders.id, orders.order_no, store.store_name, orders.order_money, orders.first_funds_button,orders.end_funds_button,package.name,ew.earliest_created_time, orders.account_money, IF(orders.STATUS = '1', '待完成', '已完成') AS statusName, " +
            "orders.order_time, orders.first_payment_time, orders.final_payment_time, orders.complete_time, orders.order_play_count, orders.order_gmv, " +
            "IFNULL(works.playCount, 0) AS actualPlayCount, IFNULL(purchase.amount, 0) AS actualGmv, IFNULL(purchase.completedOrdersNumber, 0) AS completedOrdersNumber, " +
            "IFNULL(purchase.amount, 0) AS transactionAmount, IFNULL(record.storeVisitViewCount, 0) AS storeVisitViewCount, ew.earliest_created_time AS firstWorkTime " +
            "FROM life_user_expert_order orders " +
            "left join life_promotion_package package on package.id = orders.package_id " +
            "LEFT JOIN store_info store ON store.id = orders.store_id " +
            "LEFT JOIN works ON works.order_id = orders.id " +
            "LEFT JOIN purchase ON purchase.order_id = orders.id " +
            "LEFT JOIN record ON record.order_id = orders.id " +
            "LEFT JOIN earliest_works ew ON ew.order_id = orders.id " +
            "WHERE orders.expert_id = #{expertId} AND orders.id = #{orderId}")
    LifeUserExpertOrderVo fetchExpertOrderInfo(@Param("expertId") Long expertId, @Param("orderId") String orderId);


    @Select("SELECT works.expert_id, works.order_id, works.works_url, works.play_count, works.created_time, orders.order_money " +
            "FROM life_user_expert_works works " +
            "LEFT JOIN life_user_expert_order orders ON works.order_id = orders.id " +
            "WHERE works.expert_id = #{expertId} AND works.order_id = #{orderId}")
    List<LifeUserExpertOrderVo> getTalentPromotionOrder(@Param("expertId") String expertId, @Param("orderId") String orderId);


    @Select("SELECT *  " +
            "FROM life_user_expert_order " +
            "WHERE order_id = #{orderId}")
    LifeUserExpertOrderVo selectByOrderId(Integer orderId);

    //增加一个updateButton的方法，通过订单id来更新按钮状态
//    @Update("UPDATE " +
//            "life_user_expert_order " +
//            "SET update_button = #{updateButton} " +
//            "WHERE order_id = #{orderId}")
//    Integer updateButton(@Param("orderId") String orderId, @Param("updateButton") String updateButton);


    @Update("UPDATE " +
            "life_user_expert_order " +
            "SET first_funds_button = #{firstButton}, " +
            "end_funds_button = #{endButton} " +
            "WHERE order_no = #{orderNo}")
    void updateButton(@Param("firstButton") String firstButton,@Param("endButton") String endButton,@Param("orderNo") String orderNo);
}
