package digital.laboratory.platform.reagent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.reagent.entity.ReagentConsumableInventory;
import digital.laboratory.platform.reagent.service.ReagentConsumableInventoryService;
import digital.laboratory.platform.reagent.vo.ReagentConsumableInventoryVO;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.util.List;

/**
 * 试剂耗材库存
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe 试剂耗材库存 前端控制器
 *
 * 这是与表示层的接口, 不应该接业务逻辑写在这里, 业务逻辑应该写在 service 中
 * 这里写什么:
 *     为前端提供数据, 接受前端的数据
 *     为前端提供的数据, 从 service 取得后, 可以做一些适当的加工, 这种加工不是业务层面的, 只能是数据格式上, 为方便前端处理
 *     接受前端的数据, 每一个函数的参数可以先做一些整理后, 再调用 service 中的函数。这里对参数的整理, 应该只是格式上的, 而不能是业务上的
 * 数据层在 mapper 中, 数据层不涉及业务, 只管技术上的 对象<->表 之间的转换
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/reagent_consumable_inventory" )
@Api(value = "reagent_consumable_inventory", tags = "试剂耗材库存管理")
public class ReagentConsumableInventoryController {

    private final  ReagentConsumableInventoryService reagentConsumableInventoryService;

	/**
	 * 通过id查询试剂耗材库存
	 * @param reagentConsumableInventoryId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{reagentConsumableInventoryId}" )
	@PreAuthorize("@pms.hasPermission('reagent_reagent_consumable_inventory_get')" )
	public R<ReagentConsumableInventory> getById(@PathVariable("reagentConsumableInventoryId" ) String reagentConsumableInventoryId, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ReagentConsumableInventory reagentConsumableInventory = reagentConsumableInventoryService.getById(reagentConsumableInventoryId);
		return R.ok(reagentConsumableInventory);
		//return R.ok(reagentConsumableInventoryService.getById(reagentConsumableInventoryId));
	}

	/**

     * @param reagentConsumableInventory 试剂耗材库存
     * @return
     */
    @ApiOperation(value = "耗材列表集合", notes = "耗材列表集合")
    @GetMapping("/RList" )
//    @PreAuthorize("@pms.hasPermission('reagent_reagent_consumable_inventory_get')" )
    public R<List<ReagentConsumableInventoryVO>> getReagentConsumableInventoryPage(Page<ReagentConsumableInventory> page, ReagentConsumableInventory reagentConsumableInventory, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		List<ReagentConsumableInventoryVO> reagentConsumableInventorySList = reagentConsumableInventoryService.getReagentConsumableInventoryVOList();
		return R.ok(reagentConsumableInventorySList);
//		return R.ok(reagentConsumableInventoryService.page(page, Wrappers.query(reagentConsumableInventory)));
    }

	/**
	 * 分页查询
	 *
	 * @return
	 */
	@ApiOperation(value = "标准物质列表", notes = "标准物质列表")
	@GetMapping("/MList" )
//    @PreAuthorize("@pms.hasPermission('reagent_reagent_consumable_inventory_get')" )
	public R<List<ReagentConsumableInventoryVO>> getReferenceMaterialVOList(HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		List<ReagentConsumableInventoryVO> referenceMaterialVOList = reagentConsumableInventoryService.getReferenceMaterialVOList();
		return R.ok(referenceMaterialVOList);
//		return R.ok(reagentConsumableInventoryService.page(page, Wrappers.query(reagentConsumableInventory)));
	}


    /**
     * 新增试剂耗材库存
     * @param reagentConsumableInventory 试剂耗材库存
     * @return R
     */
    @ApiOperation(value = "新增试剂耗材库存", notes = "新增试剂耗材库存")
    @SysLog("新增试剂耗材库存" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_reagent_consumable_inventory_add')" )
    public R<ReagentConsumableInventory> postAddObject(@RequestBody ReagentConsumableInventory reagentConsumableInventory, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        reagentConsumableInventory.setReagentConsumableInventoryId(IdWorker.get32UUID().toUpperCase());
		if (reagentConsumableInventoryService.save(reagentConsumableInventory)) {
			return R.ok(reagentConsumableInventory, "对象创建成功");
		}
		else {
			return R.failed(reagentConsumableInventory, "对象创建失败");
		}
    }

    /**
     * 修改试剂耗材库存
     * @param reagentConsumableInventory 试剂耗材库存
     * @return R
     */
    @ApiOperation(value = "修改试剂耗材库存", notes = "修改试剂耗材库存")
    @SysLog("修改试剂耗材库存" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('reagent_reagent_consumable_inventory_edit')" )
    public R<ReagentConsumableInventory> putUpdateById(@RequestBody ReagentConsumableInventory reagentConsumableInventory, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		if (reagentConsumableInventoryService.updateById(reagentConsumableInventory)) {
			return R.ok(reagentConsumableInventory, "保存对象成功");
		}
		else {
			return R.failed(reagentConsumableInventory, "保存对象失败");
		}
    }

    /**
     * 通过id删除试剂耗材库存
     * @param reagentConsumableInventoryId id
     * @return R
     */
    @ApiOperation(value = "通过id删除试剂耗材库存", notes = "通过id删除试剂耗材库存")
    @SysLog("通过id删除试剂耗材库存" )
    @DeleteMapping("/{reagentConsumableInventoryId}" )
//    @PreAuthorize("@pms.hasPermission('reagent_reagent_consumable_inventory_del')" )
    public R<ReagentConsumableInventory> deleteById(@PathVariable String reagentConsumableInventoryId, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        ReagentConsumableInventory oldReagentConsumableInventory = reagentConsumableInventoryService.getById(reagentConsumableInventoryId);

		if (reagentConsumableInventoryService.removeById(reagentConsumableInventoryId)) {
			return R.ok(oldReagentConsumableInventory, "对象删除成功");
		}
		else {
			return R.failed(oldReagentConsumableInventory, "对象删除失败");
		}

    }

}
