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

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
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.supplier.dto.SupplierUserDTO;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import com.tmerclub.cloud.supplier.service.SupplierUserService;
import com.tmerclub.cloud.supplier.vo.SupplierUserSimpleVO;
import com.tmerclub.cloud.supplier.vo.SupplierUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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("/s/applyShop/shopUser")
@RestController("supplierShopUserController")
@Tag(name = "supplier-店铺用户信息")
public class ApplyShopUserController {

    @Autowired
    private SupplierDetailService supplierDetailService;

    @Autowired
    private SupplierUserService supplierUserService;

    @GetMapping("/info")
    @Operation(summary = "登陆店铺用户信息", description = "获取当前登陆店铺用户的用户信息")
    public ServerResponseEntity<SupplierUserSimpleVO> info() {
        UidInfoBO userInfoInTokenBO = AuthUserContext.get();
        SupplierUserSimpleVO shopUserSimple = new SupplierUserSimpleVO();
        shopUserSimple.setIsAdmin(userInfoInTokenBO.getIsAdmin());
        shopUserSimple.setIsPassShop(userInfoInTokenBO.getIsPassShop());
        shopUserSimple.setTenantId(userInfoInTokenBO.getTenantId());
        shopUserSimple.setUserId(userInfoInTokenBO.getUserId());
        shopUserSimple.setUserName(userInfoInTokenBO.getUsername());
        if (!Objects.equals(userInfoInTokenBO.getTenantId(), Constant.DEFAULT_SUPPLIER_ID)) {
            SupplierApiDetailVO apiDetailVO = supplierDetailService.getBySupplierId(userInfoInTokenBO.getTenantId());
            shopUserSimple.setAvatar(apiDetailVO.getSupplierLogo());
            shopUserSimple.setSupplierName(apiDetailVO.getSupplierName());
        }
        return ServerResponseEntity.success(shopUserSimple);
    }

    @GetMapping("/list")
    @Operation(summary = "获取店铺下的所有员工列表", description = "获取店铺下的所有员工列表")
    public ServerResponseEntity<List<SupplierUserVO>> list() {
        SupplierUserDTO supplierUserDTO = new SupplierUserDTO();
        supplierUserDTO.setSupplierId(AuthUserContext.get().getTenantId());
        List<SupplierUserVO> shopUserVOList = supplierUserService.listBySupplierId(supplierUserDTO);
        return ServerResponseEntity.success(shopUserVOList);
    }
}
