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.PeriodVerificationPlanDTO;
import digital.laboratory.platform.reagent.entity.CheckSchedule;
import digital.laboratory.platform.reagent.entity.PeriodVerificationPlan;
import digital.laboratory.platform.reagent.service.CheckScheduleService;
import digital.laboratory.platform.reagent.vo.CheckScheduleVO;
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-04-10
 * @describe 前端控制器
 * <p>
 * 这是与表示层的接口, 不应该接业务逻辑写在这里, 业务逻辑应该写在 service 中
 * 这里写什么:
 * 为前端提供数据, 接受前端的数据
 * 为前端提供的数据, 从 service 取得后, 可以做一些适当的加工, 这种加工不是业务层面的, 只能是数据格式上, 为方便前端处理
 * 接受前端的数据, 每一个函数的参数可以先做一些整理后, 再调用 service 中的函数。这里对参数的整理, 应该只是格式上的, 而不能是业务上的
 * 数据层在 mapper 中, 数据层不涉及业务, 只管技术上的 对象<->表 之间的转换
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/check_schedule")
@Api(value = "check_schedule", tags = "管理")
public class CheckScheduleController {

    private final CheckScheduleService checkScheduleService;

    /**
     * 通过id查询
     *
     * @param checkScheduleId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{checkScheduleId}")
//	@PreAuthorize("@pms.hasPermission('reagent_check_schedule_get')" )
    public R<CheckSchedule> getById(@PathVariable("checkScheduleId") String checkScheduleId, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        CheckSchedule checkSchedule = checkScheduleService.getCheckScheduleVO(checkScheduleId);
        return R.ok(checkSchedule);
        //return R.ok(checkScheduleService.getById(managerId));
    }

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param checkSchedule
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('reagent_check_schedule_get')" )
    public R<IPage<CheckScheduleVO>> getCheckSchedulePage(Page<CheckSchedule> page, CheckSchedule checkSchedule, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        IPage<CheckScheduleVO> checkScheduleSList = checkScheduleService.getCheckScheduleVOPage(page, Wrappers.<CheckSchedule>query()
                .eq("create_by", dlpUser.getId())
                .orderByDesc("create_time")
        );
        return R.ok(checkScheduleSList);
//		return R.ok(checkScheduleService.page(page, Wrappers.query(checkSchedule)));
    }


    /**
     * 新增
     *
     * @param periodVerificationPlanDTOS
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('reagent_check_schedule_add')" )
    public R<CheckSchedule> postAddObject(@RequestBody List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();

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

        CheckSchedule checkSchedule = checkScheduleService.addPlan(periodVerificationPlanDTOS, dlpUser);

        if (checkSchedule != null) {
            return R.ok(checkSchedule, "计划创建成功");
        } else {
            return R.failed("计划创建失败");
        }
    }

    /**
     * 修改
     *
     * @param periodVerificationPlanDTOS
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('reagent_check_schedule_edit')" )
    public R<CheckSchedule> putUpdateById(@RequestBody List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        CheckSchedule checkSchedule = checkScheduleService.editPlan(periodVerificationPlanDTOS);

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

    /**
     * 提交计划
     *
     * @param periodVerificationPlanDTOS
     * @return R
     */
    @ApiOperation(value = "提交计划", notes = "提交计划")
    @SysLog("提交计划")
    @PostMapping("/commit")
//    @PreAuthorize("@pms.hasPermission('reagent_check_schedule_edit')" )
    public R<CheckSchedule> commitById(@RequestBody List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        CheckSchedule checkSchedule = checkScheduleService.commitPlan(periodVerificationPlanDTOS,dlpUser);

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


//    /**
//     * 通过id删除
//     *
//     * @param periodVerificationPlanDTOS id
//     * @return R
//     */
//    @ApiOperation(value = "通过id删除", notes = "通过id删除")
//    @SysLog("通过id删除")
//    @PostMapping("/commit")
////    @PreAuthorize("@pms.hasPermission('reagent_check_schedule_del')" )
//    public R<CheckSchedule> deleteById(@RequestBody List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS, HttpServletRequest theHttpServletRequest) {
//        Principal principal = theHttpServletRequest.getUserPrincipal();
//        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
//
//        CheckSchedule checkSchedule = checkScheduleService.commitPlan(periodVerificationPlanDTOS, dlpUser);
//
//        if (checkSchedule != null) {
//            return R.ok(checkSchedule, "提交成功");
//        } else {
//            return R.failed("提交失败");
//        }
//
//    }

    /**
     * 审核
     *
     * @param auditAndApproveDTO
     * @return R
     */
    @ApiOperation(value = "审核", notes = "审核")
    @SysLog("修改")
    @PutMapping("/audit")
//    @PreAuthorize("@pms.hasPermission('reagent_check_schedule_edit')" )
    public R<CheckSchedule> auditPlan(@RequestBody AuditAndApproveDTO auditAndApproveDTO, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        CheckSchedule checkSchedule = checkScheduleService.auditPlan(auditAndApproveDTO, dlpUser);

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


}
