package com.czg.controller.admin;

import com.czg.account.dto.SysLoginDTO;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.SysUser;
import com.czg.account.service.AuthorizationService;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.SysUserService;
import com.czg.account.vo.LoginVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商户登录相关
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/auth")
public class AuthorizationController {
    @Resource
    private AuthorizationService authorizationService;
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private SysUserService sysUserService;


    /**
     * 验证码获取
     *
     * @return 验证码信息
     */
    @GetMapping("captcha")
    public CzgResult<?> captcha() {
        return CzgResult.success(authorizationService.getCaptcha());
    }

    /**
     * 商户登录
     *
     * @param loginDTO 登录参数
     * @return token信息
     */
    @PostMapping("login")
    public CzgResult<LoginVO> login(@Validated @RequestBody SysLoginDTO loginDTO, @RequestHeader(defaultValue = "WEB") String platformtype) {
        return CzgResult.success(authorizationService.login(loginDTO, platformtype));
    }

    /**
     * 退出登录
     */
    @PostMapping("logout")
    public CzgResult<?> logout() {
        StpKit.USER.logout();
        return CzgResult.success();
    }

    @GetMapping("/permission")
    public CzgResult<?> getPermission() {
        return CzgResult.success(StpKit.USER.getPermissionList());
    }


    /**
     * 核销获取 信息使用
     * 通过token 获取登录信息
     */
    @GetMapping("/userInfo")
    public CzgResult<Map<String, Object>> getUserInfo() {
        ShopInfo shopInfo = shopInfoService.queryChain()
                .select(ShopInfo::getId, ShopInfo::getShopName)
                .eq(ShopInfo::getId, StpKit.USER.getShopId()).one();
        SysUser one = sysUserService.queryChain().eq(SysUser::getId, StpKit.USER.getShopId()).one();
        Map<String, Object> map = Map.of("shopId", shopInfo.getId(), "shopName", shopInfo.getShopName(), "account", one.getAccount());
        return CzgResult.success(map);
    }
}
