package digital.laboratory.platform.entrustment.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.log.annotation.SysLog;
import digital.laboratory.platform.entrustment.entity.SampleStore;
import digital.laboratory.platform.entrustment.service.SampleStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 样品库
 *
 * @author Zhang Xiaolong created at  2022-07-06
 * @describe 样品库 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/entrustment/sample_store" )
@Api(value = "sample_store", tags = "样品库管理")
public class SampleStoreController {

    private final  SampleStoreService sampleStoreService;

	/**
	 * 通过id查询样品库
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('entrustment_sample_store_get')" )
	public R getById(@PathVariable("id" ) String id) {
		return R.ok(sampleStoreService.getById(id));
	}

	/**
     * 分页查询
     * @param page 分页对象
     * @param sampleStore 样品库
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('entrustment_sample_store_get')" )
    public R getSampleStorePage(Page page, SampleStore sampleStore) {
        return R.ok(sampleStoreService.page(page, Wrappers.query(sampleStore)));
    }


    /**
     * 新增样品库
     * @param sampleStore 样品库
     * @return R
     */
    @ApiOperation(value = "新增样品库", notes = "新增样品库")
    @SysLog("新增样品库" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('entrustment_sample_store_add')" )
    public R postAddObject(@RequestBody SampleStore sampleStore) {
        sampleStore.setId(IdWorker.get32UUID().toUpperCase());
        return R.ok(sampleStoreService.save(sampleStore));
    }

    /**
     * 修改样品库
     * @param sampleStore 样品库
     * @return R
     */
    @ApiOperation(value = "修改样品库", notes = "修改样品库")
    @SysLog("修改样品库" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('entrustment_sample_store_edit')" )
    public R putUpdateById(@RequestBody SampleStore sampleStore) {
        return R.ok(sampleStoreService.updateById(sampleStore));
    }

    /**
     * 通过id删除样品库
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除样品库", notes = "通过id删除样品库")
    @SysLog("通过id删除样品库" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('entrustment_sample_store_del')" )
    public R deleteById(@PathVariable String id) {
        return R.ok(sampleStoreService.removeById(id));
    }

}
