package co.yixiang.yshop.module.distributor.controller.admin.distributoruser;

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributoruser.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributoruser.DistributorUserDO;
import co.yixiang.yshop.module.distributor.convert.distributoruser.DistributorUserConvert;
import co.yixiang.yshop.module.distributor.service.distributoruser.DistributorUserService;

@Tag(name = "管理后台 - 分销商所属用户")
@RestController
@RequestMapping("/distributor/user")
@Validated
public class DistributorUserController {

    @Resource
    private DistributorUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商所属用户")
    @PreAuthorize("@ss.hasPermission('distributor:user:create')")
    public CommonResult<Long> createUser(@Valid @RequestBody DistributorUserCreateReqVO createReqVO) {
        return success(userService.createUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商所属用户")
    @PreAuthorize("@ss.hasPermission('distributor:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody DistributorUserUpdateReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商所属用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distributor:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商所属用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:user:query')")
    public CommonResult<DistributorUserRespVO> getUser(@RequestParam("id") Long id) {
        DistributorUserDO user = userService.getUser(id);
        return success(DistributorUserConvert.INSTANCE.convert(user));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商所属用户列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:user:query')")
    public CommonResult<List<DistributorUserRespVO>> getUserList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorUserDO> list = userService.getUserList(ids);
        return success(DistributorUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商所属用户分页")
    @PreAuthorize("@ss.hasPermission('distributor:user:query')")
    public CommonResult<PageResult<DistributorUserRespVO>> getUserPage(@Valid DistributorUserPageReqVO pageVO) {
        PageResult<DistributorUserDO> pageResult = userService.getUserPage(pageVO);
        return success(DistributorUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商所属用户 Excel")
    @PreAuthorize("@ss.hasPermission('distributor:user:export')")
    @OperateLog(type = EXPORT)
    public void exportUserExcel(@Valid DistributorUserExportReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<DistributorUserDO> list = userService.getUserList(exportReqVO);
        // 导出 Excel
        List<DistributorUserExcelVO> datas = DistributorUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分销商所属用户.xls", "数据", DistributorUserExcelVO.class, datas);
    }

}
