package com.zt.plat.module.base.controller.admin.materialhasproperties;

import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.materialhasproperties.vo.MaterialHasPropertiesBatchSaveReqVO;
import com.zt.plat.module.base.controller.admin.materialhasproperties.vo.MaterialHasPropertiesBatchSaveRespVO;
import com.zt.plat.module.base.controller.admin.materialhasproperties.vo.MaterialHasPropertiesPageReqVO;
import com.zt.plat.module.base.controller.admin.materialhasproperties.vo.MaterialHasPropertiesRespVO;
import com.zt.plat.module.base.dal.dataobject.materialhasproperties.MaterialHasPropertiesDO;
import com.zt.plat.module.base.service.materialhasproperties.MaterialHasPropertiesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 部门私有物料持有属性")
@RestController
@RequestMapping("/base/material-has-properties-dept")
@Validated
public class MaterialHasPropertiesDeptController {


    @Resource
    private MaterialHasPropertiesService materialHasPropertiesService;

    @GetMapping("/page")
    @Operation(summary = "获得物料持有属性分页")
    @PreAuthorize("@ss.hasPermission('base:material-has-properties-dept:query')")
    public CommonResult<PageResult<MaterialHasPropertiesRespVO>> getMaterialHasPropertiesPage(@Valid MaterialHasPropertiesPageReqVO pageReqVO) {
        Long deptId = pageReqVO.getDeptId();
        if (deptId == null) {
            throw new ServiceException(401, "部门ID不能为空");
        }
        PageResult<MaterialHasPropertiesDO> pageResult = materialHasPropertiesService.getMaterialHasPropertiesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialHasPropertiesRespVO.class));
    }

    @PostMapping("/batch-save")
    @Operation(summary = "批量保存物料持有属性（全量替换）")
    @PreAuthorize("@ss.hasPermission('base:material-has-properties-dept:update')")
    public CommonResult<MaterialHasPropertiesBatchSaveRespVO> batchSave(@Valid @RequestBody MaterialHasPropertiesBatchSaveReqVO reqVO) {
        Long deptId = reqVO.getDeptId();
        if (deptId == null) {
            throw new ServiceException(500, "部门ID不能为空");
        }
        MaterialHasPropertiesBatchSaveRespVO resp = materialHasPropertiesService.batchSave(reqVO, deptId);
        return success(resp);
    }

}