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

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import com.tmerclub.cloud.api.user.vo.MemberOverviewVO;
import com.tmerclub.cloud.api.user.vo.UserApiVO;
import com.tmerclub.cloud.api.user.vo.UserManagerVO;
import com.tmerclub.cloud.api.user.vo.UserTagApiVO;
import com.tmerclub.cloud.common.constant.Constant;
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.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.Date;
import java.util.List;


/**
 * 用户信息
 *
 * @author cl
 */
@RestController("platformUserController")
@RequestMapping("/p/user")
@Tag(name = "platform-用户信息")
public class UserController {

    @Value("${tmerclub.expose.operation.auth:}")
    private Boolean permission;

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

    @GetMapping("/userPage")
    @Operation(summary = "获取会员管理别表列表", description = "分页获取会员管理别表列表")
    public ServerResponseEntity<PageVO<UserManagerVO>> userPage(@Valid PageDTO pageDTO, UserManagerDTO userManagerDTO) {
        PageVO<UserManagerVO> userPage = userService.getUserInfoPage(pageDTO, userManagerDTO);
        for (UserManagerVO userManagerVO : userPage.getList()) {
            userManagerVO.setPhone(PhoneUtil.hideBetween(userManagerVO.getPhone()).toString());
        }
        return ServerResponseEntity.success(userPage);
    }

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

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

    @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.downloadModel(response);
        return ServerResponseEntity.success();
    }

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

    @GetMapping("/getUserCountInfo")
    @Operation(summary = "获取今日客户概况", description = "获取今日客户概况")
    public ServerResponseEntity<MemberOverviewVO> getUserCountInfo() {
        MemberOverviewVO memberOverviewListVO = userService.getUserCountInfo(new Date());
        return ServerResponseEntity.success(memberOverviewListVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获取会员基本信息列表", description = "分页获取会员会员基本信息列表")
    public ServerResponseEntity<PageVO<UserApiVO>> list(@Valid PageDTO pageDTO, UserManagerDTO userManagerDTO) {
        return ServerResponseEntity.success(userService.page(pageDTO, userManagerDTO));
    }

    @Operation(summary = "更新用户保存的通联信息", description = "同步数据用(慎重使用)")
    @PutMapping("/updateUserByAllinpay")
    public ServerResponseEntity<Void> updateUserByAllinpay() {
        userService.updateUserInfoByAllinpay();
        return ServerResponseEntity.success();
    }
}
