package com.fs.xiaoshouyi.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.StringUtils;
import com.fs.xiaoshouyi.domain.XsyAccount;
import com.fs.xiaoshouyi.service.XsyAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售易账号管理
 */
@RestController
@RequestMapping("/xsy/account")
@RequiredArgsConstructor
public class XsyAccountController extends BaseController {

    private final XsyAccountService xsyAccountService;


    /**
     * 分页查询账号列表(总后台)
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:query')")
    @GetMapping("/list")
    public TableDataInfo list(XsyAccount query) {
        startPage();
        List<XsyAccount> list = xsyAccountService.selectList(query);
        return getDataTable(list);
    }



    /**
     * 查询详情
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:query')")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return AjaxResult.success(xsyAccountService.selectById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody XsyAccount account) {
        String uri = account.getRedirectUri();
        Snowflake snowflake = IdUtil.getSnowflake();
        Long id = snowflake.nextId();
        account.setId(id);

        if (StringUtils.isNotEmpty(uri) && ObjectUtil.isNotEmpty(id)) {
            uri = uri.endsWith("/") ? uri.substring(0, uri.length() - 1) : uri;
            account.setRedirectUri(uri + "/" + id);
        }
        xsyAccountService.insert(account);
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:update')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XsyAccount account) {
        xsyAccountService.update(account);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:delete')")
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        xsyAccountService.deleteById(id);
        return AjaxResult.success();
    }

    /**
     * 启用/禁用
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:update')")
    @PostMapping("/status")
    public AjaxResult updateStatus(@RequestParam Long id,
                                   @RequestParam Integer status) {
        XsyAccount account = new XsyAccount();
        account.setId(id);
        account.setStatus(status);

        xsyAccountService.update(account);
        return AjaxResult.success();
    }

    /**
     * 获取授权URL
     */
    @PreAuthorize("@ss.hasPermi('xsy:account:query')")
    @GetMapping("/authUrl/{accountId}")
    public AjaxResult getAuthUrl(@PathVariable Long accountId) {
        return AjaxResult.success(xsyAccountService.getAuthUrl(accountId));
    }
}