package com.unis.module.coding.controller.admin.zjxx;

import com.unis.framework.common.util.kqbm.KQBMUtils;
import com.unis.module.coding.dal.dataobject.zjxx.ZjxxDO;
import com.unis.module.coding.service.zjxx.ZjxxService;
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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.util.object.BeanUtils;
import static com.unis.framework.common.pojo.CommonResult.success;

import com.unis.framework.excel.core.util.ExcelUtils;

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

import com.unis.module.coding.controller.admin.zjxx.vo.*;

@Tag(name = "管理后台 - 质检信息")
@RestController
@RequestMapping("/delivery/zjxx")
@Validated
public class ZjxxController {

    @Resource
    private ZjxxService zjxxService;

    @PostMapping("/create")
    @Operation(summary = "创建质检信息")
    @PreAuthorize("@ss.hasPermission('delivery:zjxx:create')")
    public CommonResult<Long> createZjxx(@Valid @RequestBody ZjxxSaveReqVO createReqVO) {
        return success(zjxxService.createZjxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新质检信息")
    @PreAuthorize("@ss.hasPermission('delivery:zjxx:update')")
    public CommonResult<Boolean> updateZjxx(@Valid @RequestBody ZjxxSaveReqVO updateReqVO) {
        zjxxService.updateZjxx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质检信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('delivery:zjxx:delete')")
    public CommonResult<Boolean> deleteZjxx(@RequestParam("id") Long id) {
        zjxxService.deleteZjxx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质检信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:zjxx:query')")
    public CommonResult<ZjxxRespVO> getZjxx(@RequestParam("id") Long id) {
        ZjxxDO zjxx = zjxxService.getZjxx(id);
        //质检信息的图片字符转-》list
        ZjxxRespVO zjxxRespVO = BeanUtils.toBean(zjxx, ZjxxRespVO.class);
        return success(zjxxRespVO);
    }

    @PostMapping("/page")
    @Operation(summary = "获得质检信息分页")
    @PreAuthorize("@ss.hasPermission('delivery:zjxx:query')")
    public CommonResult<PageResult<ZjxxRespVO>> getZjxxPage(@Valid @RequestBody ZjxxPageReqVO pageReqVO) {
        PageResult<ZjxxDO> pageResult = zjxxService.getZjxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZjxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质检信息 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:zjxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZjxxExcel(@Valid ZjxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZjxxDO> list = zjxxService.getZjxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "质检信息.xls", "数据", ZjxxRespVO.class,
                        BeanUtils.toBean(list, ZjxxRespVO.class));
    }

}