package com.dsk.biz.controller;

import com.dsk.biz.domain.BusinessOpenTender;
import com.dsk.biz.domain.bo.BusinessOpenTenderDto;
import com.dsk.biz.service.IBusinessOpenTenderService;
import com.dsk.biz.utils.ExcelUtils;
import com.dsk.common.annotation.Log;
import com.dsk.common.annotation.RepeatSubmit;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.enums.BusinessType;
import com.dsk.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目开标记录
 *
 * @author lcl
 * @date 2023-10-23
 */
@RestController
@RequestMapping("/business/open/tender")
public class BusinessOpenTenderController extends BaseController {

    @Autowired
    private IBusinessOpenTenderService baseService;

    /**
     * 开标记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<BusinessOpenTender> list(BusinessOpenTenderDto dto, PageQuery pageQuery) {
        return baseService.selectList(dto, pageQuery);
    }

    /**
     * 添加开标记录
     */
    @PostMapping
    @RepeatSubmit
    public R<Void> add(@RequestBody BusinessOpenTender bo) {
        return toAjax(baseService.add(bo));
    }

    /**
     * 修改开标记录
     */
    @PutMapping
    @RepeatSubmit
    public R<Void> edit(@RequestBody BusinessOpenTender bo) {
        return toAjax(baseService.edit(bo));
    }

    /**
     * 删除开标记录
     */
    @DeleteMapping("/{ids}")
    @RepeatSubmit
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(baseService.remove(ids));
    }

    /**
     * 批量导入
     */
    @Log(title = "开标记录批量导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData/{businessId}")
    public R<Map<String, Object>> importData(@RequestPart("file") MultipartFile file, @PathVariable Integer businessId) throws Exception {
        List<BusinessOpenTender> list = new ExcelUtils<>(BusinessOpenTender.class).importExcel(file.getInputStream(), 1);
        if (list.isEmpty()) throw new BusinessException("请填写导入数据！");
        int sucessCount = 0;
        List<String> errorList = new ArrayList<>();
        for (BusinessOpenTender openTender : list) {
            if (ObjectUtils.isEmpty(openTender.getTenderer())) {
                errorList.add("投标人不能为空！");
                continue;
            }
            openTender.setBusinessId(businessId);
            try {
                int add = baseService.add(openTender);
                if (add > 0) {
                    sucessCount++;
                }
            } catch (Exception e) {
                errorList.add(openTender.getTenderer().concat(":").concat(e.getMessage()));
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("sucessCount", sucessCount);
        result.put("errorCount", list.size() - sucessCount);
        result.put("errorMsg", errorList);
        return R.ok(result);
    }

}
