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

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 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.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.socialstatuschangelog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.socialstatuschangelog.SocialStatusChangeLogDO;
import cn.newfeifan.mall.module.distri.service.socialstatuschangelog.SocialStatusChangeLogService;

@Tag(name = "管理后台 - 会员身价变化日志")
@RestController
@RequestMapping("/distri/social-status-change-log")
@Validated
public class SocialStatusChangeLogController {

    @Resource
    private SocialStatusChangeLogService socialStatusChangeLogService;

    @PostMapping("/create")
    @Operation(summary = "创建会员身价变化日志")
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:create')")
    public CommonResult<Long> createSocialStatusChangeLog(@Valid @RequestBody SocialStatusChangeLogSaveReqVO createReqVO) {
        return success(socialStatusChangeLogService.createSocialStatusChangeLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员身价变化日志")
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:update')")
    public CommonResult<Boolean> updateSocialStatusChangeLog(@Valid @RequestBody SocialStatusChangeLogSaveReqVO updateReqVO) {
        socialStatusChangeLogService.updateSocialStatusChangeLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员身价变化日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:delete')")
    public CommonResult<Boolean> deleteSocialStatusChangeLog(@RequestParam("id") Long id) {
        socialStatusChangeLogService.deleteSocialStatusChangeLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员身价变化日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:query')")
    public CommonResult<SocialStatusChangeLogRespVO> getSocialStatusChangeLog(@RequestParam("id") Long id) {
        SocialStatusChangeLogDO socialStatusChangeLog = socialStatusChangeLogService.getSocialStatusChangeLog(id);
        return success(BeanUtils.toBean(socialStatusChangeLog, SocialStatusChangeLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员身价变化日志分页")
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:query')")
    public CommonResult<PageResult<SocialStatusChangeLogRespVO>> getSocialStatusChangeLogPage(@Valid SocialStatusChangeLogPageReqVO pageReqVO) {
        PageResult<SocialStatusChangeLogDO> pageResult = socialStatusChangeLogService.getSocialStatusChangeLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SocialStatusChangeLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员身价变化日志 Excel")
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:export')")
    @OperateLog(type = EXPORT)
    public void exportSocialStatusChangeLogExcel(@Valid SocialStatusChangeLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SocialStatusChangeLogDO> list = socialStatusChangeLogService.getSocialStatusChangeLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员身价变化日志.xls", "数据", SocialStatusChangeLogRespVO.class,
                        BeanUtils.toBean(list, SocialStatusChangeLogRespVO.class));
    }

    @GetMapping("/getUserSignLog")
    @Operation(summary = "获得会员身价变化日志分页")
    @PreAuthorize("@ss.hasPermission('distri:social-status-change-log:create')")
    public CommonResult<PageResult<UserSignSocialLogRespVO>> getUserSignLog(@Valid SocialStatusChangeLogPageReqVO pageReqVO) {
        return success(socialStatusChangeLogService.getUserSignLog(pageReqVO));
    }

}