package com.lightinit.hsdatagateway.modules.api.gateway;

import com.lightinit.hsdatagateway.common.exceptions.BaseException;
import com.lightinit.hsdatagateway.common.utils.StringUtils;
import com.lightinit.hsdatagateway.modules.foreign.model.ReturnResult;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizRoute;
import com.lightinit.hsdatagateway.modules.gateway.service.BizRouteService;
import com.lightinit.hsdatagateway.modules.foreign.model.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("${adminPath}/api/route")
public class RouteApiController {

    @Autowired
    private BizRouteService bizRouteService;

    /**
     * 查询
     * @param bizRoute
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ReturnResult list(BizRoute bizRoute) {
        List<BizRoute> list = bizRouteService.findListByApi(bizRoute);
        return new ReturnResult(list);
    }


    /**
     * 新增
     * @param bizRoute
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResult add(@RequestBody BizRoute bizRoute) {
        if(StringUtils.isBlank(bizRoute.getRouteStatus())){
            return new ReturnResult("500","路由状态不能为空");
        }
        if(StringUtils.isBlank(bizRoute.getContextPath())){
            return new ReturnResult("500","上下文路径不能为空");
        }
        if(StringUtils.isBlank(bizRoute.getMappingUrl())){
            return new ReturnResult("500","映射url不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        //当前时间毫秒数
        String millis = String.valueOf(calendar.getTimeInMillis());
        //随机生成六位整数
        int newNum = (int) ((Math.random() * 9 + 1) * 100000);
        String random = String.valueOf(newNum);
        String routeId = "route" + millis + random;
        bizRoute.setRouteId(routeId);
        bizRoute.setCreatedAt(new Date());
        if(bizRouteService.isRepetition(bizRoute)){
            bizRouteService.save(bizRoute);
            return new ReturnResult(routeId,"success","200","保存路由管理成功");
        }
        return new ReturnResult("500","路由ID重复,操作失败");
    }

    /**
     * 根据id查询路由信息
     * @param bizRoute
     * @return
     */
    @RequestMapping(value = "/selectById",method = RequestMethod.GET)
    @ResponseBody
    public ReturnResult selectById(BizRoute bizRoute){
        if(Objects.isNull(bizRoute)){
            throw new BaseException("500","请求信息不能为空");
        }
        BizRoute route = bizRouteService.get(bizRoute);
        return new ReturnResult(route);
    }

    /**
     * 修改
     * @param bizRoute
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResult update(@RequestBody BizRoute bizRoute) {
        if(StringUtils.isBlank(bizRoute.getRouteId())){
            return new ReturnResult("500","路由ID不能为空");
        }
        if(StringUtils.isBlank(bizRoute.getRouteStatus())){
            return new ReturnResult("500","路由状态不能为空");
        }
        if(StringUtils.isBlank(bizRoute.getContextPath())){
            return new ReturnResult("500","上下文路径不能为空");
        }
        if(StringUtils.isBlank(bizRoute.getMappingUrl())){
            return new ReturnResult("500","映射url不能为空");
        }
        bizRouteService.updateByRouteId(bizRoute);
        return new ReturnResult();
    }


    /**
     * 删除
     * @param bizRoute
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ReturnResult delete(BizRoute bizRoute) {
        if(StringUtils.isBlank(bizRoute.getRouteId())){
            return new ReturnResult("500","路由ID不能为空");
        }
        bizRoute.setDeletedAt(new Date());
        bizRouteService.delete(bizRoute);
        return new ReturnResult();
    }


}
