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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packageInfo.vo.PackagePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packageInfo.vo.PackageRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packageInfo.vo.PackageSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.packageInfo.PackageDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.size.SizeDO;
import cn.iocoder.yudao.module.smartfactory.service.packageInfo.PackageService;
import cn.iocoder.yudao.module.smartfactory.service.size.SizeService;
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.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 打包线")
@RestController
@RequestMapping("/smartfactory/package")
@Validated
public class PackageController {

    @Resource
    private PackageService packageService;

    @Resource
    private SizeService sizeService;

    @PostMapping("/create")
    @Operation(summary = "创建打包线")
    @PreAuthorize("@ss.hasPermission('smartfactory:package:create')")
    public CommonResult<Long> createPackage(@Valid @RequestBody PackageSaveReqVO createReqVO) {
        return success(packageService.createPackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打包线")
    @PreAuthorize("@ss.hasPermission('smartfactory:package:update')")
    public CommonResult<Boolean> updatePackage(@Valid @RequestBody PackageSaveReqVO updateReqVO) {
        packageService.updatePackage(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得打包线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:package:query')")
    public CommonResult<PackageRespVO> getPackage(@RequestParam("id") Long id) {

        PackageDO packageDO = packageService.getPackage(id);

        //获得规格信息
        SizeDO sizeDo = sizeService.getSize(packageDO.getSizeId());

        PackageRespVO respVO = BeanUtils.toBean(packageDO, PackageRespVO.class);
        respVO.setSizeName(sizeDo.getName());

        return success(respVO);
    }

    @GetMapping("/get-list")
    @Operation(summary = "获得指定工厂的打包线列表")
    @Parameter(name = "factoryId", description = "工厂编号", required = true, example = "1000078")
    @PreAuthorize("@ss.hasPermission('smartfactory:package:query')")
    public CommonResult<List<PackageRespVO>> getPackageByFactoryId(@RequestParam("factoryId") Long factoryId) {

        return success(packageService.getPackageByFactoryId(factoryId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打包线分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:package:query')")
    public CommonResult<PageResult<PackageRespVO>> getPackagePage(@Valid PackagePageReqVO pageReqVO) {

        return success(packageService.getPackagePage(pageReqVO));
    }
}