/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.dto.ShopHeadInfoDto;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.UserCollectionShop;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserCollectionShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;


/**
 * @author lgh on 2018/08/29.
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺相关接口")
@AllArgsConstructor
public class ShopDetailController {

    private final ShopDetailService shopDetailService;

    private final UserCollectionShopService userCollectionShopService;

    @GetMapping("/headInfo")
    @ApiOperation(value = "店铺头部信息", notes = "获取的店铺头部信息")
    public ResponseEntity<ShopHeadInfoDto> getShopHeadInfo(Long shopId) {
        ShopHeadInfoDto shopHeadInfoDto = new ShopHeadInfoDto();
        // 粉丝数量
        int fansCount = userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>().eq(UserCollectionShop::getShopId, shopId));
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        if (!Objects.equals(shopDetail.getShopStatus(), 1)) {
            shopHeadInfoDto.setShopStatus(shopDetail.getShopStatus());
            return ResponseEntity.ok(shopHeadInfoDto);
        }
        shopHeadInfoDto.setShopId(shopId);
        shopHeadInfoDto.setShopLogo(shopDetail.getShopLogo());
        shopHeadInfoDto.setShopName(shopDetail.getShopName());
        shopHeadInfoDto.setIntro(shopDetail.getIntro());
        shopHeadInfoDto.setFansCount(fansCount);
        shopHeadInfoDto.setTel(shopDetail.getTel());
        shopHeadInfoDto.setLat(shopDetail.getShopLat());
        shopHeadInfoDto.setLon(shopDetail.getShopLng());
        shopHeadInfoDto.setThirdPartyCode(shopDetail.getThirdPartyCode());
        return ResponseEntity.ok(shopHeadInfoDto);
    }

    @GetMapping("/hotShops")
    @ApiOperation(value = "热门店铺", notes = "热门店铺")
    public ResponseEntity<List<ShopHeadInfoDto>> hotShops() {
        List<ShopHeadInfoDto> hotShopsHead = shopDetailService.listHotShopsHead();

        return ResponseEntity.ok(hotShopsHead);
    }


    @GetMapping("/searchShops")
    @ApiOperation(value = "搜索店铺", notes = "根据店铺名称搜索店铺")
    public ResponseEntity<Page<ShopHeadInfoDto>> searchShops(PageParam<ShopHeadInfoDto> page, ShopHeadInfoDto shopHeadInfoDto) {
        Page<ShopHeadInfoDto> hotShopsHeadPage = shopDetailService.searchShops(page, shopHeadInfoDto);

        return ResponseEntity.ok(hotShopsHeadPage);
    }

    @GetMapping("/neighborShop")
    @ApiOperation("根据经纬度获取最近店铺")
    public ResponseEntity<ShopHeadInfoDto> neighborShop(@RequestParam("lat") String lat,
                                                        @RequestParam("lon") String lon, @RequestParam("channelId") Long channelId) {
        return shopDetailService.neighborShop(lat, lon, channelId);
    }

    @GetMapping("/addrShop")
    @ApiOperation("根据地址获取最近店铺")
    public ResponseEntity<List<ShopHeadInfoDto>> neighborShopByAddr(@RequestParam("channelId") Long channelId,
                                                                    @RequestParam(value = "addrId", required = false) Long addrId,
                                                                    @RequestParam(value = "lat", required = false) String lat,
                                                                    @RequestParam(value = "lon", required = false) String lon) {
        if (null == addrId) {
            if (null == lat || null == lon) throw new GlobalException("当地址id为空时，经纬度不能为空");
            return shopDetailService.neighborShopList(lat, lon, channelId);
        }
        return shopDetailService.neighborShopByAddr(addrId, channelId);
    }

}
