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

import cn.newfeifan.mall.module.member.controller.admin.user.vo.MemberUserPageReqVO;
import cn.newfeifan.mall.module.member.controller.admin.user.vo.MemberUserRespVO;
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 java.util.stream.Collectors;

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.common.util.collection.CollectionUtils.convertSet;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.duser.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.duser.DuserDO;
import cn.newfeifan.mall.module.distri.service.duser.DuserService;

@Tag(name = "管理后台 - 推荐用户")
@RestController
@RequestMapping("/distri/duser")
@Validated
public class DuserController {

    @Resource
    private DuserService duserService;

    @PostMapping("/create")
    @Operation(summary = "创建推荐用户")
    @PreAuthorize("@ss.hasPermission('distri:duser:create')")
    public CommonResult<Long> createDuser(@Valid @RequestBody DuserSaveReqVO createReqVO) {
        return success(duserService.createDuser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新推荐用户")
    @PreAuthorize("@ss.hasPermission('distri:duser:update')")
    public CommonResult<Boolean> updateDuser(@Valid @RequestBody DuserSaveReqVO updateReqVO) {
        duserService.updateDuser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除推荐用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:duser:delete')")
    public CommonResult<Boolean> deleteDuser(@RequestParam("id") Long id) {
        duserService.deleteDuser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得推荐用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:duser:query')")
    public CommonResult<DuserRespVO> getDuser(@RequestParam("id") Long id) {
        DuserDO duser = duserService.getDuserByUser(id);
        return success(BeanUtils.toBean(duser, DuserRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得推荐用户分页")
    @PreAuthorize("@ss.hasPermission('distri:duser:query')")
    public CommonResult<PageResult<DuserRespVO>> getDuserPage(@Valid DuserPageReqVO pageReqVO) {
        PageResult<DuserDO> pageResult = duserService.getDuserPage(pageReqVO);
        PageResult<DuserRespVO> result = BeanUtils.toBean(pageResult, DuserRespVO.class);

        duserService.getSharePathCount(result);
        return success(result);
    }

    @GetMapping("/memberUserPage")
    @Operation(summary = "获取会员用户分页 - mUser")
    @PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<PageResult<MemberUserRespVO>> getUserPage(@Valid MemberUserPageReqVO pageVO) {
        return success(duserService.getMemberUserPage(pageVO));

    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出推荐用户 Excel")
    @PreAuthorize("@ss.hasPermission('distri:duser:export')")
    @OperateLog(type = EXPORT)
    public void exportDuserExcel(@Valid DuserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DuserDO> list = duserService.getDuserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "推荐用户.xls", "数据", DuserRespVO.class,
                        BeanUtils.toBean(list, DuserRespVO.class));
    }


    @GetMapping("/getDuserInfo")
    @Operation(summary = "获取分销人员信息")
    @PreAuthorize("@ss.hasPermission('distri:duser:query')")
    public CommonResult<DuserInfoVO> getDuserInfo(@RequestParam("userId") Long userId) {
        DuserInfoVO duserInfo = duserService.getDuserInfo(userId);
        return success(duserInfo);
    }

    @GetMapping("/initDuser")
    @Operation(summary = "初始化营销人员数据")
    public CommonResult<DuserInfoVO> initDuser() {
        DuserInfoVO duserInfo = duserService.initDuser();
        return success(duserInfo);
    }



}