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

import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import cn.newfeifan.mall.module.distri.controller.app.sharepath.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
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.dal.dataobject.sharepath.SharePathDO;
import cn.newfeifan.mall.module.distri.service.sharepath.SharePathService;

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

    @Resource
    private SharePathService sharePathService;

    @PostMapping("/create")
    @Operation(summary = "创建分销用户关系")
    @PreAuthenticated
    public CommonResult<Boolean> createSharePath(@Valid @RequestBody SharePathSaveReqVO createReqVO) {
        sharePathService.createSharePathByUserId(createReqVO);
        return success(true);
    }

    @GetMapping("/getTree")
    @Operation(summary = "获取分销人员关系树")
    @PreAuthenticated
    public CommonResult<SharePathRespVO> getTree() {
        TreeNode sharePath = sharePathService.getTree();
        return success(BeanUtils.toBean(sharePath, SharePathRespVO.class));
    }

    @PutMapping("/transference_rela")
    @Operation(summary = "转让分销关系")
    @PreAuthenticated
    public CommonResult<Boolean> transferenceRela(SharePathtransFerenceRelaReqVO sharePathtransFerenceRelaReqVO) {
        sharePathService.transferenceRela(sharePathtransFerenceRelaReqVO);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新分销用户关系")
    @PreAuthenticated
    public CommonResult<Boolean> updateSharePath(@Valid @RequestBody SharePathSaveReqVO updateReqVO) {
        sharePathService.updateSharePath(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/page")
    @Operation(summary = "获得分销用户关系分页")
    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")
    @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("/getTeam")
    @Operation(summary = "获取团队")
    @PreAuthenticated
    public CommonResult<AppSharePathRespVO> getTeam(@Valid PageParam pageParam){
        return success(sharePathService.getTeam(pageParam));
    }
}