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

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.activity.vo.ActivityPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activity.vo.ActivityRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activity.vo.ActivitySaveReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityrestaurant.vo.ActivityRestaurantRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activity.ActivityDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityrestaurant.ActivityRestaurantDO;
import cn.iocoder.yudao.module.member.service.activity.ActivityService;
import cn.iocoder.yudao.module.member.service.activityrestaurant.ActivityRestaurantService;
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")
@Validated
public class ActivityController {

    @Resource
    private ActivityService activityService;
    @Resource
    private ActivityRestaurantService activityRestaurantService;

    @PostMapping("/create")
    @Operation(summary = "创建活动")
    public CommonResult<Long> createActivity(@Valid @RequestBody ActivitySaveReqVO createReqVO) {
        return success(activityService.createActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动")
    @PreAuthorize("@ss.hasPermission('member:activity:update')")
    public CommonResult<Boolean> updateActivity(@Valid @RequestBody ActivitySaveReqVO updateReqVO) {
        activityService.updateActivity(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity:query')")
    public CommonResult<ActivityRespVO> getActivity(@RequestParam("id") Long id) {
        ActivityDO activity = activityService.getActivity(id);
        ActivityRespVO bean = BeanUtils.toBean(activity, ActivityRespVO.class);
        ActivityRestaurantDO activityRestaurantDO = activityRestaurantService.getByActivityId(id);
        bean.setActivityRestaurantSaveDto(BeanUtils.toBean(activityRestaurantDO, ActivityRestaurantRespVO.class));
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动分页")
    @PreAuthorize("@ss.hasPermission('member:activity:query')")
    public CommonResult<PageResult<ActivityRespVO>> getActivityPage(@Valid ActivityPageReqVO pageReqVO) {
        PageResult<ActivityRespVO> pageResult = activityService.getActivityPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityExcel(@Valid ActivityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityRespVO> list = activityService.getActivityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动.xls", "数据", ActivityRespVO.class,
                list);
    }

}