package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.StoreInfo;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.entity.store.vo.StoreLicenseInfoVo;
import shop.alien.entity.store.vo.StoreMainInfoVo;

import java.util.List;

/**
 * 门店信息 Mapper 接口
 *
 * @author ssk
 * @since 2024-12-05
 */
@Mapper
public interface StoreInfoMapper extends BaseMapper<StoreInfo> {

    @Select("SELECT a.*, b.dict_detail businessStatusStr, c.dict_detail storeAreaStr FROM store_info a " +
            "left join store_dictionary b on a.business_status = b.dict_id and b.type_name='businessStatus' " +
            "left join store_dictionary c on a.store_area = c.dict_id and c.type_name='storeArea' where a.id = #{id}")
    StoreMainInfoVo getStoreInfo(Integer id);

    @Select("select a.*, b.name store_contact, b.phone store_phone, b.password, img.img_url entranceImage, dict.dict_detail storeTypeStr, " +
            "( " +
            " select ifnull(round(avg(score), 1), 0) " +
            " from store_evaluation eval " +
            " where eval.store_id = a.id and eval.delete_flag = 0 " +
            ") score " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id and b.delete_flag = 0 " +
            "left join store_img img on img.store_id = a.id and img.img_type = 1 and img.delete_flag = 0 " +
            "left join store_dictionary dict on dict.type_name = 'storeType' and dict.dict_id = a.store_type and dict.delete_flag = 0 " +
            " ${ew.customSqlSegment}")
    StoreInfoVo getStoreInfoVoOne(@Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper);

    @Select("with menu as (select store_id,GROUP_CONCAT(dish_name) dish_name from store_menu GROUP BY store_id) " +
            "select a.*, b.name store_contact, b.phone store_phone, b.password, c.dish_name, a.store_type, img.img_url entranceImage, d.img_url, dict.dict_detail storeTypeStr, dict2.dict_detail businessStatusStr " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id  AND b.delete_flag = 0 " +
            "left join menu c on a.id = c.store_id " +
            "left join store_img img on img.store_id = a.id and img.img_type = 1 and img.delete_flag = 0 " +
            "left join store_dictionary dict on dict.type_name = 'storeType' and dict.dict_id = a.store_type and dict.delete_flag = 0 " +
            "left join store_dictionary dict2 on dict2.type_name = 'businessStatus' and a.business_status = dict2.dict_id and dict2.delete_flag = 0 " +
            "left join store_img d on d.store_id = a.id and d.img_type = 10 and d.delete_flag = 0" +
            " ${ew.customSqlSegment}")
    List<StoreInfoVo> getStoreInfoVoList(@Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper);

    @Select("with menu as (select store_id,GROUP_CONCAT(dish_name) dish_name from store_menu GROUP BY store_id) " +
            "select a.*, b.name store_contact, b.phone store_phone, b.password, c.dish_name, a.store_type, img.img_url entranceImage, d.img_url, dict.dict_detail storeTypeStr, dict2.dict_detail businessStatusStr," +
            "            ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(a.store_position, ',', ' '), ')' ))) / 1000, 2) dist " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id  AND b.delete_flag = 0 " +
            "left join menu c on a.id = c.store_id " +
            "left join store_img img on img.store_id = a.id and img.img_type = 1 and img.delete_flag = 0 " +
            "left join store_dictionary dict on dict.type_name = 'storeType' and dict.dict_id = a.store_type and dict.delete_flag = 0 " +
            "left join store_dictionary dict2 on dict2.type_name = 'businessStatus' and a.business_status = dict2.dict_id and dict2.delete_flag = 0 " +
            "left join store_img d on d.store_id = a.id and d.img_type = 10 and d.delete_flag = 0" +
            " ${ew.customSqlSegment}")
    List<StoreInfoVo> getStoreInfoVoListNew(@Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper,  @Param("position") String position);

    /**
     * web端-门店列表
     *
     * @param page         分页
     * @param queryWrapper 查询条件
     * @return IPage<StoreInfoVo>
     */
    @Select("select a.*, img.img_url entranceImage,img1.img_url foodLicenceImageUrl,b.name store_contact, b.phone store_phone, b.id_card idCard, b.password, c.dict_detail store_status_str, d.dict_detail business_status_str, e.dict_detail store_type_str,  " +
            "( " +
            " select ifnull(round(avg(score), 1), 0) " +
            " from store_evaluation eval " +
            " where eval.store_id = a.id and eval.delete_flag = 0 " +
            ") score " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id and a.delete_flag = 0 and b.delete_flag = 0 " +
            "left join store_img img on img.store_id = a.id and img.img_type = 1 and img.delete_flag = 0 " +
            "left join store_img img1 on img1.store_id = a.id and img1.img_type = 24 and img1.delete_flag = 0 " +
            "left join store_dictionary c on a.store_status = c.dict_id and c.type_name = 'storeState' and c.delete_flag = 0 " +
            "left join store_dictionary d on a.business_status = d.dict_id and d.type_name = 'businessStatus' and d.delete_flag = 0 " +
            "left join store_dictionary e on e.type_name = 'storeType' and e.dict_id = a.store_type and e.delete_flag = 0 " +
            "${ew.customSqlSegment}")
    IPage<StoreInfoVo> getStoreInfoVoPage(IPage<StoreInfoVo> page, @Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper);

    /**
     * web端-门店列表
     *
     * @param page         分页
     * @param queryWrapper 查询条件
     * @return IPage<StoreInfoVo>
     */
    @Select("select a.*, " +
            "b.name store_contact, " +
            "b.phone store_phone, " +
            "b.id_card idCard, " +
            "b.password, " +
            "c.dict_detail store_status_str, " +
            "d.dict_detail business_status_str, " +
            "e.dict_detail store_type_str,  " +
            "( " +
            " select ifnull(round(avg(score), 1), 0) " +
            " from store_evaluation eval " +
            " where eval.store_id = a.id and eval.delete_flag = 0 " +
            ") score " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id and a.delete_flag = 0 and b.delete_flag = 0 " +
            "left join store_dictionary c on a.store_status = c.dict_id and c.type_name = 'storeState' and c.delete_flag = 0 " +
            "left join store_dictionary d on a.business_status = d.dict_id and d.type_name = 'businessStatus' and d.delete_flag = 0 " +
            "left join store_dictionary e on e.type_name = 'storeType' and e.dict_id = a.store_type and e.delete_flag = 0 " +
            "${ew.customSqlSegment}")
    IPage<StoreInfoVo> getNewStoreInfoVoPage(IPage<StoreInfoVo> page, @Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper);

    @Select("select info.store_name, user.phone, img.img_url " +
            "from store_info info " +
            "join store_user user on user.store_id = info.id " +
            "left join store_img img on img.store_id = info.id and img.img_type = '10' and img.delete_flag = 0 " +
            "where info.id = #{id} and info.delete_flag = 0 and user.delete_flag = 0 ")
    StoreInfoVo getStoreUserInfo(Integer id);

    @Select("select a.*, b.name store_contact, b.phone store_phone, b.id_card idCard, b.password, c.dict_detail store_status_str, d.dict_detail business_status_str " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id " +
            "left join store_dictionary c on a.store_status = c.dict_id and c.type_name = 'storeState' " +
            "left join store_dictionary d on a.business_status = d.dict_id and d.type_name = 'businessStatus' ${ew.customSqlSegment}")
    List<StoreInfoVo> getStoreInfoVo(@Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper);

    @Select("select * from store_info ${ew.customSqlSegment}")
    List<StoreInfo> getList(@Param(Constants.WRAPPER) LambdaQueryWrapper<StoreInfo> queryWrapper);

    @Select("with menu as (select store_id,GROUP_CONCAT(dish_name) dish_name from store_menu GROUP BY store_id) " +
            "select a.*," +
            "            ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(a.store_position, ',', ' '), ')' ))) / 1000, 2) dist" +
            ", b.name store_contact, b.phone store_phone, b.password, c.dish_name,  img.img_url entranceImage, d.img_url, dict.dict_detail storeTypeStr, dict2.dict_detail businessStatusStr " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id  AND b.delete_flag = 0 " +
            "left join menu c on a.id = c.store_id " +
            "left join store_img img on img.store_id = a.id and img.img_type = 1 and img.delete_flag = 0 " +
            "left join store_dictionary dict on dict.type_name = 'storeType' and dict.dict_id = a.store_type and dict.delete_flag = 0 " +
            "left join store_dictionary dict2 on dict2.type_name = 'businessStatus' and a.business_status = dict2.dict_id and dict2.delete_flag = 0 " +
            "left join store_img d on d.store_id = a.id and d.img_type = 10 and d.delete_flag = 0 ${ew.customSqlSegment}")
    IPage<StoreInfoVo> getPageForDistance(IPage<StoreInfoVo> page,  @Param("position") String position,@Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper);


    @Select("SELECT a.* FROM store_info a " +
            "left join store_user b on a.id =b.store_id  where b.phone = #{phoneIdNew} limit 1")
    StoreMainInfoVo getStoreNameByPhone(String phoneIdNew);

    @Select(
            "select " +
            "            ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(store_position, ',', ' '), ')' ))) / 1000, 1) dist " +
            "from store_info where id = #{storeId}"
            )
    Double getStoreDistance(@Param("position") String position,@Param("storeId") Integer storeId);


    /**
     * 更多推荐（用户端）
     *
     * @param queryWrapper 查询条件
     * @param position 经纬度位置（格式：经度,纬度）
     * @return List<StoreInfoVo>
     */
    @Select("select a.*,b.name store_contact, b.phone store_phone, b.id_card idCard, b.password, c.dict_detail store_status_str, d.dict_detail business_status_str, e.dict_detail store_type_str,  " +
            "( " +
            " select ifnull(round(avg(score), 1), 0) " +
            " from store_evaluation eval " +
            " where eval.store_id = a.id and eval.delete_flag = 0 " +
            ") score, " +
            "ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(a.store_position, ',', ' '), ')' )))) distance3 " +
            "from store_info a " +
            "left join store_user b on a.id = b.store_id and a.delete_flag = 0 and b.delete_flag = 0 " +
            "left join store_dictionary c on a.store_status = c.dict_id and c.type_name = 'storeState' and c.delete_flag = 0 " +
            "left join store_dictionary d on a.business_status = d.dict_id and d.type_name = 'businessStatus' and d.delete_flag = 0 " +
            "left join store_dictionary e on e.type_name = 'storeType' and e.dict_id = a.store_type and e.delete_flag = 0 " +
            "${ew.customSqlSegment} " +
            "and a.store_position is not null and a.store_position != '' " +
            "and a.head_img_status = 1 " +
            "and exists (select 1 from contract_bundle cb where cb.subject_type = 'store' and cb.subject_id = a.id and cb.status = '已签署') " +
            "order by distance3 asc")
    List<StoreInfoVo> getMoreRecommendedStores(@Param(Constants.WRAPPER) QueryWrapper<StoreInfoVo> queryWrapper, @Param("position") String position);

    /**
     * 门店证照查询（分页）
     *
     * @param page            分页对象
     * @param storeName       门店名称（模糊）
     * @param storeContact    门店联系人（模糊）
     * @param storeTel        门店电话（模糊）
     * @param businessSection 经营板块
     * @param imgType         证照图片类型（14：营业执照；35：其他资质证明）
     * @param states          证照状态
     * @param startSubmitDate 提交开始时间（yyyy-MM-dd HH:mm:ss）
     * @param endSubmitDate   提交结束时间（yyyy-MM-dd HH:mm:ss）
     * @return 证照分页结果
     */
    IPage<StoreLicenseInfoVo> getStoreLicensePage(IPage<StoreLicenseInfoVo> page,
                                                  @Param("storeName") String storeName,
                                                  @Param("storeContact") String storeContact,
                                                  @Param("storeTel") String storeTel,
                                                  @Param("businessSection") String businessSection,
                                                  @Param("imgType") Integer imgType,
                                                  @Param("states") String states,
                                                  @Param("startSubmitDate") String startSubmitDate,
                                                  @Param("endSubmitDate") String endSubmitDate);
}
