
package com.tmerclub.cloud.multishop.controller.multishop;

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.multishop.dto.ShopUserDTO;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.service.ShopUserService;
import com.tmerclub.cloud.multishop.vo.ShopUserSimpleVO;
import com.tmerclub.cloud.multishop.vo.ShopUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/5/12 20:27
 */
@RequestMapping("/m/applyShop/shopUser")
@RestController("multishopShopUserController")
@Tag(name = "multishop-店铺用户信息")
public class ApplyShopUserController {

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ShopUserService shopUserService;

    @DubboReference
    private AccountFeignClient accountFeignClient;

    @GetMapping("/info")
    @Operation(summary = "登陆店铺用户信息", description = "获取当前登陆店铺用户的用户信息")
    public ServerResponseEntity<ShopUserSimpleVO> info() {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        ShopUserSimpleVO shopUserSimple = new ShopUserSimpleVO();
        shopUserSimple.setIsAdmin(uidInfoBO.getIsAdmin());
        shopUserSimple.setIsPassShop(uidInfoBO.getIsPassShop());
        shopUserSimple.setTenantId(uidInfoBO.getTenantId());
        shopUserSimple.setUserId(uidInfoBO.getUserId());
        shopUserSimple.setUserName(uidInfoBO.getUsername());
        if (!Objects.equals(uidInfoBO.getTenantId(), Constant.DEFAULT_SHOP_ID)) {
            ShopDetailVO shopDetail = shopDetailService.getByShopId(uidInfoBO.getTenantId());
            shopUserSimple.setAvatar(shopDetail.getShopLogo());
            shopUserSimple.setShopName(shopDetail.getShopName());
        }
        return ServerResponseEntity.success(shopUserSimple);
    }

    @GetMapping("/list")
    @Operation(summary = "获取店铺下的所有员工列表", description = "获取店铺下的所有员工列表")
    public ServerResponseEntity<List<ShopUserVO>> list() {
        ShopUserDTO shopUserDTO = new ShopUserDTO();
        shopUserDTO.setShopId(AuthUserContext.get().getTenantId());
        List<ShopUserVO> shopUserVOList = shopUserService.listByShopId(shopUserDTO);
        return ServerResponseEntity.success(shopUserVOList);
    }
}
