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

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.activitymerchant.vo.ActivityMerchantPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activitymerchant.vo.ActivityMerchantRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activitymerchant.vo.ActivityMerchantSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitymerchant.ActivityMerchantDO;
import cn.iocoder.yudao.module.member.service.activitymerchant.ActivityMerchantService;
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.*;

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/activity-merchant")
@Validated
public class ActivityMerchantController {

    @Resource
    private ActivityMerchantService activityMerchantService;

    @PostMapping("/create")
    @Operation(summary = "创建活动商家")
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:create')")
    public CommonResult<Long> createActivityMerchant(@Valid @RequestBody ActivityMerchantSaveReqVO createReqVO) {
        return success(activityMerchantService.createActivityMerchant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动商家")
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:update')")
    public CommonResult<Boolean> updateActivityMerchant(@Valid @RequestBody ActivityMerchantSaveReqVO updateReqVO) {
        activityMerchantService.updateActivityMerchant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动商家")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:delete')")
    public CommonResult<Boolean> deleteActivityMerchant(@RequestParam("id") Long id) {
        activityMerchantService.deleteActivityMerchant(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动商家")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:query')")
    public CommonResult<ActivityMerchantRespVO> getActivityMerchant(@RequestParam("id") Long id) {
        ActivityMerchantDO activityMerchant = activityMerchantService.getActivityMerchant(id);
        return success(BeanUtils.toBean(activityMerchant, ActivityMerchantRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动商家分页")
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:query')")
    public CommonResult<PageResult<ActivityMerchantRespVO>> getActivityMerchantPage(@Valid ActivityMerchantPageReqVO pageReqVO) {
        PageResult<ActivityMerchantDO> pageResult = activityMerchantService.getActivityMerchantPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityMerchantRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得活动商家列表")
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:query')")
    public CommonResult<List<ActivityMerchantRespVO>> getActivityMerchantList(@Valid ActivityMerchantPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityMerchantDO> list = activityMerchantService.getActivityMerchantPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ActivityMerchantRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动商家 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-merchant:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityMerchantExcel(@Valid ActivityMerchantPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityMerchantDO> list = activityMerchantService.getActivityMerchantPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动商家.xls", "数据", ActivityMerchantRespVO.class,
                        BeanUtils.toBean(list, ActivityMerchantRespVO.class));
    }

}