package cn.iocoder.yudao.module.member.controller.admin.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberPointsPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberPointsRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.*;
import cn.iocoder.yudao.module.member.service.user.MemberPointsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 会员用户积分")
@RestController
@RequestMapping("/member/points")
@Validated
public class MemberPointsController {

    @Resource
    private MemberPointsService pointsService;


    //
//    @PostMapping("/create")
//    @Operation(summary = "创建会员用户积分")
//    @PreAuthorize("@ss.hasPermission('member:points:create')")
//    public CommonResult<Long> createPoints(@Valid @RequestBody PointsSaveReqVO createReqVO) {
//        return success(pointsService.createPoints(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新会员用户积分")
//    @PreAuthorize("@ss.hasPermission('member:points:update')")
//    public CommonResult<Boolean> updatePoints(@Valid @RequestBody PointsSaveReqVO updateReqVO) {
//        pointsService.updatePoints(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除会员用户积分")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('member:points:delete')")
//    public CommonResult<Boolean> deletePoints(@RequestParam("id") Long id) {
//        pointsService.deletePoints(id);
//        return success(true);
//    }
//
    @GetMapping("/get")
    @Operation(summary = "获得会员用户积分详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MemberPointsRespVO> getDetail(@RequestParam("id") Long id) {
        return success(pointsService.getDetail(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员用户积分分页")
    @TenantIgnore
    public CommonResult<PageResult<MemberPointsDO>> getMemberPointsPage(@Valid MemberPointsPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<MemberPointsDO> pageResult = pointsService.getMemberPointsPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/bonusPoints")
    @Operation(summary = "加分")
    public CommonResult<Boolean> bonusPoints(@RequestBody MemberBonusPointsDO bonusPointsDO) {
        pointsService.bonusPoints(bonusPointsDO);
        return success(true);
    }

    @PostMapping("/exchangePointsItem")
    @Operation(summary = "兑换")
    public CommonResult<Boolean> exchangePointsItem(@RequestBody MemberPointsExchangeDO memberPointsExchangeDO) {
        pointsService.exchangePointsItem(memberPointsExchangeDO);
        return success(true);
    }


    @PostMapping("/settingPointsItems")
    @Operation(summary = "物品设置")
    public CommonResult<Boolean> settingPointsItems(@RequestBody List<MemberPointsItemDO> items) {
        pointsService.settingPointsItems(items);
        return success(true);
    }

    @GetMapping("/getPointsItems")
    @Operation(summary = "获取物品列表")
    @TenantIgnore
    public CommonResult<List<MemberPointsItemDO>> getPointsItems(@RequestParam(value = "orgType", required = false) Integer orgType,
                                                                 @RequestParam(value = "tenantId",required = false) Long tenantId) {
        return success(pointsService.getPointsItems(orgType,tenantId));
    }

    @PostMapping("/addMemberPointsResetConfig")
    @Operation(summary = "新增清零设置")
    public CommonResult<Boolean> addMemberPointsResetConfig(@RequestBody MemberPointsResetConfigDO resetConfigDO) {
        pointsService.addMemberPointsResetConfig(resetConfigDO);
        return success(true);
    }

    @PostMapping("/editMemberPointsResetConfig")
    @Operation(summary = "编辑清零设置")
    public CommonResult<Boolean> editMemberPointsResetConfig(@RequestBody MemberPointsResetConfigDO resetConfigDO) {
        pointsService.editMemberPointsResetConfig(resetConfigDO);
        return success(true);
    }


    @GetMapping("/getMemberPointsResetConfig")
    @Operation(summary = "获取清零设置")
    @TenantIgnore
    public CommonResult<MemberPointsResetConfigDO> getMemberPointsResetConfig(@RequestParam(value = "orgType", required = false) Integer orgType,
                                                                              @RequestParam(value = "tenantId", required = false) Long tenantId) {
        tenantId = Objects.isNull(tenantId) ? TenantContextHolder.getTenantId() : tenantId;
        return success(pointsService.getMemberPointsResetConfig(orgType,tenantId));
    }


    @PostMapping("/updateMemberPointsConfig")
    @Operation(summary = "编辑抵扣设置")
    public CommonResult<Boolean> updateMemberPointsConfig(@RequestBody MemberPointsConfigDO pointsConfigDO) {
        pointsService.updateMemberPointsConfig(pointsConfigDO);
        return success(true);
    }

    @GetMapping("/getMemberPointsConfigList")
    @Operation(summary = "获取抵扣设置")
    @TenantIgnore
    public CommonResult<List<MemberPointsConfigDO>> getMemberPointsConfigList(@RequestParam(value = "orgType",required = false) Integer orgType,
                                                                              @RequestParam(value = "tenantId",required = false) Long tenantId) {
        tenantId = Objects.isNull(tenantId) ? TenantContextHolder.getTenantId() : tenantId;
        return success(pointsService.getMemberPointsConfigList(orgType,tenantId));
    }

//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出会员用户积分 Excel")
//    @PreAuthorize("@ss.hasPermission('member:points:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportPointsExcel(@Valid PointsPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<PointsDO> list = pointsService.getPointsPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "会员用户积分.xls", "数据", PointsRespVO.class,
//                        BeanUtils.toBean(list, PointsRespVO.class));
//    }

}