package com.hdkj.lt.bf.controller;


import com.hdkj.lt.bf.entity.dto.CityDTO;
import com.hdkj.lt.bf.entity.dto.FeederIdDTO;
import com.hdkj.lt.bf.entity.dto.StruReqDTO;
import com.hdkj.lt.bf.entity.dto.StruTreeRespDTO;
import com.hdkj.lt.bf.entity.vo.FeederSwitchInfoVO;
import com.hdkj.lt.bf.entity.vo.StruTreeVO;
import com.hdkj.lt.bf.service.DwdShbDsSwitchBaseService;
import com.hdkj.lt.bf.service.StruService;
import com.hdkj.lt.core.mvc.BaseController;
import com.hdkj.lt.core.sys.service.DwdShbDsFeederBaseService;
import com.hdkj.lt.core.sys.service.ISysStruService;
import com.hdkj.hussar.ApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author luorui
 * @date 2025/5/12 10:35
 *
 * 组织机构查询控制器
 */
@RestController
@RequestMapping("/stru")
@Validated
public class StruController extends BaseController {

    @Autowired
    private StruService struService;

    @Autowired
    private ISysStruService sysStruService;

    @Autowired
    DwdShbDsFeederBaseService dwdShbDsFeederBaseService;

    @Autowired
    DwdShbDsSwitchBaseService dwdShbDsSwitchBaseService;

    /**
     * 查询组织列表
     * @param struReqDTO
     * @return
     */
    @PostMapping("/getStruTree")
    @ApiOperation(value = "获取组织机构和线路")
    public ApiResponse<StruTreeRespDTO> getStruTree(@RequestBody StruReqDTO struReqDTO) {
//        StruTreeRespDTO respDTO = struService.getStruTree(struReqDTO.getStruId());
        StruTreeRespDTO respDTO = struService.getStruTreeFromCache(struReqDTO.getStruId());
        return ApiResponse.success(respDTO);
    }

    /**
     * 查询用户可见的地市信息
     * @param struReqDTO
     * @return
     */
    @PostMapping("/getCityListId")
    @ApiOperation(value = "获取组织机构和线路")
    public ApiResponse<List<CityDTO> > getCityListId(@RequestBody StruReqDTO struReqDTO) {
        List<CityDTO> cityDTOList= struService.getCityListId(struReqDTO.getStruId());
        return ApiResponse.success(cityDTOList);
    }

    /**
     * 查询省，市，区县的组织树
     * @param
     * @return
     */
    @PostMapping("/getStruList")
    @ApiOperation(value = "查询省，市，区县的组织树")
    public ApiResponse<List<StruTreeVO>> getStruList() {
        List<StruTreeVO> struTree= struService.getStruList();
        return ApiResponse.success(struTree);
    }

    /**
     * 刷新组织结构树缓存
     * @param
     * @return
     */
    @PostMapping("/refreshStruTreeCache")
    @ApiOperation(value = "刷新组织结构树缓存")
    public ApiResponse<String> refreshStruTreeCache() {
        sysStruService.refreshStruTreeByCache();
        return ApiResponse.success("刷新组织树缓存成功");
    }


    /**
     * 刷新线路缓存
     * @param
     * @return
     */
    @PostMapping("/refreshAllFeederCache")
    @ApiOperation(value = "刷新线路缓存")
    public ApiResponse<String> refreshAllFeederCache() {
        dwdShbDsFeederBaseService.refreshAllFeederCache();
        return ApiResponse.success("刷新线路缓存成功");
    }

    /**
     * 刷新线路缓存
     * @param
     * @return
     */
    @GetMapping("/refreshFeederCacheById")
    @ApiOperation(value = "刷新单个线路的文件缓存")
    public ApiResponse<String> refreshFeederCacheById(@NotBlank(message = "线路ID不能为空") String feederId) {
        dwdShbDsFeederBaseService.refreshFeederCacheById(feederId);
        return ApiResponse.success(String.format("刷新【%s】线路缓存成功", feederId));
    }

    @PostMapping("/getSwitchListByFeederId")
    @ApiOperation(value = "根据feeder获取开关列表")
    public ApiResponse<List<FeederSwitchInfoVO>> getSwitchListByFeederId(@RequestBody FeederIdDTO feederIdDTO) {
        return ApiResponse.success(dwdShbDsSwitchBaseService.getSwitchListByFeederId(feederIdDTO.getFeederId()));
    }
}
