package com.op.system.controller;

import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.web.controller.BaseController;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.system.api.domain.SysUser;
import com.op.system.domain.SysDatasource;
import com.op.system.service.ISysDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工厂数据库Controller
 *
 * @author sf
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/datasource")
public class SysDatasourceController extends BaseController {
    @Autowired
    private ISysDatasourceService sysDatasourceService;

    /**
     * 查询工厂数据库列表
     */
    @RequiresPermissions("system:datasource:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDatasource sysDatasource) {
        startPage();
        List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
        return getDataTable(list);
    }

    /**
     * 导出工厂数据库列表
     */
    @RequiresPermissions("system:datasource:export")
    @Log(title = "工厂数据库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDatasource sysDatasource) {
        List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
        ExcelUtil<SysDatasource> util = new ExcelUtil<SysDatasource>(SysDatasource.class);
        util.exportExcel(response, list, "工厂数据库数据");
    }

    /**
     * 获取工厂数据库详细信息
     */
    @RequiresPermissions("system:datasource:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sysDatasourceService.selectSysDatasourceById(id));
    }

    /**
     * 新增工厂数据库
     */
    @RequiresPermissions("system:datasource:add")
    @Log(title = "工厂数据库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDatasource sysDatasource) {
        return toAjax(sysDatasourceService.insertSysDatasource(sysDatasource));
    }

    /**
     * 修改工厂数据库
     */
    @RequiresPermissions("system:datasource:edit")
    @Log(title = "工厂数据库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDatasource sysDatasource) {
        return toAjax(sysDatasourceService.updateSysDatasource(sysDatasource));
    }

    /**
     * 删除工厂数据库
     */
    @RequiresPermissions("system:datasource:remove")
    @Log(title = "工厂数据库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysDatasourceService.deleteSysDatasourceByIds(ids));
    }

    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:datasource:edit")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(SysDatasource sysDatasource) {
        startPage();
        List<SysDatasource> list = sysDatasourceService.selectAllocatedList(sysDatasource);
        return getDataTable(list);
    }

    /**
     * 查询未分配用户角色列表
     */
    //@RequiresPermissions("system:role:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(SysUser user) {
        startPage();
        List<SysUser> list = sysDatasourceService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 批量选择用户授权
     */
    //@RequiresPermissions("system:role:edit")
    @Log(title = "数据源管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(String datasourceId, Long[] userIds) {
        //sysDatasourceService.checkRoleDataScope(datasourceId);
        return toAjax(sysDatasourceService.insertAuthUsers(datasourceId, userIds));
    }

    /**
     * 取消授权用户
     */
    //@RequiresPermissions("system:role:edit")
    @Log(title = "数据源管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody SysDatasource sysDatasource) {
        return toAjax(sysDatasourceService.deleteAuthUser(sysDatasource));
    }

    /**
     * 批量取消授权用户
     */
    //@RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(String datasourceId, Long[] userIds) {
        return toAjax(sysDatasourceService.deleteAuthUsers(datasourceId, userIds));
    }

    @RequiresPermissions("system:datasource:add")
    @Log(title = "激活工厂数据库", businessType = BusinessType.UPDATE)
    @GetMapping("/changeDBStatus/{datasourceId}")
    public AjaxResult changeDBStatus(@PathVariable("datasourceId") String datasourceId) {
        SysDatasource sysDatasource = new SysDatasource();
        sysDatasource.setDatasourceId(datasourceId);
        return success(sysDatasourceService.changeDBStatus(sysDatasource));
    }

}
