package digital.laboratory.platform.inspection.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.inspection.dto.TemplateDeleteDto;
import digital.laboratory.platform.inspection.entity.TestTemplate;
import digital.laboratory.platform.inspection.service.TestTemplateService;
import digital.laboratory.platform.inspection.vo.TestTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/test_template")
@Api(tags = "13-实验模板", description = "实验模板")
public class TestTemplateController {
    @Resource
    private TestTemplateService testTemplateService;

    @PostMapping("/createTemplate/{testId}")
    @ApiOperation(value = "根据实验创建模板")
    public R createTemplate(@RequestBody TestTemplate testTemplate, HttpServletRequest httpServletRequest, @PathVariable String testId) throws Exception {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        TestTemplate template = testTemplateService.createTemplate(testTemplate, testId, dlpUser);
        return template != null ? R.ok(template, "创建成功!") : R.failed("创建失败!");
    }

    @PutMapping("/editTemplate")
    @ApiOperation(value = "修改模版信息")
    public R editTemplate(TestTemplate testTemplate) {
        return testTemplateService.editTemplate(testTemplate) != null ? R.ok(testTemplate, "修改成功!") : R.failed("修改失败!");
    }

    @PutMapping("/publishTemplate")
    @ApiOperation(value = "通过ID发布模板")
    public R publishTemplate(String id) {
        TestTemplateVo template = testTemplateService.publishTemplate(id);
        return template != null ? R.ok(template, "发布成功!") : R.failed("发布失败!");
    }

    @PutMapping("/blockTemplate")
    @ApiOperation(value = "通过ID停用模板")
    public R blockTemplate(String id) {
        TestTemplateVo template = testTemplateService.blockTemplate(id);
        return template != null ? R.ok(template, "停用成功!") : R.failed("停用失败!");
    }

    @GetMapping("/getTemplateById")
    @ApiOperation(value = "通过ID查询模板信息")
    public R getTemplateById(String id) {
        return R.ok(testTemplateService.getTemplateById(id), "数据获取成功!");
    }

    @GetMapping("/getTemplatePage")
    @ApiOperation(value = "分页查询模板信息")
    public R getTemplateVoPage(Page page, Integer opCode, HttpServletRequest httpServletRequest, String keywords) {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        return R.ok(testTemplateService.getTemplateVoPage(page, opCode, dlpUser, keywords), "数据获取成功!");
    }

    @GetMapping("/getTemplateList")
    @ApiOperation(value = "列表查询模板信息(创建实验时)")
    public R getTemplateVoList(HttpServletRequest httpServletRequest) {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        return R.ok(testTemplateService.getTemplateVoList(dlpUser), "数据获取成功!");
    }

    @DeleteMapping()
    @ApiOperation(value = "根据ID删除模板")
    public R delTemplate(@RequestBody List<String>idList, HttpServletRequest httpServletRequest) {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        return testTemplateService.delTemplate(idList, dlpUser) ? R.ok("删除成功!") : R.failed("删除失败!");
    }


}
