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

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.module.miniapplet.controller.admin.brand.vo.AppletBrandPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.brand.vo.AppletBrandRespVO;
import com.yc.ship.module.miniapplet.controller.admin.brand.vo.AppletBrandSaveReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.brand.AppletBrandDO;
import com.yc.ship.module.miniapplet.service.brand.AppletBrandService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 小程序系统配置")
@RestController
@RequestMapping("/applet/brand")
@Validated
public class AppletBrandController {

    @Resource
    private AppletBrandService brandService;

    @PostMapping("/create")
    @Operation(summary = "创建小程序系统配置")
    @PreAuthorize("@ss.hasPermission('applet:brand:create')")
    public CommonResult<Long> createBrand(@Valid @RequestBody AppletBrandSaveReqVO createReqVO) {
        return success(brandService.createBrand(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序系统配置")
    @PreAuthorize("@ss.hasPermission('applet:brand:update')")
    public CommonResult<Boolean> updateBrand(@Valid @RequestBody AppletBrandSaveReqVO updateReqVO) {
        brandService.updateBrand(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序系统配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('applet:brand:delete')")
    public CommonResult<Boolean> deleteBrand(@RequestParam("id") Long id) {
        brandService.deleteBrand(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序系统配置")
    @PreAuthorize("@ss.hasPermission('applet:brand:query')")
    public CommonResult<AppletBrandRespVO> getBrand() {
        AppletBrandDO brand = brandService.getBrand();
        return success(BeanUtils.toBean(brand, AppletBrandRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序系统配置分页")
    @PreAuthorize("@ss.hasPermission('applet:brand:query')")
    public CommonResult<PageResult<AppletBrandRespVO>> getBrandPage(@Valid AppletBrandPageReqVO pageReqVO) {
        PageResult<AppletBrandDO> pageResult = brandService.getBrandPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppletBrandRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小程序系统配置 Excel")
    @PreAuthorize("@ss.hasPermission('applet:brand:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBrandExcel(@Valid AppletBrandPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppletBrandDO> list = brandService.getBrandPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "小程序系统配置.xls", "数据", AppletBrandRespVO.class,
                        BeanUtils.toBean(list, AppletBrandRespVO.class));
    }

}