package com.aidex.web.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.constant.Constants;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.enums.BusinessType;
import com.aidex.system.domain.SysConfig;
import com.aidex.system.service.SysConfigService;
import com.alibaba.fastjson2.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 航线保存查询-Redis
 * @author ChenSir
 * @email 914769835
 * @date 2024-03-16
 */
@RestController
@RequestMapping("/system/sysRouter")
public class SysRouteController extends BaseController {


    @Autowired
    private SysConfigService configService;


    /**
     * 获取码头管理详细信息
     */
    @GetMapping
    public R<JSONArray> detail() {
        JSONArray jsonArray = new JSONArray();
        SysConfig sysConfigRoute = configService.findConfigByKey(Constants.ROUTE_STORE_PREFIX);
        if (sysConfigRoute != null) {
            jsonArray = JSONArray.parseArray(sysConfigRoute.getConfigValue());
        }
        return R.data(jsonArray);
    }

    /**
     * 新增码头管理
     */
    @Log(title = "航线管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  JSONArray data) {
        SysConfig sysConfigCurrent = configService.findConfigByKey(Constants.ROUTE_STORE_PREFIX);
        if (sysConfigCurrent != null) {
            sysConfigCurrent.setConfigValue(data.toJSONString());
            return R.status(configService.save(sysConfigCurrent));
        } else {
            SysConfig sysConfigNew = new SysConfig();
            sysConfigNew.setConfigKey(Constants.ROUTE_STORE_PREFIX);
            sysConfigNew.setConfigName("航线数据");
            sysConfigNew.setConfigValue(data.toJSONString());
            sysConfigNew.setConfigType("Y");
            return R.status(configService.save(sysConfigNew));
        }
    }


    /**
     * 删除航线管理
     */
    @Log(title = "航线管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delRouteLine")
    public R remove() {
        SysConfig sysConfigCurrent = configService.findConfigByKey(Constants.ROUTE_STORE_PREFIX);
        if (sysConfigCurrent != null) {
            return R.status(configService.removeByConfigKey(Constants.ROUTE_STORE_PREFIX));
        } else {
            return R.status(true);
        }
    }






}
