package com.ruoyi.biz.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.biz.domain.vo.ArStoreVo;
import com.ruoyi.biz.domain.bo.ArStoreBo;
import com.ruoyi.biz.service.IArStoreService;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.biz.controller.ProjectController.httpGetSend;

/**
 * 门店
 *
 * @author gwd
 * @date 2023-07-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/store")
public class ArStoreController extends BaseController {

    private final IArStoreService iArStoreService;

    /**
     * 查询门店列表
     */
    //@SaCheckPermission("biz:store:list")
    @GetMapping("/list")
    public TableDataInfo<ArStoreVo> list(ArStoreBo bo, PageQuery pageQuery) {
        return iArStoreService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出门店列表
     */
    //@SaCheckPermission("biz:store:export")
    @Log(title = "门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArStoreBo bo, HttpServletResponse response) {
        List<ArStoreVo> list = iArStoreService.queryList(bo);
        ExcelUtil.exportExcel(list, "门店", ArStoreVo.class, response);
    }

    /**
     * 获取门店详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("biz:store:query")
    @GetMapping("/{id}")
    public R<ArStoreVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iArStoreService.queryById(id));
    }

    /**
     * 新增门店
     */
    //@SaCheckPermission("biz:store:add")
    @Log(title = "门店", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArStoreBo bo) {
        return toAjax(iArStoreService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改门店
     */
    //@SaCheckPermission("biz:store:edit")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArStoreBo bo) {
        return toAjax(iArStoreService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除门店
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("biz:store:remove")
    @Log(title = "门店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iArStoreService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 获取门店列表（根据经纬度）
     */
    @GetMapping("/getStoreList")
    public R<List<ArStoreVo>> getStoreList(BigDecimal latitude,BigDecimal longitude) {
        String url = "https://apis.map.qq.com/ws/coord/v1/translate?locations="+ latitude +","+ longitude +"&type=1&key=D73BZ-YSVWJ-ABLFO-XGZ3J-G76IE-BJFMJ";
        String json = httpGetSend(url,"UTF-8");
        Map<String, Object> map = (Map<String, Object>) JSON.parse(json);
        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("locations");
        if(list.size()>0){
            return R.ok(iArStoreService.getStoreList((BigDecimal) list.get(0).get("lat"), (BigDecimal) list.get(0).get("lng")));
        }
        return R.ok(iArStoreService.getStoreList(latitude, longitude));
    }
}
