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.dto.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.dto.PeriodVerificationImplementationDTO;
import digital.laboratory.platform.reagent.entity.PeriodVerificationImplementation;
import digital.laboratory.platform.reagent.service.PeriodVerificationImplementationService;
import digital.laboratory.platform.reagent.vo.PeriodVerificationImplementationVO;
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("/period_verification_implementation" )
@Api(value = "period_verification_implementation", tags = "(标准物质期间核查实施情况及结果记录表)管理")
public class PeriodVerificationImplementationController {

    private final  PeriodVerificationImplementationService periodVerificationImplementationService;

	/**
	 * 通过id查询(标准物质期间核查实施情况及结果记录表)
	 * @param periodVerificationImplementationId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{periodVerificationImplementationId}" )
//	@PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_get')" )
	public R<PeriodVerificationImplementation> getById(@PathVariable("periodVerificationImplementationId" ) String periodVerificationImplementationId, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        PeriodVerificationImplementation periodVerificationImplementation = periodVerificationImplementationService.getPeriodVerificationImplementationVO(periodVerificationImplementationId);
		return R.ok(periodVerificationImplementation);
		//return R.ok(periodVerificationImplementationService.getById(periodVerificationImplementationId));
	}

	/**
     * 分页查询
     * @param page 分页对象
     * @param periodVerificationImplementation (标准物质期间核查实施情况及结果记录表)
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_get')" )
    public R<IPage<PeriodVerificationImplementationVO>> getPeriodVerificationImplementationPage(Page<PeriodVerificationImplementation> page, PeriodVerificationImplementation periodVerificationImplementation, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		IPage<PeriodVerificationImplementationVO> periodVerificationImplementationSList = periodVerificationImplementationService.getPeriodVerificationImplementationVOPage(page, Wrappers.<PeriodVerificationImplementation>query()
				.eq("create_by", dlpUser.getId())
				.orderByDesc("create_time")
		);
		return R.ok(periodVerificationImplementationSList);
//		return R.ok(periodVerificationImplementationService.page(page, Wrappers.query(periodVerificationImplementation)));
    }


    /**
     * 新增(标准物质期间核查实施情况及结果记录表)
     * @param periodVerificationImplementationDTOS (标准物质期间核查实施情况及结果记录表)
     * @return R
     */
    @ApiOperation(value = "新增(标准物质期间核查实施情况及结果记录表)", notes = "新增(标准物质期间核查实施情况及结果记录表)")
    @SysLog("新增(标准物质期间核查实施情况及结果记录表)" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_add')" )
    public R<PeriodVerificationImplementation> postAddObject(@RequestBody PeriodVerificationImplementationDTO periodVerificationImplementationDTOS, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		PeriodVerificationImplementation periodVerificationImplementation = periodVerificationImplementationService.addById(periodVerificationImplementationDTOS, dlpUser);

		if (periodVerificationImplementation!=null) {
			return R.ok(periodVerificationImplementation, "保存成功");
		}
		else {
			return R.failed(periodVerificationImplementation, "对象创建失败");
		}
    }

    /**
     * 修改(标准物质期间核查实施情况及结果记录表)
     * @param periodVerificationImplementationDTO (标准物质期间核查实施情况及结果记录表)
     * @return R
     */
    @ApiOperation(value = "修改(标准物质期间核查实施情况及结果记录表)", notes = "修改(标准物质期间核查实施情况及结果记录表)")
    @SysLog("修改(标准物质期间核查实施情况及结果记录表)" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_edit')" )
    public R<PeriodVerificationImplementation> putUpdateById(@RequestBody PeriodVerificationImplementationDTO periodVerificationImplementationDTO, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		PeriodVerificationImplementation periodVerificationImplementation = periodVerificationImplementationService.editById(periodVerificationImplementationDTO);

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

//    /**
//     * 通过id删除(标准物质期间核查实施情况及结果记录表)
//     * @param periodVerificationImplementationId id
//     * @return R
//     */
//    @ApiOperation(value = "通过id删除(标准物质期间核查实施情况及结果记录表)", notes = "通过id删除(标准物质期间核查实施情况及结果记录表)")
//    @SysLog("通过id删除(标准物质期间核查实施情况及结果记录表)" )
//    @DeleteMapping("/{periodVerificationImplementationId}" )
//    @PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_del')" )
//    public R<PeriodVerificationImplementation> deleteById(@PathVariable String periodVerificationImplementationId, HttpServletRequest theHttpServletRequest) {
//		Principal principal = theHttpServletRequest.getUserPrincipal();
//		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
//
//        PeriodVerificationImplementation oldPeriodVerificationImplementation = periodVerificationImplementationService.getById(periodVerificationImplementationId);
//
//		if (periodVerificationImplementationService.removeById(periodVerificationImplementationId)) {
//			return R.ok(oldPeriodVerificationImplementation, "对象删除成功");
//		}
//		else {
//			return R.failed(oldPeriodVerificationImplementation, "对象删除失败");
//		}
//
//    }

	/**
	 * 提交(标准物质期间核查实施情况及结果记录表)
	 * @param periodVerificationImplementationDTO (标准物质期间核查实施情况及结果记录表)
	 * @return R
	 */
	@ApiOperation(value = "提交(标准物质期间核查实施情况及结果记录表)", notes = "提交(标准物质期间核查实施情况及结果记录表)")
	@SysLog("提交(标准物质期间核查实施情况及结果记录表)" )
	@PostMapping("/commit")
//	@PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_edit')" )
	public R<PeriodVerificationImplementation> commitById(@RequestBody PeriodVerificationImplementationDTO periodVerificationImplementationDTO, HttpServletRequest theHttpServletRequest) {

		Principal principal = theHttpServletRequest.getUserPrincipal();

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

		PeriodVerificationImplementation periodVerificationImplementation = periodVerificationImplementationService.commitById(periodVerificationImplementationDTO,dlpUser);

		if (periodVerificationImplementation!=null) {
			return R.ok(periodVerificationImplementation, "提交成功");
		}
		else {
			return R.failed("提交失败");
		}
	}

	/**
	 * 审核(标准物质期间核查实施情况及结果记录表)
	 * @param auditAndApproveDTO (标准物质期间核查实施情况及结果记录表)
	 * @return R
	 */
	@ApiOperation(value = "审核(标准物质期间核查实施情况及结果记录表)", notes = "审核(标准物质期间核查实施情况及结果记录表)")
	@SysLog("修改(标准物质期间核查实施情况及结果记录表)" )
	@PutMapping("/audit")
//	@PreAuthorize("@pms.hasPermission('reagent_period_verification_implementation_edit')" )
	public R<PeriodVerificationImplementation> auditById(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		PeriodVerificationImplementation periodVerificationImplementation = periodVerificationImplementationService.auditById(auditAndApproveDTO,dlpUser);

		if (periodVerificationImplementation!=null) {
			return R.ok(periodVerificationImplementation, "审核成功");
		}
		else {
			return R.failed("审核失败");
		}
	}


}
