package digital.laboratory.platform.reagent.controller;

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.CategoryTable;
import digital.laboratory.platform.reagent.service.CategoryTableService;
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 java.security.Principal;
import java.util.List;

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

    private final CategoryTableService categoryTableService;

	/**
	 * 列表查询
	 * * @param type
	 * @return R
	 */
	@SysLog("类别显示种类" )
	@ApiOperation(value = "列表显示种类", notes = "列表显示种类")
	@GetMapping("/species")
//	@PreAuthorize("@pms.hasPermission('reagent_type_table_get')" )
	public R<List<CategoryTable>> getSpecies(String category, HttpServletRequest theHttpServletRequest) {

		Principal principal = theHttpServletRequest.getUserPrincipal();

		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		List<CategoryTable> categoryTableList = categoryTableService.getSpecies(category);

		return R.ok(categoryTableList);

	}


//
//    /**
//     * 新增
//     * @param type
//     * @return R
//     */
//    @ApiOperation(value = "新增类别", notes = "新增类别")
//    @SysLog("新增类别" )
//    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_type_table_add')" )
//    public R<TypeTable> postAddObject(@PathVariable String  type, HttpServletRequest theHttpServletRequest) {
//		Principal principal = theHttpServletRequest.getUserPrincipal();
//		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
//
//		TypeTable typeTable = typeTableService.addType(type);
//
//		if (typeTable!=null) {
//			return R.ok(typeTable, "保存成功");
//		}
//		else {
//			return R.failed( "保存失败");
//		}
//    }

	/**
	 * 新增
	 * @param species
	 * @return R
	 */
	@ApiOperation(value = "新增种类", notes = "新增种类")
	@SysLog("新增类别" )
	@PostMapping("species")
//	@PreAuthorize("@pms.hasPermission('reagent_type_table_add')" )
	public R<CategoryTable> postAdd(@PathVariable String  category, @PathVariable String  species, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		CategoryTable categoryTable = categoryTableService.addSpecies(category,species);

		if (categoryTable !=null) {
			return R.ok(categoryTable, "保存成功");
		}
		else {
			return R.failed( "保存失败");
		}
	}


	/**
	 * 通过id删除种类
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除种类", notes = "通过id删除种类")
	@SysLog("通过id删除种类" )
	@DeleteMapping("/species/{id}" )
//	@PreAuthorize("@pms.hasPermission('reagent_type_table_del')" )
	public R<String > deleteSpeciesById(@PathVariable String id, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
		if (categoryTableService.delSpeciesById(id)) {
			return R.ok( "删除成功");
		}
		else {
			return R.failed("删除失败");
		}

	}

}
