package cn.newfeifan.mall.module.distri.controller.admin.sharepath;

import cn.newfeifan.mall.module.distri.dal.dataobject.duser.DuserDO;
import cn.newfeifan.mall.module.member.controller.admin.user.vo.MemberUserRespVO;
import cn.newfeifan.mall.module.member.controller.admin.user.vo.MemberUserUpdateReqVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;

import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO;
import cn.newfeifan.mall.module.distri.service.sharepath.SharePathService;

@Tag(name = "管理后台 - 分销用户关系")
@RestController
@RequestMapping("/distri/share-path")
@Validated
public class SharePathController {

    @Resource
    private SharePathService sharePathService;

    @PostMapping("/create")
    @Operation(summary = "创建分销用户关系")
    @PreAuthorize("@ss.hasPermission('distri:share-path:create')")
    public CommonResult<Boolean> createSharePath(@Valid @RequestBody SharePathSaveReqVO createReqVO) {
        sharePathService.createSharePathByUserId(createReqVO, true);
        return success(true);
    }



    @PutMapping("/transference_rela")
    @Operation(summary = "转让分销关系")
    @PreAuthorize("@ss.hasPermission('distri:share-path:update')")
    public CommonResult<Boolean> transferenceRela(SharePathtransFerenceRelaReqVO sharePathtransFerenceRelaReqVO) {
        sharePathService.transferenceRela(sharePathtransFerenceRelaReqVO);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新分销用户关系")
    @PreAuthorize("@ss.hasPermission('distri:share-path:update')")
    public CommonResult<Boolean> updateSharePath(@Valid @RequestBody SharePathSaveReqVO updateReqVO) {
        sharePathService.updateSharePath(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销用户关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:share-path:delete')")
    public CommonResult<Boolean> deleteSharePath(@RequestParam("id") Long id) {
        sharePathService.deleteSharePath(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销用户关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:share-path:query')")
    public CommonResult<SharePathRespVO> getSharePath(@RequestParam("id") Long id) {
        SharePathDO sharePath = sharePathService.getSharePath(id);
        return success(BeanUtils.toBean(sharePath, SharePathRespVO.class));
    }

    @GetMapping("/getMemberUser")
    @Operation(summary = "获得会员用户详情")
    @Parameter(name = "id", description = "用户编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<MemberUserRespVO> getUser(@RequestParam("id") Long userId) {
        return success(sharePathService.getMemberUser(userId));
    }

    @GetMapping("/getDescendants")
    @Operation(summary = "通过推荐人获得直推人")
    @PreAuthorize("@ss.hasPermission('distri:duser:query')")
    public CommonResult<PageResult<SharePathRespVO>> getDescendants(@Valid SharePathPageReqVO pageReqVO) {
        PageResult<SharePathRespVO> descendants = sharePathService.getDescendants(pageReqVO);
        return success(descendants);
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销用户关系分页")
    @PreAuthorize("@ss.hasPermission('distri:share-path:query')")
    public CommonResult<PageResult<SharePathRespVO>> getSharePathPage(@Valid SharePathPageReqVO pageReqVO) {
        PageResult<SharePathDO> pageResult = sharePathService.getSharePathPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SharePathRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销用户关系 Excel")
    @PreAuthorize("@ss.hasPermission('distri:share-path:export')")
    @OperateLog(type = EXPORT)
    public void exportSharePathExcel(@Valid SharePathPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SharePathDO> list = sharePathService.getSharePathPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分销用户关系.xls", "数据", SharePathRespVO.class,
                BeanUtils.toBean(list, SharePathRespVO.class));
    }


    @GetMapping("/selectSon")
    @Operation(summary = "获取当前用户的所有后代")
    @PreAuthorize("@ss.hasPermission('distri:share-path:query')")
    public CommonResult<List<DuserDO>> selectSon(@RequestParam("userId") Long userId) {
        List<DuserDO> pageResult = sharePathService.selectSon(userId);
        return success(pageResult);
    }

    @GetMapping("/getTree")
    @Operation(summary = "获取分销人员关系树")
    @PreAuthorize("@ss.hasPermission('distri:share-path:query')")
    public CommonResult<TreeNode> getTree(@RequestParam("userId") Long userId,
                                          @RequestParam("maxDepth") Integer maxDepth) {
        TreeNode sharePath = sharePathService.getTree(userId,maxDepth);
        return success(sharePath);
    }

    @GetMapping("/getTreeByNameOrMobile")
    @Operation(summary = "通过人员的名称或者电话获取分销人员关系树")
    @PreAuthorize("@ss.hasPermission('distri:share-path:query')")
    public CommonResult<TreeNode> getTreeByNameOrMobile(SharePathTreeReqVO sharePathTreeReqVO) {
        TreeNode sharePath = sharePathService.getTreeByNameOrMobile(sharePathTreeReqVO);
        return success(sharePath);
    }

    @PutMapping("/updateMemberUser")
    @Operation(summary = "更新会员用户信息")
    @PreAuthorize("@ss.hasPermission('member:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody MemberUserUpdateReqVO updateReqVO) {
        sharePathService.updateUser(updateReqVO);
        return success(true);
    }
}