package cn.com.qmth.examcloud.core.questions.api.controller;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.base.bean.ExportServiceManageBean;
import cn.com.qmth.examcloud.core.questions.base.dto.ExportTemplateDto;
import cn.com.qmth.examcloud.core.questions.dao.ExportServiceManageRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExportServiceManage;
import cn.com.qmth.examcloud.core.questions.service.ExportTemplateService;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "导出模板设定")
@RestController
@RequestMapping("${api_cqb}/exportTemplate")
public class ExportTemplateController extends ControllerSupport {
    @Autowired
    ExportServiceManageRepo exportServiceManageRepo;
    @Autowired
    private ExportTemplateService exportTemplateService;

    @ApiOperation(value = "分页查询")
    @GetMapping("page/{pageNo}/{pageSize}")
    public Page<ExportTemplateDto> getPage(@PathVariable Integer pageNo, @PathVariable Integer pageSize,
            @RequestParam Long rootOrgId, @RequestParam(required = false) String fileName,
            @RequestParam(required = false) String type) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100000", "无效的请求");
            }
        }
        return exportTemplateService.getPage(rootOrgId, fileName, type, pageNo, pageSize);
    }

    @ApiOperation(value = "新增")
    @PostMapping("add/{rootOrgId}")
    public void addResource(@RequestParam @NotNull(message = "模板名称不能为空！") String templateName,
            @PathVariable Long rootOrgId, @RequestParam @NotNull(message = "模板类型不能为空！") String type,
            @RequestPart @NotNull(message = "上传文件不能为空！") MultipartFile dataFile) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100001", "无效的请求");
            }
        }
        exportTemplateService.addFile(accessUser,rootOrgId, templateName, type, dataFile);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{rootOrgId}/{id}")
    public void delete(@PathVariable Long rootOrgId, @PathVariable Long id) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100002", "无效的请求");
            }
        }
        exportTemplateService.delete(rootOrgId, id);
    }

    @ApiOperation(value = "启用")
    @PutMapping("/enable/{rootOrgId}/{id}")
    public void enable(@PathVariable Long rootOrgId, @PathVariable Long id) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100003", "无效的请求");
            }
        }
        exportTemplateService.enable(rootOrgId, id);
    }

    @ApiOperation(value = "禁用")
    @PutMapping("/disenable/{rootOrgId}/{id}")
    public void disenable(@PathVariable Long rootOrgId, @PathVariable Long id) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100004", "无效的请求");
            }
        }
        exportTemplateService.disenable(rootOrgId, id);
    }
    
    @ApiOperation(value = "查询配置")
    @GetMapping("/config/{rootOrgId}")
    public ExportServiceManageBean getConfig(@PathVariable Long rootOrgId) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100005", "无效的请求");
            }
        }
        ExportServiceManage esm = exportServiceManageRepo.findByOrgId(String.valueOf(rootOrgId));
        ExportServiceManageBean bean=new ExportServiceManageBean();
        if(esm!=null) {
            bean.setOrgId(esm.getOrgId());
            bean.setId(esm.getId());
            bean.setExportServiceName(esm.getExportServiceName());
        }
        return bean;
    }

    @ApiOperation(value = "新增/修改配置")
    @PutMapping("/config/{rootOrgId}")
    public void modifyConfig(@RequestParam @NotNull(message = "配置名称不能为空！") String serviceName,
            @PathVariable Long rootOrgId) {
        User accessUser = getAccessUser();
        if (!isSuperAdmin()) {
            if (!rootOrgId.equals(accessUser.getRootOrgId())) {
                throw new StatusException("100006", "无效的请求");
            }
        }
        if(StringUtils.isBlank(serviceName)) {
            throw new StatusException("100007", "配置名称不能为空！");
        }
        ExportServiceManage esm = exportServiceManageRepo.findByOrgId(String.valueOf(rootOrgId));
        if(esm==null) {
            esm=new ExportServiceManage();
            esm.setOrgId(String.valueOf(rootOrgId));
        }
        esm.setExportServiceName(serviceName.trim());
        exportServiceManageRepo.save(esm);
    }
}
