package com.xhpc.user.controller;

import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.user.domain.XhpcStationInternetBlacklist;
import com.xhpc.user.service.IXhpcInternetUserService;
import com.xhpc.user.service.IXhpcStationInternetBlacklistService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 电站流量方黑名单
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/station/internet")
public class XhpcStationInternetBlacklistController extends BaseController {

    @Autowired
    private IXhpcStationInternetBlacklistService iXhpcStationInternetBlacklistService;

    @Autowired
    private IXhpcInternetUserService xhpcInternetUserService;

    /**
     * 新增电站流量方黑名单
     */
    @Log(title = "场站-设置流量方黑名单", businessType = BusinessType.INSERT)
    @ApiOperation("新增电站流量方黑名单")
    // @PreAuthorize(hasPermi = "station:internet:add")
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody List<XhpcStationInternetBlacklist> list) {

        String internetUserId = list.get(0).getInternetUserId();
        iXhpcStationInternetBlacklistService.deleteByOperatorIdEvcs(internetUserId);
        if (null != list && list.size() > 0) {
            for (XhpcStationInternetBlacklist xhpcStationInternetBlacklist : list) {
                Map<String, Object> info =
                        xhpcInternetUserService.info(Long.valueOf(xhpcStationInternetBlacklist.getInternetUserId()));
                if (info != null || info.get("operatorIdEvcs") != null) {
                    xhpcStationInternetBlacklist.setOperatorIdEvcs(info.get("operatorIdEvcs").toString());
                }
                iXhpcStationInternetBlacklistService.insert(xhpcStationInternetBlacklist);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 通过流量用户id查询电站流量方黑名单(包含已选择的黑名单)
     */
    @ApiOperation("通过运营商id查询流量方黑名单")
    //@PreAuthorize(hasPermi = "station:internet:list")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam String internetUserId) {
        startPage();
        List<Map<String, Object>> list = iXhpcStationInternetBlacklistService.list(internetUserId);
        return getDataTable(list);
    }

    @GetMapping("/list1")
    public TableDataInfo list1(@RequestParam String internetUserId) {
        startPage();
        List<Map<String, Object>> list = iXhpcStationInternetBlacklistService.list(internetUserId);
        return getDataTable(list);
    }

}
