package com.yc.ship.module.miniapplet.controller.admin.adsense;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.miniapplet.controller.admin.adsense.vo.AdsensePageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.adsense.vo.AdsenseRespVO;
import com.yc.ship.module.miniapplet.controller.admin.adsense.vo.AdsenseSaveReqVO;
import com.yc.ship.module.miniapplet.controller.admin.adsense.vo.AdsenseSimpleRespVO;
import com.yc.ship.module.miniapplet.dal.dataobject.adsense.AdsenseDO;
import com.yc.ship.module.miniapplet.service.adsense.AdsenseService;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 广告位管理")
@RestController
@RequestMapping("/applet/adsense")
@Validated
public class AdsenseController {

    @Resource
    private AdsenseService adsenseService;

    @PostMapping("/create")
    @Operation(summary = "创建广告位管理")
    @PreAuthorize("@ss.hasPermission('applet:adsense:create')")
    public CommonResult<Long> createAdsense(@Valid @RequestBody AdsenseSaveReqVO createReqVO) {
        return success(adsenseService.createAdsense(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告位管理")
    @PreAuthorize("@ss.hasPermission('applet:adsense:update')")
    public CommonResult<Boolean> updateAdsense(@Valid @RequestBody AdsenseSaveReqVO updateReqVO) {
        adsenseService.updateAdsense(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告位管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('applet:adsense:delete')")
    public CommonResult<Boolean> deleteAdsense(@RequestParam("id") Long id) {
        adsenseService.deleteAdsense(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告位管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:adsense:query')")
    public CommonResult<AdsenseRespVO> getAdsense(@RequestParam("id") Long id) {
        AdsenseDO adsense = adsenseService.getAdsense(id);
        return success(BeanUtils.toBean(adsense, AdsenseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告位管理分页")
    @PreAuthorize("@ss.hasPermission('applet:adsense:query')")
    public CommonResult<PageResult<AdsenseRespVO>> getAdsensePage(@Valid AdsensePageReqVO pageReqVO) {
        PageResult<AdsenseDO> pageResult = adsenseService.getAdsensePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdsenseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告位管理 Excel")
    @PreAuthorize("@ss.hasPermission('applet:adsense:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportAdsenseExcel(@Valid AdsensePageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdsenseDO> list = adsenseService.getAdsensePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告位管理.xls", "数据", AdsenseRespVO.class, BeanUtils.toBean(list, AdsenseRespVO.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取门店精简信息列表", description = "只包含被开启的门店，主要用于前端的下拉选项")
    public CommonResult<List<AdsenseSimpleRespVO>> getSimpleList(@RequestParam("status") Integer status) {
        List<AdsenseDO> list = adsenseService.getAdsenseListByStatus(status);
        return success(BeanUtils.toBean(list, AdsenseSimpleRespVO.class));
    }

}
