package com.yc.ship.module.marketing.controller.admin.column;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
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.module.marketing.controller.admin.column.vo.ColumnPageReqVO;
import com.yc.ship.module.marketing.controller.admin.column.vo.ColumnRespVO;
import com.yc.ship.module.marketing.controller.admin.column.vo.ColumnSaveReqVO;
import com.yc.ship.module.marketing.dal.dataobject.column.ColumnDO;
import com.yc.ship.module.marketing.service.column.ColumnService;
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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 小程序-栏目管理")
@RestController
@RequestMapping("/marketing/column")
@Validated
public class ColumnController {

    @Resource
    private ColumnService columnService;

    @PostMapping("/create")
    @Operation(summary = "创建小程序-栏目管理")
    @PreAuthorize("@ss.hasPermission('marketing:column:create')")
    public CommonResult<String> createColumn(@Valid @RequestBody ColumnSaveReqVO createReqVO) {
        return success(columnService.createColumn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序-栏目管理")
    @PreAuthorize("@ss.hasPermission('marketing:column:update')")
    public CommonResult<Boolean> updateColumn(@Valid @RequestBody ColumnSaveReqVO updateReqVO) {
        columnService.updateColumn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序-栏目管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('marketing:column:delete')")
    public CommonResult<Boolean> deleteColumn(@RequestParam("id") String id) {
        columnService.deleteColumn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序-栏目管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('marketing:column:query')")
    public CommonResult<ColumnRespVO> getColumn(@RequestParam("id") String id) {
        ColumnDO column = columnService.getColumn(id);
        return success(BeanUtils.toBean(column, ColumnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序-栏目管理分页")
    @PreAuthorize("@ss.hasPermission('marketing:column:query')")
    public CommonResult<PageResult<ColumnRespVO>> getColumnPage(@Valid ColumnPageReqVO pageReqVO) {
        PageResult<ColumnDO> pageResult = columnService.getColumnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ColumnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小程序-栏目管理 Excel")
    @PreAuthorize("@ss.hasPermission('marketing:column:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportColumnExcel(@Valid ColumnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ColumnDO> list = columnService.getColumnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "小程序-栏目管理.xls", "数据", ColumnRespVO.class,
                        BeanUtils.toBean(list, ColumnRespVO.class));
    }

}