package com.muyu.server.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.domain.database.ElectronicFence;
import com.muyu.common.domain.req.ElectroicFenceAddReq;
import com.muyu.common.domain.req.ElectroicFenceListReq;
import com.muyu.common.domain.req.ElectroicFenceUpdReq;
import com.muyu.common.domain.req.FenceWayReq;
import com.muyu.common.domain.resp.ElectronicFenceResp;
import com.muyu.server.service.ElectronicFenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子围栏
 * @author yuping
 * @Package：com.muyu.fence.controller
 * @Project：cloud-server
 * @name：FenceEtlController
 * @Date：2024/9/17 16:28
 */
@Log4j2
@Tag(name = "电子围栏",description = "电子围栏")
@RestController
@RequestMapping("/fence")
@RequiredArgsConstructor
public class ElectronicFenceController {

    @Autowired
    private ElectronicFenceService electronicFenceService;


    @PostMapping("/fenceArray")
    @Operation(summary = "查询所有可用的电子围栏",description = "查询所有可用的围栏")
    public Result<List<ElectronicFenceResp>> fenceArray() {
        System.out.println("=====>" + "hgfvhgjy");
        return Result.success(electronicFenceService.fenceArray());
    }

    @PostMapping("/fenceselectList")
    @Operation(summary = "列表",description = "列表")
    public Result<List<ElectronicFenceResp>> fenceselectList(@RequestBody ElectroicFenceListReq electroicFenceListReq) {
        System.out.println("=====>" + "hgfvhgjy");
        return Result.success(electronicFenceService.fenceselectList(electroicFenceListReq));
    }

    @PostMapping("/add")
    @Operation(summary = "添加电子围栏",description = "添加电子围栏")
    public Result AddFence(@RequestBody ElectroicFenceAddReq electroicFenceAddReq) {

        electronicFenceService.unquireFence(electroicFenceAddReq.getName());
        electronicFenceService.AddFence(electroicFenceAddReq);

        return Result.success();
    }

    @PostMapping("/upd/{id}")
    @Operation(summary = "修改电子围栏",description = "修改电子围栏")
    public Result UpdFence(@PathVariable("id") Long id, @RequestBody ElectroicFenceUpdReq electroicFenceUpdReq) {

        electronicFenceService.updateById(ElectronicFence.buildByElectronicUpd(electroicFenceUpdReq, () -> id));

        return Result.success();
    }

    @PostMapping("/findElectronicByid/{id}")
    @Operation(summary = "通过Id回显电子围栏信息",description = "通过Id回显电子围栏")
    public Result<ElectronicFence> findElectronicByid(@PathVariable("id") Long id) {

        ElectronicFence  electronicFence= electronicFenceService.findElectronicByid(id);
        return Result.success(electronicFence);
    }


//    @PostMapping("/delElectronById/{id}")
//    @Operation(description = "通过id删除围栏")
//    public Result delElectronById(@PathVariable("id") Long id) {
//
//        electronicFenceService.delElectronById(id);
//
//        return Result.success();
//    }

    /**
     * 电子围栏表信息表删除
     * @param electronicFenceId 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/{electronicFenceId}")
    @Operation(summary = "电子围栏表信息表删除",
            description = "通过ID删除电子围栏表信息")
    public Result<String> remove(@PathVariable("electronicFenceId") Long electronicFenceId)
    {
        boolean b = electronicFenceService.removeById(electronicFenceId);
        return Result.success(null, "操作成功");
    }

    @PostMapping("/open/{id}")
    @Operation(summary = "开启电子围栏",description = "开启电子围栏")
    public Result openFence(@PathVariable("id") Long id) {

        electronicFenceService.openFence(id);

        return Result.success();
    }

    @PostMapping("/close/{id}")
    @Operation(summary = "关闭电子围栏",description = "关闭电子围栏")
    public Result closeFence(@PathVariable("id") Long id) {

        electronicFenceService.closeFence(id);

        return Result.success();
    }

    @PostMapping("/setFenceWay")
    @Operation(summary = "设置电子围栏的位置",description = "设置电子围栏的位置")
    public Result setFenceWay(@RequestBody FenceWayReq fenceWayReq) {

        Long id = fenceWayReq.getId();

        String longitudeLatitude = fenceWayReq.getLongitudeLatitude();

        log.info("接收到的数据,:{}" + id + "====" + longitudeLatitude);

        electronicFenceService.setFenceWay(id, longitudeLatitude);

        return Result.success();

    }


}
