package co.yixiang.yshop.module.member.controller.admin.usertag;

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.member.controller.admin.usertag.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.usertag.UserTagDO;
import co.yixiang.yshop.module.member.convert.usertag.UserTagConvert;
import co.yixiang.yshop.module.member.service.usertag.UserTagService;

@Tag(name = "管理后台 - 会员标签")
@RestController
@RequestMapping("/member/user-tag")
@Validated
public class UserTagController {

    @Resource
    private UserTagService userTagService;

    @PostMapping("/create")
    @Operation(summary = "创建会员标签")
    @PreAuthorize("@ss.hasPermission('member:user-tag:create')")
    public CommonResult<Long> createUserTag(@Valid @RequestBody UserTagCreateReqVO createReqVO) {
        return success(userTagService.createUserTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员标签")
    @PreAuthorize("@ss.hasPermission('member:user-tag:update')")
    public CommonResult<Boolean> updateUserTag(@Valid @RequestBody UserTagUpdateReqVO updateReqVO) {
        userTagService.updateUserTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-tag:delete')")
    public CommonResult<Boolean> deleteUserTag(@RequestParam("id") Long id) {
        userTagService.deleteUserTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-tag:query')")
    public CommonResult<UserTagRespVO> getUserTag(@RequestParam("id") Long id) {
        UserTagDO userTag = userTagService.getUserTag(id);
        return success(UserTagConvert.INSTANCE.convert(userTag));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员标签分页")
    @PreAuthorize("@ss.hasPermission('member:user-tag:query')")
    public CommonResult<PageResult<UserTagRespVO>> getUserTagPage(@Valid UserTagPageReqVO pageVO) {
        PageResult<UserTagDO> pageResult = userTagService.getUserTagPage(pageVO);
        return success(UserTagConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员标签列表")
    @PreAuthorize("@ss.hasPermission('member:user-tag:query')")
    public CommonResult<List<UserTagRespVO>> getUserTagList(@Valid UserTagPageReqVO pageVO) {
        List<UserTagDO> list = userTagService.getUserTagList(pageVO);
        return success(UserTagConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员标签 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-tag:export')")
    @OperateLog(type = EXPORT)
    public void exportUserTagExcel(@Valid UserTagExportReqVO exportReqVO,
                                   HttpServletResponse response) throws IOException {
        List<UserTagDO> list = userTagService.getUserTagList(exportReqVO);
        // 导出 Excel
        List<UserTagExcelVO> datas = UserTagConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员标签.xls", "数据", UserTagExcelVO.class, datas);
    }

}
