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 shop.alien.entity.store.StoreClockIn;
import shop.alien.entity.store.vo.StoreClockInVo;

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

/**
 * <p>
 * 店铺打卡 Mapper 接口
 * </p>
 *
 * @author ssk
 * @since 2025-05-15
 */
public interface StoreClockInMapper extends BaseMapper<StoreClockIn> {

    @Select("select clock.id, clock.user_id, clock.store_id, clock.permission, user.user_name, store.store_name,store.score_avg score, clock.like_count, store.store_position, clock.content, clock.created_time, clock.img_url clockImg, img.img_url storeImg, user.user_image userImg,clock.maybe_ai_content," +
            "store.administrative_region_district_name region, dict.dict_detail storeType, concat('user_', user.user_phone) phoneId, store.business_section,store.business_section_name,business_types_name,img1.img_url entranceImage," +
            "( " +
            " select ifnull(round(sum(uorder.price) / count(1), 0), 0) " +
            " from life_user_order uorder " +
            " where uorder.store_id = store.id and uorder.status = '1' and uorder.delete_flag = 0 " +
            ") perCapitaPrice, " +
            "( " +
            " select ifnull(round(avg(score), 1), 0) " +
            " from store_evaluation eval " +
            " where eval.store_id = store.id and eval.delete_flag = 0 " +
            ") score " +
            "from store_clock_in clock " +
            "join life_user user on user.id = clock.user_id " +
            "join store_info store on store.id = clock.store_id " +
            "left join store_img img on img.store_id = clock.store_id and img.img_type = '10' and img.delete_flag = 0 " +
            "left join store_img img1 on img1.store_id = clock.store_id and img1.img_type = '1' and img1.delete_flag = 0 " +
            "left join store_dictionary dict on dict.type_name = 'storeType' and store.store_type = dict.dict_id and dict.delete_flag = 0 " +
            "${ew.customSqlSegment} ")
    IPage<StoreClockInVo> getStoreClockInList(IPage<StoreClockIn> page, @Param(Constants.WRAPPER) QueryWrapper<StoreClockIn> queryWrapper);

    /**
     * 获取门店打卡次数
     *
     * @return list
     */
    @Select("SELECT store_id as storeId, count(store_id) as count FROM `store_clock_in` where delete_flag = 0 GROUP BY store_id ")
    List<Map<Integer, Integer>> getStoreClockInCount();
     /**
      * 获取所有店铺打卡次数（有图片并且设置为可见的）
      *
      * @return map
      */
    @Select("SELECT store_id as storeId, count(store_id) as count FROM `store_clock_in` where delete_flag = 0 and permission = 1 and img_url is not null GROUP BY store_id ")
    List<Map<Integer, Integer>> getStoreClockInWithCanLookCount();
}
