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

import com.unis.module.coding.controller.admin.hwk.vo.HwkPageReqVO;
import com.unis.module.coding.controller.admin.hwk.vo.HwkRespVO;
import com.unis.module.coding.controller.admin.hwk.vo.HwkSaveReqVO;
import com.unis.module.coding.dal.dataobject.hwk.HwkDO;
import com.unis.module.coding.service.hwk.HwkService;
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.*;


@Tag(name = "管理后台 - 货位卡信息")
@RestController
@RequestMapping("/coding/hwk")
@Validated
public class HwkController {

    @Resource
    private HwkService hwkService;

    @PostMapping("/create")
    @Operation(summary = "创建货位卡信息")
    @PreAuthorize("@ss.hasPermission('coding:hwk:create')")
    public CommonResult<Long> createHwk(@Valid @RequestBody HwkSaveReqVO createReqVO) {
        return success(hwkService.createHwk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新货位卡信息")
    @PreAuthorize("@ss.hasPermission('coding:hwk:update')")
    public CommonResult<Boolean> updateHwk(@Valid @RequestBody HwkSaveReqVO updateReqVO) {
        hwkService.updateHwk(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除货位卡信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('coding:hwk:delete')")
    public CommonResult<Boolean> deleteHwk(@RequestParam("id") Long id) {
        hwkService.deleteHwk(id);
        return success(true);
    }
    @GetMapping("/getInfoByCfId")
    @Operation(summary = "根据仓房id获得货位卡信息")
    @Parameter(name = "cfId", description = "仓房编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:hwk:queryByCfId')")
    public CommonResult<HwkRespVO> getHwkByCfId(@RequestParam("id") Long cfId) {
        HwkDO hwk = hwkService.getHwkByCfId(cfId);
        return success(BeanUtils.toBean(hwk, HwkRespVO.class));
    }
    @GetMapping("/get")
    @Operation(summary = "获得货位卡信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:hwk:query')")
    public CommonResult<HwkRespVO> getHwk(@RequestParam("id") Long id) {
        HwkDO hwk = hwkService.getHwk(id);
        return success(BeanUtils.toBean(hwk, HwkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得货位卡信息分页")
    @PreAuthorize("@ss.hasPermission('coding:hwk:query')")
    public CommonResult<PageResult<HwkRespVO>> getHwkPage(@Valid HwkPageReqVO pageReqVO) {
        PageResult<HwkDO> pageResult = hwkService.getHwkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HwkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出货位卡信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:hwk:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHwkExcel(@Valid HwkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HwkDO> list = hwkService.getHwkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "货位卡信息.xls", "数据", HwkRespVO.class,
                        BeanUtils.toBean(list, HwkRespVO.class));
    }

}