package com.sptg.gateway.controller.ad;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.entity.ThresholdConfig;
import com.sptg.ad.api.feigns.ThresholdConfigFeign;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.entity.Account;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *            
 *               
 * description:
 */
@Controller
@RequiredArgsConstructor
public class ThresholdConfigController {

    private final ThresholdConfigFeign thresholdConfigFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;

    @Security
    @GetMapping(value = "/ad/threshold_config/{type}")
    public ModelAndView manage(@PathVariable(value = "type")Integer type){
        ModelAndView modelAndView = new ModelAndView("ad/threshold-manage");
        modelAndView.addObject("type",type);
        return modelAndView;
    }

    @Security
    @ResponseBody
    @GetMapping(value = "/ad/threshold_config/pagingQuery")
    public ResultBean<Page> pagingQuery(ThresholdConfig dto,PageBean<ThresholdConfig, ThresholdConfig> pageBean){
        Assert.notNull(dto.getType(),"类型不能为空");
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgId(onLineUser.getRootOrgId());
        pageBean.setDto(dto);
        return ResultBean.ok(this.thresholdConfigFeign.pagingQuery(pageBean));
    }

    @Security
    @GetMapping(value = "/ad/threshold_config/add")
    public ModelAndView addConfig(Integer type){
        ModelAndView modelAndView = new ModelAndView("ad/threshold-add");
        modelAndView.addObject("type",type);
        return modelAndView;
    }

    @Security
    @ResponseBody
    @PostMapping(value = "/ad/threshold_config/add")
    public ResultBean addConfig(ThresholdConfig config){
        Assert.notNull(config.getType(),"类型不能为空");
        Assert.notNull(config.getValue(),"阈值不能为空");
        Assert.hasText(config.getRegion(),"区域不能为空");
        Assert.notNull(config.getRegionCode(),"区域编号不能为空");
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        config.setOrgId(onLineUser.getRootOrgId());
        return this.thresholdConfigFeign.addConfig(config);
    }

    @Security
    @GetMapping(value = "/ad/threshold_config/edit")
    public ModelAndView editConfig(Long id){
        Assert.notNull(id,"Id不能为空");
        ThresholdConfig config = this.thresholdConfigFeign.getById(id);
        Assert.notNull(config,"Id不存在");
        ModelAndView modelAndView = new ModelAndView("ad/threshold-edit");
        modelAndView.addObject("config",config);
        return modelAndView;
    }

    @Security
    @ResponseBody
    @PostMapping(value = "/ad/threshold_config/edit")
    public ResultBean editConfig(ThresholdConfig config){
        Assert.notNull(config.getId(),"Id不能为空");
        Assert.notNull(config.getValue(),"阈值不能为空");
        return this.thresholdConfigFeign.editConfig(config);
    }

    @Security
    @ResponseBody
    @PostMapping(value = "/ad/threshold_config/delete")
    public ResultBean deleteConfig(Long id){
        Assert.notNull(id,"Id不能为空");
        return this.thresholdConfigFeign.deleteConfig(id);
    }


}
