package cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.packagedata.PackageDataDO;
import cn.iocoder.yudao.module.smartfactory.service.packagedata.PackageDataService;
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 cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 打包线数据")
@RestController
@RequestMapping("/factory/package-data")
@Validated
public class PackageDataController {

    @Resource
    private PackageDataService packageDataService;

    @PostMapping("/create")
    @Operation(summary = "创建打包线数据")
    @PreAuthorize("@ss.hasPermission('factory:package-data:create')")
    public CommonResult<Long> createPackageData(@Valid @RequestBody PackageDataSaveReqVO createReqVO) {
        return success(packageDataService.createPackageData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打包线数据")
    @PreAuthorize("@ss.hasPermission('factory:package-data:update')")
    public CommonResult<Boolean> updatePackageData(@Valid @RequestBody PackageDataSaveReqVO updateReqVO) {
        packageDataService.updatePackageData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除打包线数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('factory:package-data:delete')")
    public CommonResult<Boolean> deletePackageData(@RequestParam("id") Long id) {
        packageDataService.deletePackageData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得打包线数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:package-data:query')")
    public CommonResult<PackageDataRespVO> getPackageData(@RequestParam("id") Long id) {
        PackageDataDO packageData = packageDataService.getPackageData(id);
        return success(BeanUtils.toBean(packageData, PackageDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打包线数据分页")
    @PreAuthorize("@ss.hasPermission('factory:package-data:query')")
    public CommonResult<PageResult<PackageDataRespVO>> getPackageDataPage(@Valid PackageDataPageReqVO pageReqVO) {
        PageResult<PackageDataDO> pageResult = packageDataService.getPackageDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PackageDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打包线数据 Excel")
    @PreAuthorize("@ss.hasPermission('factory:package-data:export')")
    @OperateLog(type = EXPORT)
    public void exportPackageDataExcel(@Valid PackageDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PackageDataDO> list = packageDataService.getPackageDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打包线数据.xls", "数据", PackageDataRespVO.class,
                        BeanUtils.toBean(list, PackageDataRespVO.class));
    }

}