package com.muyu.enterprise.controller;

import com.muyu.common.redis.service.RedisService;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.enterprise.cache.AllFenceCahceService;
import com.muyu.enterprise.cache.FenceCahceService;
import com.muyu.enterprise.cache.VehicleCacheService;
import com.muyu.enterprise.service.ElectService;
import com.muyu.enterprise.service.LanTypeService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.domain.Fence;
import com.muyu.domain.LanType;
import com.muyu.domain.req.FenceReq;
import com.muyu.domain.resp.FenceResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Security;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 电子围栏信息
 */
@RequestMapping("/elect")
@RestController
@Log4j2
@Tag(name = "电子围栏信息展示",description = "电子围栏信息展示")
public class ElectController extends BaseController {

    @Autowired
    private ElectService electService;
    @Autowired
    private LanTypeService lanTypeService;

    @Autowired
    private FenceCahceService fenceCahceService;

    @Autowired
    private AllFenceCahceService allFenceCahceService;


    /**
     * 获取电子围栏信息
     * @return
     */
    @RequestMapping(path = "/list",method = RequestMethod.POST)
    @Operation(description = "获取电子围栏信息列表")
    public Result<TableDataInfo<FenceResp>> showList(@RequestBody @Validated FenceReq req) {
        startPage();
        List<FenceResp> list = electService.selectFenceList(req);
        //将列表存到Redis
        for (FenceResp fenceResp : list) {
            allFenceCahceService.put(fenceResp.getCoordinates(),fenceResp);
        }
        return getDataTable(list);
    }

    /**
     * 添加电子围栏信息
     * @param fence
     * @return
     */
    @PostMapping("/add")
    @Operation(description = "添加电子围栏信息")
    public Result<String> add(@Validated @RequestBody Fence fence){
        boolean save = electService.save(fence);
        //获取用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获取租户唯一标识
        String databaseName = loginUser.getSysUser().getDatabaseName();
        //将信息存到Redis
        fenceCahceService.put(databaseName+fence.getCoordinates(),fence);
        return Result.success(save?"操作成功":"操作失败");
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Operation(description = "通过ID删除电子围栏信息")
    public Result<String> deleteById(@PathVariable("id")Long id){
        electService.removeById(id);
        return Result.success(null,"操作成功");
    }

    /**
     * 下拉框查询
     */
    @RequestMapping(path = "/showLanTypeList",method = RequestMethod.POST)
    @Operation(description = "获取电子围栏信息列表")
    public Result<List<LanType>> showLanTypeList(){
        List<LanType> list = lanTypeService.selectLanTypeList();
        return Result.success(list,"操作成功");
    }

    /**
     * 启用
     */
    @PostMapping("/fenceOn/{fenceId}")
    @Operation(description = "启用电子围栏")
    public Result<String> fenceOn(
            @Schema(title = "ID",defaultValue = "1",type = "Long",description = "修改信息所需的唯一条件")
            @PathVariable("fenceId") Long fenceId
            ){
        electService.updateFenceOn(fenceId);
        return Result.success(null,"操作成功");
    }

    /**
     * 禁用
     */
    @GetMapping("/fenceOff/{fenceId}")
    @Operation(description = "禁用电子围栏")
    public Result<Integer> fenceOff(
            @Schema(title = "ID",defaultValue = "1",type = "Long",description = "修改信息所需的唯一条件")
            @PathVariable("fenceId") Long fenceId){
        electService.updateFenceOff(fenceId);
        return Result.success(null,"操作成功");
    }

    /**
     * 地图信息回显
     */
    @GetMapping("/mapShow/{fenceId}")
    @Operation(description = "地图信息回显,围栏")
    public Result<List<Fence>> mapShow(@PathVariable("fenceId") Long fenceId){
        List<Fence> fences = electService.mapShow(fenceId);
        System.out.println(fences);
        return Result.success(fences);
    }

    /**
     * 批删
     */
    @DeleteMapping("/delMoreFence")
    @Operation(description = "批量删除电子围栏")
    public Result delMore(@RequestBody List<Long> fenceIds){
        //批量删除内容存到Redis
        electService.removeBatchByIds(fenceIds);
        return Result.success(null,"操作成功");
    }

    /**
     * 查询绑定的电子围栏（终版）
     */
    @GetMapping("/showBoundFence/{fenceGroupId}")
    @Operation(description = "查询绑定的电子围栏")
    public Result<List<Fence>> showBoundFence(@PathVariable("fenceGroupId") Long fenceGroupId){
        List<Fence> fences = electService.showFenceBound(fenceGroupId);
        return Result.success(fences);
    }

}
