package com.yc.ship.module.otc.controller.admin.print;

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.otc.controller.admin.print.vo.PrintPageReqVO;
import com.yc.ship.module.otc.controller.admin.print.vo.PrintRespVO;
import com.yc.ship.module.otc.controller.admin.print.vo.PrintSaveReqVO;
import com.yc.ship.module.otc.controller.admin.print.vo.PrintSimpleVO;
import com.yc.ship.module.otc.dal.dataobject.print.PrintDO;
import com.yc.ship.module.otc.service.print.PrintService;
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("/otc/print")
@Validated
public class PrintController {

    @Resource
    private PrintService printService;

    @PostMapping("/create")
    @Operation(summary = "创建打印模板")
    @PreAuthorize("@ss.hasPermission('otc:print:create')")
    public CommonResult<Long> create(@Valid @RequestBody PrintSaveReqVO createReqVO) {
        return success(printService.createPrint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打印模板")
    @PreAuthorize("@ss.hasPermission('otc:print:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody PrintSaveReqVO updateReqVO) {
        printService.updatePrint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除打印模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:print:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        printService.deletePrint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得打印模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('otc:print:query')")
    public CommonResult<PrintRespVO> get(@RequestParam("id") Long id) {
        PrintDO print = printService.getPrint(id);
        return success(BeanUtils.toBean(print, PrintRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打印模板分页")
//    @PreAuthorize("@ss.hasPermission('otc:print:query')")
    public CommonResult<PageResult<PrintRespVO>> getPage(@Valid PrintPageReqVO pageReqVO) {
        PageResult<PrintDO> pageResult = printService.getPrintPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PrintRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打印模板 Excel")
    @PreAuthorize("@ss.hasPermission('otc:print:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportExcel(@Valid PrintPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PrintDO> list = printService.getPrintPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打印模板.xls", "数据", PrintRespVO.class,
                BeanUtils.toBean(list, PrintRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得打印模板列表")
    public CommonResult<List<PrintSimpleVO>> getSimpleList() {
        List<PrintDO> list = printService.getPrintList();
        return success(BeanUtils.toBean(list, PrintSimpleVO.class));
    }

}
