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

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.usertaggroup.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.usertaggroup.UserTagGroupDO;
import co.yixiang.yshop.module.member.convert.usertaggroup.UserTagGroupConvert;
import co.yixiang.yshop.module.member.service.usertaggroup.UserTagGroupService;

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

    @Resource
    private UserTagGroupService userTagGroupService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得会员标签分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-tag-group:query')")
    public CommonResult<UserTagGroupRespVO> getUserTagGroup(@RequestParam("id") Long id) {
        UserTagGroupDO userTagGroup = userTagGroupService.getUserTagGroup(id);
        return success(UserTagGroupConvert.INSTANCE.convert(userTagGroup));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员标签分组列表")
    @PreAuthorize("@ss.hasPermission('member:user-tag-group:query')")
    public CommonResult<List<UserTagGroupRespVO>> getUserTagGroupList() {
        List<UserTagGroupDO> list = userTagGroupService.getUserTagGroupList();
        return success(UserTagGroupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员标签分组分页")
    @PreAuthorize("@ss.hasPermission('member:user-tag-group:query')")
    public CommonResult<PageResult<UserTagGroupRespVO>> getUserTagGroupPage(@Valid UserTagGroupPageReqVO pageVO) {
        PageResult<UserTagGroupDO> pageResult = userTagGroupService.getUserTagGroupPage(pageVO);
        return success(UserTagGroupConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员标签分组 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-tag-group:export')")
    @OperateLog(type = EXPORT)
    public void exportUserTagGroupExcel(@Valid UserTagGroupExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<UserTagGroupDO> list = userTagGroupService.getUserTagGroupList(exportReqVO);
        // 导出 Excel
        List<UserTagGroupExcelVO> datas = UserTagGroupConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员标签分组.xls", "数据", UserTagGroupExcelVO.class, datas);
    }

}
