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.LifeUserExpert;
import shop.alien.entity.store.vo.ExpertGoodsTypeVo;
import shop.alien.entity.store.vo.LifeUserExpertOrderVo;
import shop.alien.entity.store.vo.LifeUserExpertVo;

import java.util.List;

/**
 * <p>
 * 用户达人表 Mapper 接口
 * </p>
 *
 * @author ssk
 * @since 2025-02-19
 */
public interface LifeUserExpertMapper extends BaseMapper<LifeUserExpert> {

    @Select("with fans as ( " +
            "    select followed_user_type, followed_ref_id, count(id) num " +
            "    from life_fans " +
            "    where delete_flag = 0 and followed_user_type = 1 " +
            "    group by followed_user_type, followed_ref_id " +
            "), " +
            "works as ( " +
            "    select expert_id, count(id) worksNum, sum(like_count) likeNum, sum(play_count) playNum " +
            "    from life_user_expert_works " +
            "    where delete_flag = 0 " +
            "    group by expert_id " +
            "), " +
            "orders as ( " +
            "    select expert_id, sum(order_money) orderMoney, sum(account_money) accountMoney " +
            "    from alien.life_user_expert_order " +
            "    where delete_flag = 0 " +
            "    group by expert_id " +
            ") " +
            "select expert.id, expert.expert_code,expert.expert_status, user.user_phone,user.user_name, ifnull(fans.num, 0) fansNum, ifnull(works.worksNum, 0) worksNum, ifnull(works.likeNum, 0) likeNum, ifnull(works.playNum, 0) playNum, " +
            "       ifnull(orders.orderMoney, 0) orderMoney, ifnull(orders.accountMoney, 0) accountMoney, (ifnull(orders.orderMoney, 0) - ifnull(orders.accountMoney, 0)) pendMoney " +
            "from life_user_expert expert " +
            "join life_user user on user.id = expert.user_id " +
            "left join fans on fans.followed_user_type = 1 and fans.followed_ref_id = user.id  " +
            "left join works on works.expert_id = expert.id " +
            "left join orders on orders.expert_id = expert.id " +
            "${ew.customSqlSegment}")
    IPage<LifeUserExpertVo> getUserExpertList(IPage<LifeUserExpertVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeUserExpertVo> wrapper);

    @Select("with works as ( " +
            "    select order_id, sum(play_count) playCount " +
            "    from life_user_expert_works " +
            "    where delete_flag = 0 " +
            "    group by order_id " +
            "), " +
            "purchase as ( " +
            "    select order_id, sum(purchase_amount) amount " +
            "    from life_user_expert_purchase " +
            "    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', '待完成', '已完成') 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) actualPlayCount, ifnull(purchase.amount, 0) actualGmv " +
            "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 " +
            "left join purchase on purchase.order_id = orders.id " +
            "${ew.customSqlSegment}")
    IPage<LifeUserExpertOrderVo> getExpertOrderList(IPage<LifeUserExpertOrderVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeUserExpertOrderVo> wrapper);

    @Select("select expert.id, expert.expert_code, user.user_name, user.province, user.city, user.district, user.user_brithday, user.user_sex " +
            "from life_user_expert expert " +
            "join life_user user on user.id = expert.user_id " +
            "where expert.delete_flag = 0 and user.delete_flag = 0")
    List<LifeUserExpertVo> getExpertInfoList();

    @Select("with gmv1 as ( " +
            "    select expert_id, store_id, sum(order_gmv) orderGmv " +
            "    from life_user_expert_order " +
            "    where delete_flag = 0 " +
            "    group by expert_id, store_id " +
            "), " +
            "gmv2 as ( " +
            "    select orders.expert_id, orders.store_id, sum(purchase.purchase_amount) purchaseAmount " +
            "    from life_user_expert_purchase purchase " +
            "    join life_user_expert_order orders on orders.id = purchase.order_id " +
            "    where purchase.delete_flag = 0 and orders.delete_flag = 0 " +
            "    group by orders.expert_id, orders.store_id " +
            ") " +
            "select store.store_name, img.img_url storeImg, ifnull(gmv1.orderGmv, 0) orderGmv, ifnull(gmv2.purchaseAmount, 0) actualGmv " +
            "from life_user_expert_order orders " +
            "left join store_info store on store.id = orders.store_id  " +
            "left join store_img img on img.store_id = store.id and img.img_type = '10' and img.delete_flag = 0 " +
            "left join gmv1 on gmv1.expert_id = orders.expert_id and gmv1.store_id = orders.store_id " +
            "left join gmv2 on gmv2.expert_id = orders.expert_id and gmv2.store_id = orders.store_id " +
            "where orders.delete_flag = 0 and orders.expert_id = #{expertId} " +
            "order by actualGmv desc ")
    List<LifeUserExpertOrderVo> getCooperateStore(Integer expertId);

    @Select("select c.store_type as storeType, sum(a.purchase_amount ) as amount, d.dict_detail as storeTypeStr, count(a.id) as typeCount from life_user_expert_purchase a " +
            "left join life_user_expert_order b on a.order_id = b.id  and b.delete_flag = 0 " +
            "left join store_info c on b.store_id = c.id and c.delete_flag = 0 " +
            "left join store_dictionary d on c.store_type = d.dict_id and d.type_name ='storeType' and d.delete_flag = 0 " +
            "where a.delete_flag = 0 and b.expert_id = #{expertId} GROUP BY c.store_type")
    List<ExpertGoodsTypeVo> greatGoodsType(@Param("expertId") Integer expertId);


    @Select("with fans as ( " +
            "    select followed_user_type, followed_ref_id, count(id) num " +
            "    from life_fans " +
            "    where delete_flag = 0 and followed_user_type = 1 " +
            "    group by followed_user_type, followed_ref_id " +
            ") " +
            " select expert.id, expert.expert_code, expert.expert_type, user.id AS user_id, user.user_image, user.user_name, user.user_phone, ifnull(fans.num, 0) fansNum " +
            " from life_user_expert expert " +
            " left join life_user user on user.id = expert.user_id " +
            " left join fans on fans.followed_user_type = 1 and fans.followed_ref_id = user.id  " +
            " where expert.delete_flag = 0 and user.delete_flag = 0 " +
            " ORDER BY fansNum DESC")
    List<LifeUserExpertVo> getRankList();

    @Select("with fans as ( " +
            "    select followed_user_type, followed_ref_id, count(id) num " +
            "    from life_fans " +
            "    where delete_flag = 0 and followed_user_type = 1 " +
            "    group by followed_user_type, followed_ref_id " +
            "), " +  // 这里添加逗号分隔不同的 CTE
            "works as ( " +
            "    select expert_id, count(id) worksNum, sum(like_count) likeNum, sum(play_count) playNum " +
            "    from life_user_expert_works " +
            "    where delete_flag = 0 " +
            "    group by expert_id " +
            ") " +  // 去掉多余的逗号
            " select expert.id, expert.expert_code, expert.expert_type, user.user_image, user.user_name, user.jianjie, user.user_phone, ifnull(works.worksNum, 0) worksNum, ifnull(works.likeNum, 0) likeNum, " +
            "ifnull(fans.num, 0) fansNum " +
            " from life_user_expert expert " +
            " left join life_user user on user.id = expert.user_id " +
            " left join fans on fans.followed_user_type = 1 and fans.followed_ref_id = user.id  " +
            " left join works on works.expert_id = expert.id " + // 添加与 works 表的连接条件
            " where expert.id = #{expertId}")
    List<LifeUserExpertVo> getExpertDetails(Integer expertId);

    @Select("select expert.id, expert.expert_code, user.user_name, user.province, user.city, user.district, user.user_brithday, user.user_sex " +
            " from life_user_expert expert " +
            " left join life_user user on user.id = expert.user_id " +
            " left join life_fans fans on fans.followed_user_type = 1 and fans.followed_ref_id = user.id and fans.delete_flag = 0 " +
            "where expert.delete_flag = 0 and user.delete_flag = 0 and fans.delete_flag = 0" +
            " and expert.id = #{expertId}")
    List<LifeUserExpertVo> getFanPortrait(Integer expertId);

    @Select("select expert.id, expert.expert_code, " +
            "expert.delete_flag, expert.created_time, expert.expert_status,expert.created_user_id, expert.updated_time, expert.updated_user_id," +
            "user.user_name, user.province, user.city, user.district, user.user_brithday, user.user_sex " +
            " from life_user_expert expert " +
            " left join life_user user on user.id = expert.user_id " +
            " left join life_fans fans on fans.followed_user_type = 1 and fans.followed_ref_id = user.id and fans.delete_flag = 0 " +
            "where expert.delete_flag = 0 and user.delete_flag = 0" +
            " and user.id = #{userId}")
    List<LifeUserExpertVo> ifExpert(String userId);


    @Select("with fans as ( " +
            "    select followed_user_type, followed_ref_id, count(id) num " +
            "    from life_fans " +
            "    where delete_flag = 0 and followed_user_type = 1 " +
            "    group by followed_user_type, followed_ref_id " +
            "), " +  // 这里添加逗号分隔不同的 CTE
            "works as ( " +
            "    select expert_id, count(id) worksNum, sum(like_count) likeNum, sum(play_count) playNum " +
            "    from life_user_expert_works " +
            "    where delete_flag = 0 " +
            "    group by expert_id " +
            ") " +  // 去掉多余的逗号
            " select expert.id, expert.expert_code, user.user_image, user.real_name, user.id_card, user.nationality, user.english_name, user.expiration_date, user.user_name, user.jianjie, user.user_phone, " +
            "ifnull(works.worksNum, 0) worksNum, ifnull(works.likeNum, 0) likeNum, " +
            "ifnull(fans.num, 0) fansNum " +
            " from life_user_expert expert " +
            " left join life_user user on user.id = expert.user_id " +
            " left join fans on fans.followed_user_type = 1 and fans.followed_ref_id = user.id  " +
            " left join works on works.expert_id = expert.id " + // 添加与 works 表的连接条件
            " where expert.id = #{expertId}")
    LifeUserExpertVo getTalentIdentityInformation(Integer expertId);

    @Select("select * from life_user_expert where user_id = #{userId} and delete_flag = 1")
    LifeUserExpert selectIsExpert(Integer userId);

    @Update("update life_user_expert set expert_code = #{expertCode},expert_status = #{expertStatus},delete_flag = '0'" +
            "where id = #{id}")
    Boolean updateExpert(LifeUserExpert lifeUserExpert);

    @Select(
            "select expert.* ,user.*" +
                    "from life_user_expert expert " +
                    "join life_user user on user.id = expert.user_id " +
                    "where expert.expert_status = 1 and orders.expert_id = #{expertId} ")

    IPage<LifeUserExpertVo> getApplicationExpertList(IPage<LifeUserExpertVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeUserExpertVo> wrapper);

}
