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.Blacklist;
import digital.laboratory.platform.reagent.service.BlacklistService;
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;

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

    private final  BlacklistService blacklistService;

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

        Blacklist blacklist = blacklistService.getById(blacklistId);
		return R.ok(blacklist);
		//return R.ok(blacklistService.getById(blacklistId));
	}

	/**
     * 分页查询
     * @param page 分页对象
     * @param blacklist (试剂耗材黑名单)
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('reagent_blacklist_get')" )
    public R<IPage<Blacklist>> getBlacklistPage(Page<Blacklist> page, Blacklist blacklist, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		IPage<Blacklist> blacklistSList = blacklistService.page(page, Wrappers.<Blacklist>query()
				.eq("create_by", dlpUser.getId())
				.orderByDesc("create_time")
		);
		return R.ok(blacklistSList);
//		return R.ok(blacklistService.page(page, Wrappers.query(blacklist)));
    }


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

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

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

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

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

        Blacklist oldBlacklist = blacklistService.getById(blacklistId);

		if (blacklistService.removeById(blacklistId)) {
			return R.ok(oldBlacklist, "对象删除成功");
		}
		else {
			return R.failed(oldBlacklist, "对象删除失败");
		}

    }

}
