package com.casic.missiles.controller.business;

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.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.outsource.OutsourceCertificateListRequest;
import com.casic.missiles.dto.business.outsource.OutsourceCertificateListResponse;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessOutsourceCertificate;
import com.casic.missiles.service.business.IBusinessOutsourceCertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 分包检定证书表 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-03-25
 */
@Api(tags = "分包管理接口-分包证书管理")
@Slf4j
@RestController
@RequestMapping("/business/outsourceCertificate")
public class BusinessOutsourceCertificateController extends ExportController {
    @Autowired
    private IBusinessOutsourceCertificateService certificateService;

    @ApiOperation("分包证书列表（分页）")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<OutsourceCertificateListResponse>> listPage(@RequestBody @Valid OutsourceCertificateListRequest request) {
        Page<OutsourceCertificateListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(certificateService.listPage(request, page)));
    }

    @ApiOperation("分包证书列表")
    @PostMapping("/list")
    public ReturnDTO<PageInfoBT<OutsourceCertificateListResponse>> list(@RequestBody @Valid OutsourceCertificateListRequest request) {
        return ReturnUtil.success(certificateService.list(request));
    }

    /**
     * 批量导入，zip包
     * 解析zip包，读取zip包中excel文件
     */
    @ApiOperation("分包证书批量导入")
    @PostMapping("/batchImport")
    public ReturnDTO batchImport(@RequestParam("file") MultipartFile file) throws IOException {
        if(Objects.isNull(file)){
            throw new BusinessException(BusinessExceptionEnum.UPLOAD_FILE_NULL);
        }
        return certificateService.batchImport(file);
    }

    /**
     * 模板下载，模板为zip包，调用系统设置-下载中心接口
     */

    @ApiOperation("分包证书导出")
    @PostMapping("/listExport")
    @ResponseBody
    public void export(@RequestBody @Valid OutsourceCertificateListRequest request) throws IOException {
        List<OutsourceCertificateListResponse> list;
        if (!CollectionUtils.isEmpty(request.getIds())) {
            list = certificateService.listByCertificateIds(request.getIds());
        } else {
            list = certificateService.list(request);
        }
        super.exportExcel(OutsourceCertificateListResponse.class, list, ExportEnum.OUTSOURCER_CERTIFICATE_EXPORT.getSheetName());
    }

    @ApiOperation("分包证书新增")
    @PostMapping("/add")
    @ResponseBody
    public ReturnDTO add(@RequestBody BusinessOutsourceCertificate certificate) {
        return certificateService.addCertificate(certificate);
    }

    @ApiOperation("分包证书编辑")
    @PostMapping("/update")
    @ResponseBody
    public ReturnDTO update(@RequestBody BusinessOutsourceCertificate certificate) {
        return certificateService.updateCertificate(certificate);
    }

}
