package org.dromara.system.controller.system;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysCustomerBo;
import org.dromara.system.domain.vo.SysCustomerVo;
import org.dromara.system.service.ISysCustomerService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 客户管理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/customer")
public class SysCustomerController extends BaseController {

    private final ISysCustomerService sysCustomerService;

    /**
     * 获取当前登录客户信息
     */
    @GetMapping("/getInfo")
    public R<SysCustomerVo> getMyInfo() {
        Long customerId = LoginHelper.getUserId();
        return R.ok(sysCustomerService.queryById(customerId));
    }

    /**
     * 更新当前登录客户信息
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateProfile")
    public R<Void> updateMyInfo(@RequestBody SysCustomerBo bo) {
        Long customerId = LoginHelper.getUserId();
        bo.setId(customerId);
        return toAjax(sysCustomerService.updateByBo(bo));
    }

    /**
     * 查询客户列表
     */
//     @SaCheckPermission("customer:customer:list")
    @GetMapping("/list")
    public TableDataInfo<SysCustomerVo> list(SysCustomerBo bo, PageQuery pageQuery) {
        return sysCustomerService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取客户详细信息
     *
     * @param id 主键
     */
//     @SaCheckPermission("customer:customer:query")
    @GetMapping("/{id}")
    public R<SysCustomerVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(sysCustomerService.queryById(id));
    }

    /**
     * 授权客户
     *
     * @param id             客户ID
     * @param authClientFRowID 授权客户 RowID
     * @Author: Antigravity
     */
//     @SaCheckPermission("customer:customer:auth")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/auth")
    public R<Void> auth(@NotNull(message = "客户ID不能为空") Long id,
                        @NotEmpty(message = "授权客户 RowID 不能为空") String authClientFRowID) {
        SysCustomerBo bo = new SysCustomerBo();
        bo.setId(id);
        bo.setAuthClientFRowID(authClientFRowID);
        return toAjax(sysCustomerService.updateByBo(bo));
    }
}
