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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.supplier.entity.BaseSupplier;
import cn.iocoder.yudao.module.supplier.service.BaseSupplierService;
import cn.iocoder.yudao.module.supplier.vo.BaseSupplierPageReqVO;
import cn.iocoder.yudao.module.supplier.vo.BaseSupplierRespVO;
import cn.iocoder.yudao.module.supplier.vo.BaseSupplierSaveReqVO;
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 javax.validation.Valid;

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

@Tag(name = "管理后台 - 供应商")
@RestController
@RequestMapping("/supplier/baseSupplier")
@Validated
public class BaseSupplierController {

    @Resource
    private BaseSupplierService baseSupplierService;

    /**
     * 新增供应商
     *
     * @param saveReqVO BaseSupplierSaveReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.String>
     * @author PQZ
     * @date 11:00 2024/9/11
     **/
    @PostMapping("/create")
    @Operation(summary = "创建供应商")
    @PreAuthorize("@ss.hasPermission('supplier:base-supplier:create')")
    public CommonResult<Boolean> createBaseSupplier(@Valid @RequestBody BaseSupplierSaveReqVO saveReqVO) {
        baseSupplierService.saveBaseSupplier(saveReqVO);
        return success(true);
    }

    /**
     * 更新供应商
     *
     * @param saveReqVO BaseSupplierSaveReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 11:14 2024/9/11
     **/
    @PutMapping("/update")
    @Operation(summary = "更新供应商")
    @PreAuthorize("@ss.hasPermission('supplier:base-supplier:update')")
    public CommonResult<Boolean> updateBaseSupplier(@Valid @RequestBody BaseSupplierSaveReqVO saveReqVO) {
        baseSupplierService.saveBaseSupplier(saveReqVO);
        return success(true);
    }

    /**
     * 删除供应商
     *
     * @param id 供应商id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 10:04 2024/9/12
     **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('supplier:base-supplier:delete')")
    public CommonResult<Boolean> deleteBaseSupplier(@RequestParam("id") String id) {
        baseSupplierService.deleteBaseSupplier(id);
        return success(true);
    }

    /**
     * 获取供应商
     *
     * @param id 供应商id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.supplier.vo.BaseSupplierRespVO>
     * @author PQZ
     * @date 10:04 2024/9/12
     **/
    @GetMapping("/get")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:base-supplier:query')")
    public CommonResult<BaseSupplierRespVO> getBaseSupplier(@RequestParam("id") String id) {
        BaseSupplier baseSupplier = baseSupplierService.getBaseSupplier(id);
        return success(BeanUtils.toBean(baseSupplier, BaseSupplierRespVO.class));
    }

    /**
     *
     * @author PQZ
     * @date 10:05 2024/9/12
     * @param pageReqVO BaseSupplierPageReqVO 实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.framework.common.pojo.PageResult<cn.iocoder.yudao.module.supplier.vo.BaseSupplierRespVO>>
    **/
    @GetMapping("/page")
    @Operation(summary = "获得供应商分页")
    @PreAuthorize("@ss.hasPermission('supplier:base-supplier:query')")
    public CommonResult<IPage<?>> getBaseSupplierPage(BaseSupplierPageReqVO pageReqVO,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<BaseSupplierRespVO> page = new Page<>(pageNo, pageSize);
        return success(baseSupplierService.queryListPage(pageReqVO,page));
    }

    /**
     * 获得所有供应商
     * @author 小李
     * @date 9:23 2024/9/13
    **/
    @GetMapping("/list")
    @Operation(summary = "获得所有供应商")
    public CommonResult<?> getBaseSupplierList(){
        return success(baseSupplierService.list());
    }

    /**
     * 根据输入名称模糊查询供应商
     * @author 小李
     * @date 9:23 2024/9/13
     **/
    @GetMapping("/searchList")
    @Operation(summary = "根据输入名称模糊查询供应商")
    public CommonResult<?> searchList(String name){
        LambdaQueryWrapper<BaseSupplier> queryWrapper = new LambdaQueryWrapper<BaseSupplier>()
                .like(BaseSupplier::getName,name);
        return success(baseSupplierService.list(queryWrapper));
    }
}