package com.jojubanking.boot.module.system.controller.admin.orderview;

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.annotations.*;

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

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.orderview.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.orderview.OrderViewDO;
import com.jojubanking.boot.module.system.convert.orderview.OrderViewConvert;
import com.jojubanking.boot.module.system.service.orderview.OrderViewService;

@Api(tags = "管理后台 - VIEW")
@RestController
@RequestMapping("/system/order-view")
@Validated
public class OrderViewController {

    @Resource
    private OrderViewService orderViewService;

    @PostMapping("/create")
    @ApiOperation("创建VIEW")
    @PreAuthorize("@ss.hasPermission('system:order-view:create')")
    public CommonResult<Long> createOrderView(@Valid @RequestBody OrderViewCreateReqVO createReqVO) {
        return success(orderViewService.createOrderView(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新VIEW")
    @PreAuthorize("@ss.hasPermission('system:order-view:update')")
    public CommonResult<Boolean> updateOrderView(@Valid @RequestBody OrderViewUpdateReqVO updateReqVO) {
        orderViewService.updateOrderView(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除VIEW")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:order-view:delete')")
    public CommonResult<Boolean> deleteOrderView(@RequestParam("id") Long id) {
        orderViewService.deleteOrderView(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得VIEW")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:order-view:query')")
    public CommonResult<OrderViewRespVO> getOrderView(@RequestParam("id") Long id) {
        OrderViewDO orderView = orderViewService.getOrderView(id);
        return success(OrderViewConvert.INSTANCE.convert(orderView));
    }

    @GetMapping("/list")
    @ApiOperation("获得VIEW列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:order-view:query')")
    public CommonResult<List<OrderViewRespVO>> getOrderViewList(@RequestParam("ids") Collection<Long> ids) {
        List<OrderViewDO> list = orderViewService.getOrderViewList(ids);
        return success(OrderViewConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得VIEW分页")
    @PreAuthorize("@ss.hasPermission('system:order-view:query')")
    public CommonResult<PageResult<OrderViewRespVO>> getOrderViewPage(@Valid OrderViewPageReqVO pageVO) {
        PageResult<OrderViewDO> pageResult = orderViewService.getOrderViewPage(pageVO);
        return success(OrderViewConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出VIEW Excel")
    @PreAuthorize("@ss.hasPermission('system:order-view:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderViewExcel(@Valid OrderViewExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderViewDO> list = orderViewService.getOrderViewList(exportReqVO);
        // 导出 Excel
        List<OrderViewExcelVO> datas = OrderViewConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "VIEW.xls", "数据", OrderViewExcelVO.class, datas);
    }

}
