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

import com.tmerclub.cloud.api.user.vo.UserManagerVO;
import com.tmerclub.cloud.api.user.vo.UserTagApiVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.user.dto.ShopCustomerDTO;
import com.tmerclub.cloud.user.dto.UserDTO;
import com.tmerclub.cloud.user.dto.UserManagerDTO;
import com.tmerclub.cloud.user.manager.UserExcelManager;
import com.tmerclub.cloud.user.service.UserService;
import com.tmerclub.cloud.user.service.UserTagUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 用户信息
 *
 * @author cl
 */
@RestController("multishopUserController")
@RequestMapping("/m/user")
@Tag(name = "multishop-客户信息")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserTagUserService userTagUserService;
    @Autowired
    private UserExcelManager userExcelManager;


    @GetMapping("/shopUserPage")
    @Operation(summary = "获取客户列表", description = "分页获客户列表")
    public ServerResponseEntity<PageVO<UserManagerVO>> shopUserPage(@Valid PageDTO pageDTO, ShopCustomerDTO shopCustomerDTO) {
        shopCustomerDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<UserManagerVO> shopCustomerPageVO = userService.pageShopCustomerByParam(pageDTO, shopCustomerDTO);
        return ServerResponseEntity.success(shopCustomerPageVO);
    }


    /**
     * 修改
     */
    @PutMapping
    public ServerResponseEntity<Void> update(@RequestBody UserDTO userDTO) {
        userService.updateUserInfo(userDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getUserInfo")
    @Operation(summary = "根据用户id获取会员信息", description = "根据用户id获取会员信息")
    public ServerResponseEntity<UserManagerVO> info(@RequestParam Long userId) {
        UserManagerVO param = userService.getUserInfo(userId, AuthUserContext.get().getTenantId());
        List<UserTagApiVO> userTagParams = userTagUserService.getUserTagsByUserId(userId);
        param.setUserTagList(userTagParams);
        return ServerResponseEntity.success(param);
    }

    @GetMapping("/soldExcel")
    @Operation(summary = "导出excel", description = "导出客户信息excel")
    public ServerResponseEntity<Void> userSoldExcel(HttpServletResponse response, UserManagerDTO userManagerDTO) {
        userExcelManager.exportUserInfoExcel(response, userManagerDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/exportModel")
    @Operation(summary = "下载客户信息excel模板", description = "下载客户信息excel模板")
    public ServerResponseEntity<Void> exportModel(HttpServletResponse response) {
        userExcelManager.downloadShopCustomerModel(response);
        return ServerResponseEntity.success();
    }

    @PostMapping("/importExcel")
    @Operation(summary = "导入excel", description = "导入客户信息excel")
    public ServerResponseEntity<String> importExcel(@RequestParam("excelFile") MultipartFile file) {
        if (file == null) {
            throw new LuckException("网络繁忙，请稍后重试");
        }
        String info = userExcelManager.importShopCustomerExcel(file);
        return ServerResponseEntity.success(info);
    }


}
