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.service.SampleService;
import digital.laboratory.platform.sys.entity.entrustment.Sample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 检验用的样本
 *
 * @author Zhang Xiaolong created at  2022-07-26
 * @describe 检验用的样本 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/entrustment/sample" )
@Api(value = "sample", tags = "200-检验用的样本管理")
public class SampleController {

    private final  SampleService sampleService;

	/**
	 * 通过id查询检验用的样本
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('entrustment_sample_get')" )
	public R getById(@PathVariable("id" ) String id) {
		return R.ok(sampleService.getById(id));
	}

	/**
     * 分页查询
     * @param page 分页对象
     * @param sample 检验用的样本
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('entrustment_sample_get')" )
    public R getSamplePage(Page page, Sample sample) {
        return R.ok(sampleService.page(page, Wrappers.query(sample)));
    }


    /**
     * 新增检验用的样本
     * @param sample 检验用的样本
     * @return R
     */
    @ApiOperation(value = "新增检验用的样本", notes = "新增检验用的样本")
    @SysLog("新增检验用的样本" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('entrustment_sample_add')" )
    public R postAddObject(@RequestBody Sample sample) {
        sample.setId(IdWorker.get32UUID().toUpperCase());

	    if (sampleService.save(sample)) {
		    return R.ok(sample, "新增样本成功");
	    }
	    else {
		    return R.failed(sample, "新增样本失败");
	    }

    }

    /**
     * 修改检验用的样本
     * @param sample 检验用的样本
     * @return R
     */
    @ApiOperation(value = "修改检验用的样本", notes = "修改检验用的样本")
    @SysLog("修改检验用的样本" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('entrustment_sample_edit')" )
    public R putUpdateById(@RequestBody Sample sample) {
        return R.ok(sampleService.updateById(sample));
    }

    /**
     * 通过id删除检验用的样本
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除检验用的样本", notes = "通过id删除检验用的样本")
    @SysLog("通过id删除检验用的样本" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('entrustment_sample_del')" )
    public R deleteById(@PathVariable String id) {
        return R.ok(sampleService.removeById(id));
    }

}
