package com.ruoyi.hezhi.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hezhi.domain.TbStudyCenter;
import com.ruoyi.hezhi.domain.vo.StudyCenterVO;
import com.ruoyi.hezhi.service.ITbStudyCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 学习中心Controller
 *
 * @author ruoyi
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/hezhi/studyCenter")
public class TbStudyCenterController extends BaseController
{
    @Autowired
    private ITbStudyCenterService tbStudyCenterService;

    /**
     * 查询学习中心列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:studyCenter:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbStudyCenter tbStudyCenter)
    {
        startPage();
        List<TbStudyCenter> list = tbStudyCenterService.selectTbStudyCenterList(tbStudyCenter);
        return getDataTable(list);
    }

    /**
     * 导出学习中心列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:studyCenter:export')")
    @Log(title = "学习中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbStudyCenter tbStudyCenter)
    {
        List<TbStudyCenter> list = tbStudyCenterService.selectTbStudyCenterList(tbStudyCenter);
        ExcelUtil<TbStudyCenter> util = new ExcelUtil<TbStudyCenter>(TbStudyCenter.class);
        util.exportExcel(response, list, "学习中心数据");
    }

    /**
     * 获取学习中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:studyCenter:query')")
    @GetMapping(value = "/{studyCenterId}")
    public AjaxResult getInfo(@PathVariable("studyCenterId") Long studyCenterId)
    {
        return success(tbStudyCenterService.selectTbStudyCenterByStudyCenterId(studyCenterId));
    }

    /**
     * 新增学习中心
     */
    @PreAuthorize("@ss.hasPermi('hezhi:studyCenter:add')")
    @Log(title = "学习中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbStudyCenter tbStudyCenter)
    {
        return toAjax(tbStudyCenterService.insertTbStudyCenter(tbStudyCenter));
    }

    /**
     * 修改学习中心
     */
    @PreAuthorize("@ss.hasPermi('hezhi:studyCenter:edit')")
    @Log(title = "学习中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbStudyCenter tbStudyCenter)
    {
        return toAjax(tbStudyCenterService.updateTbStudyCenter(tbStudyCenter));
    }

    /**
     * 删除学习中心
     */
    @PreAuthorize("@ss.hasPermi('hezhi:studyCenter:remove')")
    @Log(title = "学习中心", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studyCenterIds}")
    public AjaxResult remove(@PathVariable Long[] studyCenterIds)
    {
        return toAjax(tbStudyCenterService.deleteTbStudyCenterByStudyCenterIds(studyCenterIds));
    }

    /**
     * 获取学习中心列表
     * @return 结果
     */
    @GetMapping("/getAllStudyCenterList")
    public AjaxResult getAllStudyCenterList(){
        List<StudyCenterVO> studyCenterVOList = tbStudyCenterService.getAllStudyCenterList();
        return AjaxResult.success(studyCenterVOList);
    }


    //地址逆解析
    @GetMapping("/api/map/geocoder")
    public AjaxResult getGeocoder(@RequestParam String lat, @RequestParam String lng) {
        String key = "2OZBZ-WUCE7-SLKXP-HJVOW-3P6RF-WVB7H";
        String url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key;
        String json = HttpUtil.get(url);
        return AjaxResult.success(JSON.parseObject(json));
    }

}
