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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.storeevaluate.vo.StoreEvaluatePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storeevaluate.vo.StoreEvaluateRespVO;
import cn.iocoder.yudao.module.member.controller.admin.storeevaluate.vo.StoreEvaluateSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storeevaluate.StoreEvaluateDO;
import cn.iocoder.yudao.module.member.service.storeevaluate.StoreEvaluateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 店铺评价")
@RestController
@RequestMapping("/member/store-evaluate")
@Validated
public class StoreEvaluateController {

    @Resource
    private StoreEvaluateService storeEvaluateService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺评价")
    @PreAuthorize("@ss.hasPermission('member:store-evaluate:create')")
    public CommonResult<Long> createStoreEvaluate(@Valid @RequestBody StoreEvaluateSaveReqVO createReqVO) {
        return success(storeEvaluateService.createStoreEvaluate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺评价")
    @PreAuthorize("@ss.hasPermission('member:store-evaluate:update')")
    public CommonResult<Boolean> updateStoreEvaluate(@Valid @RequestBody StoreEvaluateSaveReqVO updateReqVO) {
        storeEvaluateService.updateStoreEvaluate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺评价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-evaluate:delete')")
    public CommonResult<Boolean> deleteStoreEvaluate(@RequestParam("id") Long id) {
        storeEvaluateService.deleteStoreEvaluate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺评价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-evaluate:query')")
    public CommonResult<StoreEvaluateRespVO> getStoreEvaluate(@RequestParam("id") Long id) {
        StoreEvaluateDO storeEvaluate = storeEvaluateService.getStoreEvaluate(id);
        return success(BeanUtils.toBean(storeEvaluate, StoreEvaluateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺评价分页")
    @PreAuthorize("@ss.hasPermission('member:store-evaluate:query')")
    public CommonResult<PageResult<StoreEvaluateRespVO>> getStoreEvaluatePage(@Valid StoreEvaluatePageReqVO pageReqVO) {
        PageResult<StoreEvaluateDO> pageResult = storeEvaluateService.getStoreEvaluatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreEvaluateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺评价 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-evaluate:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreEvaluateExcel(@Valid StoreEvaluatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreEvaluateDO> list = storeEvaluateService.getStoreEvaluatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺评价.xls", "数据", StoreEvaluateRespVO.class,
                        BeanUtils.toBean(list, StoreEvaluateRespVO.class));
    }




}