package com.casic.missiles.controller.system.download;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.system.DownloadFileRequest;
import com.casic.missiles.dto.system.DownloadListRequest;
import com.casic.missiles.dto.system.DownloadListResponse;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.system.SystemFileConfig;
import com.casic.missiles.service.system.IDownloadCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cz
 * @since 2023-04-03
 */
@Api(tags = "常用工具-文件配置管理接口")
@RestController
@RequestMapping("/download/center")
@AllArgsConstructor
public class SystemFileConfigController  extends ExportController {

    private final IDownloadCenterService  downloadCenterService;

    @ApiOperation("下载文件分页")
    @PostMapping("/listPage")
    @ResponseBody
    public ReturnDTO<PageInfoBT<DownloadListResponse>> downloadFileListPage(@RequestBody @Valid DownloadListRequest request, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<SystemFileConfig> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(downloadCenterService.listPage(page, request)));
    }

    @ApiOperation("更新下载文件")
    @ResponseBody
    @PostMapping("/update")
    public ReturnDTO updateDownloadFile(@RequestBody @Valid SystemFileConfig downloadCenter, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return downloadCenterService.updateDownloadFile(downloadCenter);
    }

    @ApiOperation("新增下载文件")
    @ResponseBody
    @PostMapping("/add")
    public ReturnDTO addDownloadFile(@RequestBody @Valid SystemFileConfig downloadCenter, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return downloadCenterService.addDownloadFile(downloadCenter);
    }

    @ApiOperation("下载文件详情")
    @ResponseBody
    @PostMapping("/detail")
    public ReturnDTO<SystemFileConfig> downloadFileDetail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(downloadCenterService.downloadFileDetail(idDTO.getId()));
    }

    @ApiOperation("删除下载文件")
    @ResponseBody
    @PostMapping("/delete")
    public ReturnDTO deleteDownloadFile(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return downloadCenterService.deleteDownloadFile(idDTO.getId());
    }

    @ApiOperation("模板下载")
    @ResponseBody
    @PostMapping("/downloadFile")
    public ReturnDTO<ResponseEntity> downloadFile(@RequestBody @Valid DownloadFileRequest downloadFileRequest, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return downloadCenterService.downloadFile(downloadFileRequest);

    }


    @ApiOperation("文件导出")
    @PostMapping("/listExport")
    @ResponseBody
    public void export(@RequestBody @Valid DownloadListRequest request, BindingResult bindingResult, HttpServletResponse response) throws Exception {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        downloadCenterService.downloadFileExport(request,response);
    }
}
