package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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 shop.alien.entity.store.LifeUserOrder;
import shop.alien.entity.store.vo.LifeUserOrderVo;

import java.util.List;
import java.util.Map;

/**
 * 用户订单
 */
@Mapper
public interface LifeUserOrderMapper extends BaseMapper<LifeUserOrder> {
    @Select("select store_id, ifnull(round(sum(uorder.price) / count(1), 0), 0) avg_price\n" +
            "from life_user_order uorder \n" +
            "where uorder.status = '1' and uorder.delete_flag = 0\n" +
            "group by store_id")
    List<Map<String, Object>> allStoreAvgPrice();

    @Select("SELECT quan_id quanId, count(id) count FROM life_user_order ${ew.customSqlSegment}")
    List<LifeUserOrderVo> getQuanCount(@Param(Constants.WRAPPER) QueryWrapper<LifeUserOrderVo> queryWrapper);

    LifeUserOrderVo queryUserOrderDetail(@Param("orderId") String orderId,@Param("position") String position);

    @Select("with total_coupon as(\n" +
            "-- 团购\n" +
            "\tselect id coupon_id,2 coupon_type,lgbm.group_name coupon_name, SUBSTRING_INDEX(image_id, ',', 1) AS image_id,effective_date_type,effective_date_value \n" +
            "from life_group_buy_main lgbm where lgbm.delete_flag = 0\n" +
            "\tunion all\n" +
            "-- 代金券\n" +
            "\tselect id coupon_id,1 coupon_type,lc.name coupon_name, SUBSTRING_INDEX(image_path, ',', 1) AS image_id,0 effective_date_type,expiration_date effective_date_value \n" +
            "from life_coupon lc where lc.delete_flag = 0\n" +
            ")\n" +
            "select luo.id,luo.buy_time,luo.status,luo.price,luo.final_price,luo.user_id,luo.store_id,luo.order_no,luo.pay_time,luo.cancel_time,luo.finish_time,\n" +
            "si.store_name,si.commission_rate,si.business_section store_type,\n" +
            "count(ocm.coupon_code) coupon_count,\n" +
            "simg.img_url,\n" +
            "lu.user_phone,\n" +
            "tc.*\n" +
            "from life_user_order luo\n" +
            "left join store_info si on si.id = luo.store_id and si.delete_flag = 0 -- 查询店铺相关 \n" +
            "left join life_user lu on lu.id = luo.user_id and lu.delete_flag = 0 -- 查询用户相关 \n" +
            "left join order_coupon_middle ocm on ocm.order_id = luo.id and ocm.delete_flag = 0\n" +
            "inner join total_coupon tc on tc.coupon_id = ocm.coupon_id and tc.coupon_type = luo.coupon_type\n" +
            "left join  store_img simg on simg.id = tc.image_id and simg.delete_flag = 0 \n" +
            "${ew.customSqlSegment}")
    IPage<LifeUserOrderVo> queryUserOrderList(IPage<LifeUserOrderVo> brandedPage,@Param(Constants.WRAPPER) QueryWrapper<LifeUserOrderVo> lifeUserOrderQueryWrapper);

    /**
     * 查询过期退款订单
     * @param selectWrapper
     * @return
     */
    List<LifeUserOrderVo> selectRefundList(LambdaUpdateWrapper<LifeUserOrder> selectWrapper);
}
