package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.ThirdLoginConfig;
import org.springblade.modules.system.service.IThirdLoginConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/third-login")
@Api(value = "三方登录配置",tags = "三方登录配置接口")
public class ThirdLoginConfigController extends BladeController {

    @Autowired
    private IThirdLoginConfigService thirdLoginConfigService;

    @GetMapping("/page")
    @ApiOperation("分页查询三方登录配置")
    public R<Page<ThirdLoginConfig>> getPage(ThirdLoginConfig loginConfig, Query query){

        return R.data(thirdLoginConfigService.getPage(loginConfig,query));
    }

    @PostMapping("/save-update")
    @ApiOperation("创建或者更新三方登录配置")
    public R<Boolean> createOrUpdate(@RequestBody ThirdLoginConfig loginConfig){

        return R.data(thirdLoginConfigService.doSaveOrupdate(loginConfig,getUser()));
    }

    @GetMapping("/remove")
    @ApiOperation("删除三方登录配置")
    public R<Boolean> removeLoginConf(Integer id){

        return R.data(thirdLoginConfigService.removeById(id));
    }

    @GetMapping("/enable-lose")
    @ApiOperation("启用或者禁用三方登录配置")
    public R<Boolean> enableOrlose(Integer id,Integer status){

        ThirdLoginConfig loginConfig = new ThirdLoginConfig();
        loginConfig.setId(id);
        loginConfig.setStatus(status);
        loginConfig.setUpdateTime(LocalDateTime.now());
        return R.data(thirdLoginConfigService.updateById(loginConfig));
    }

    @GetMapping("/enable-lose/list")
    @ApiOperation("批量启用或者禁用三方登录配置")
    public R<Boolean> enableOrloseList(List<Integer> ids, Integer status){

        List<ThirdLoginConfig> loginConfigs = new ArrayList<>();
        for (Integer id : ids) {
            ThirdLoginConfig loginConfig = new ThirdLoginConfig();
            loginConfig.setId(id);
            loginConfig.setStatus(status);
            loginConfig.setUpdateTime(LocalDateTime.now());
            loginConfigs.add(loginConfig);
        }

        return R.data(thirdLoginConfigService.updateBatchById(loginConfigs));
    }
}
