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

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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.storecomplain.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storecomplain.StoreComplainDO;
import cn.iocoder.yudao.module.member.service.storecomplain.StoreComplainService;

@Tag(name = "管理后台 - 投诉")
@RestController
@RequestMapping("/member/store-complain")
@Validated
public class StoreComplainController {

    @Resource
    private StoreComplainService storeComplainService;

    @PostMapping("/create")
    @Operation(summary = "创建投诉")
    @PreAuthorize("@ss.hasPermission('member:store-complain:create')")
    public CommonResult<Long> createStoreComplain(@Valid @RequestBody StoreComplainSaveReqVO createReqVO) {
        return success(storeComplainService.createStoreComplain(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉")
    @PreAuthorize("@ss.hasPermission('member:store-complain:update')")
    public CommonResult<Boolean> updateStoreComplain(@Valid @RequestBody StoreComplainSaveReqVO updateReqVO) {
        storeComplainService.updateStoreComplain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投诉")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-complain:delete')")
    public CommonResult<Boolean> deleteStoreComplain(@RequestParam("id") Long id) {
        storeComplainService.deleteStoreComplain(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投诉")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-complain:query')")
    public CommonResult<StoreComplainRespVO> getStoreComplain(@RequestParam("id") Long id) {
        StoreComplainDO storeComplain = storeComplainService.getStoreComplain(id);
        return success(BeanUtils.toBean(storeComplain, StoreComplainRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投诉分页")
    @PreAuthorize("@ss.hasPermission('member:store-complain:query')")
    public CommonResult<PageResult<StoreComplainRespVO>> getStoreComplainPage(@Valid StoreComplainPageReqVO pageReqVO) {
        PageResult<StoreComplainDO> pageResult = storeComplainService.getStoreComplainPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreComplainRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投诉 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-complain:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreComplainExcel(@Valid StoreComplainPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreComplainDO> list = storeComplainService.getStoreComplainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投诉.xls", "数据", StoreComplainRespVO.class,
                        BeanUtils.toBean(list, StoreComplainRespVO.class));
    }

}