package cn.iocoder.yudao.module.property.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.property.entity.PropertyPos;
import cn.iocoder.yudao.module.property.service.PropertyPosService;
import cn.iocoder.yudao.module.property.vo.PropertyPosReqVO;
import cn.iocoder.yudao.module.property.vo.PropertyPosRespVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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 java.util.List;

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

@Tag(name = "管理后台 - 企业管理-资产存放位置")
@RestController
@RequestMapping("/company/property-pos")
@Validated
public class PropertyPosController {

    @Resource
    private PropertyPosService propertyPosService;

    @PostMapping("/create")
    @Operation(summary = "创建企业管理-资产存放位置")
    @PreAuthorize("@ss.hasPermission('company:property-pos:create')")
    public CommonResult<String> createPropertyPos(@RequestBody PropertyPosReqVO createReqVO) {
        return success(propertyPosService.createPropertyPos(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业管理-资产存放位置")
    @PreAuthorize("@ss.hasPermission('company:property-pos:update')")
    public CommonResult<Boolean> updatePropertyPos(@RequestBody PropertyPosReqVO updateReqVO) {
        propertyPosService.updatePropertyPos(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业管理-资产存放位置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('company:property-pos:delete')")
    public CommonResult<Boolean> deletePropertyPos(@RequestParam("id") String id) {
        propertyPosService.deletePropertyPos(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业管理-资产存放位置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('company:property-pos:query')")
    public CommonResult<PropertyPosRespVO> getPropertyPos(@RequestParam("id") String id) {
        PropertyPos propertyPos = propertyPosService.getPropertyPos(id);
        return success(BeanUtils.toBean(propertyPos, PropertyPosRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业管理-资产存放位置分页")
    @PreAuthorize("@ss.hasPermission('company:property-pos:query')")
    public CommonResult<IPage<?>> getPropertyPosPage(PropertyPosReqVO pageReqVO,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<PropertyPos> page = new Page<>(pageNo, pageSize);
        return success(propertyPosService.getPropertyPosPage( page,pageReqVO));
    }

    /**
     * 资产存放位置列表 --下拉选择使用
     * @author vinjor-M
     * @date 17:37 2024/8/14
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage<?>>
    **/
    @GetMapping("/list")
    @Operation(summary = "获得企业管理-资产存放位置列表")
    @PreAuthorize("@ss.hasPermission('company:property-pos:query')")
    public CommonResult<List<?>> getPropertyPosList() {
        return success(propertyPosService.list());
    }

    /**
     * 资产存放位置列表 分不同企业
     * @author 小李
     * @date 17:01 2024/8/17
     * @param id 企业ID
    **/
    @GetMapping("/listByCorpId")
    @Operation(summary = "获得企业管理-资产存放位置列表")
    @PreAuthorize("@ss.hasPermission('company:property-deal:query')")
    public CommonResult<List<PropertyPos>> getPositionByCorpId(String id){
        return success(propertyPosService.list(new LambdaQueryWrapper<PropertyPos>().eq(PropertyPos::getCorpId, id)));
    }
}