package com.huashe.park.application.web.controller.bd;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huashe.common.domain.AjaxResult;
import com.huashe.park.core.service.IBdDevcTrailUwbService;
import com.huashe.park.domain.entity.BdDevcTrailUwb;
import com.huashe.park.infrastructure.cfg.forest.UWBForestCfg;
import com.huashe.park.infrastructure.uwb.HolidayWebService;
import com.huashe.park.infrastructure.uwb.UWBWebService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 室内坐标定位Controller
 *
 * @author ruoyi
 * @date 2024-10-16
 */
@RestController
@CrossOrigin
@RequestMapping("/bd/devcTrailUwb")
@Anonymous
public class BdDevcTrailUwbController extends BaseController {
    @Autowired
    private IBdDevcTrailUwbService bdDevcTrailUwbService;

    // @Autowired
    // private MqttClientTemplate client;

    @Autowired
    private UWBForestCfg uwbForestCfg;

    @Autowired
    private HolidayWebService holidayWebService;

    @Resource
    private UWBWebService uwbWebService;

    /**
     * 查询室内坐标定位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BdDevcTrailUwb bdDevcTrailUwb) {
        // Map map = uwbWebService.exchangeToken(new HashMap<String, Object>() {
        // {
        // {
        // put("username", uwbForestCfg.getUwbUsr());
        // put("password", uwbForestCfg.getUwbPwd());
        // put("isfresh", 1);
        // }
        // }
        // });
        // Map yearHoliday = holidayWebService.getYearHoliday("2025");
        startPage();
        List<BdDevcTrailUwb> list = bdDevcTrailUwbService.selectBdDevcTrailUwbList(bdDevcTrailUwb);
        return getDataTable(list);
    }

    /**
     * 导出室内坐标定位列表
     */
    @Log(title = "室内坐标定位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdDevcTrailUwb bdDevcTrailUwb) {
        List<BdDevcTrailUwb> list = bdDevcTrailUwbService.selectBdDevcTrailUwbList(bdDevcTrailUwb);
        ExcelUtil<BdDevcTrailUwb> util = new ExcelUtil<BdDevcTrailUwb>(BdDevcTrailUwb.class);
        util.exportExcel(response, list, "室内坐标定位数据");
    }

    // @PostMapping("/point")
    // @Anonymous
    // public void pushPoint(@RequestBody BdDevcTrailUwb bdDevcTrailUwb) {
    // Map<String, Object> map = new HashMap() {
    // {
    // put("latitude", bdDevcTrailUwb.getLat());
    // put("longitude", bdDevcTrailUwb.getLng());
    // put("srcTimestamp", DateTimeUtil.timestampMillis());
    // put("key", bdDevcTrailUwb.getDevcKey());
    // put("deviceId", bdDevcTrailUwb.getDevcKey());
    // }
    // };
    // client.publish(String.format(BDConst.MQTT_TOPIC.DEVICE_LOCATION_TOPIC, bdDevcTrailUwb.getDevcKey()),
    // JSON.toJSONBytes(map));
    // }

    /**
     * 获取室内坐标定位详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bdDevcTrailUwbService.selectBdDevcTrailUwbById(id));
    }

    @GetMapping(value = "/token/{id}")
    @Anonymous
    public AjaxResult getTokenInfo() {
        return success(bdDevcTrailUwbService.uwbOAuth());
    }

    /**
     * 新增室内坐标定位
     */
    @Log(title = "室内坐标定位", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody BdDevcTrailUwb bdDevcTrailUwb) {
        return toAjax(bdDevcTrailUwbService.insertBdDevcTrailUwb(bdDevcTrailUwb));
    }

    /**
     * 修改室内坐标定位
     */
    @Log(title = "室内坐标定位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BdDevcTrailUwb bdDevcTrailUwb) {
        return toAjax(bdDevcTrailUwbService.updateBdDevcTrailUwb(bdDevcTrailUwb));
    }

    /**
     * 删除室内坐标定位
     */
    @Log(title = "室内坐标定位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bdDevcTrailUwbService.deleteBdDevcTrailUwbByIds(ids));
    }

}
