package com.jwipc.tender.controller.pc;

import com.jwipc.exception.DaoException;
import com.jwipc.page.PageView;
import com.jwipc.tender.constant.ModuleConst;
import com.jwipc.tender.dto.TenderTemplateSaveDTO;
import com.jwipc.tender.logic.TenderTemplateLogic;
import com.jwipc.tender.model.query.TenderTemplateQuery;
import com.jwipc.util.EasyExcelImportUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p> 招标模板相关接口</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping(ModuleConst.MODULE_ROOT + "/template/rest")
public class TenderTemplateRestController {
    private final TenderTemplateLogic tenderTemplateLogic;

    /**
     * 招标模板 列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping(value = "list")
    public PageView list(TenderTemplateQuery query) {
        return tenderTemplateLogic.getList(query);
    }

    /**
     * 招标模板全部数据
     * @param typeId 招标类型
     * @return
     */
    @GetMapping(value = "listAll")
    public ResponseEntity listAll(@RequestParam(required = false, defaultValue = "0")Long typeId) {
        return ResponseEntity.ok(tenderTemplateLogic.getListAll(typeId));
    }

    /**
     * 招标模板 模板字段选择器的数据接口
     *
     * @return
     */
    @GetMapping(value = "fieldList")
    public PageView fieldList() {
        return tenderTemplateLogic.getFieldList();
    }

    /**
     * 招标模板 字段数据
     *
     * @param id 模板主键
     * @return
     */
    @GetMapping("templateFieldList")
    public ResponseEntity templateFieldList(Long id) {
        return ResponseEntity.ok(tenderTemplateLogic.getTemplateFieldList(id));
    }

    /**
     * 招标模板 新增 、修改和复制
     *
     * @param saveDTO 模板数据
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody TenderTemplateSaveDTO saveDTO) {
        tenderTemplateLogic.saveOrUpdate(saveDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 招标模板 删除
     *
     * @param id 模板主键
     * @return
     */
    @PostMapping("remove")
    public ResponseEntity remove(Long id) {
        tenderTemplateLogic.remove(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 下载招标模板数据
     *
     * @param templateId 模板ID
     * @return 供应商报价列表
     */
    @RequestMapping("/export")
    public void export(HttpServletResponse response, Long templateId,
                            @RequestParam(defaultValue = "0") Integer thresholdFlag) throws IOException
    {
        tenderTemplateLogic.tempExport(response, templateId,thresholdFlag);
    }

    /**
     * 招标行项目导入
     * @param file
     * @return
     */
    @RequestMapping("/import")
    public ResponseEntity<List<Map<String, String>>> importMa(@RequestPart("file") MultipartFile file) {
        try {
            List<Map<String, String>> dataList = EasyExcelImportUtils.parseExcelToData(file.getInputStream(), 2);
            return ResponseEntity.ok(tenderTemplateLogic.handleImportData(dataList));
        } catch (IOException e) {
            throw new DaoException("读取Excel失败");
        }
    }

    /**
     * 下载投标报价数据
     *
     * @param bidId 投标单id
     * @return 供应商报价列表
     */
    @RequestMapping("/export1")
    public void export1(HttpServletResponse response, Long bidId,Long historyFlag,
                            @RequestParam(defaultValue = "0") Integer thresholdFlag,
                        @RequestParam(defaultValue = "0") Integer bargaining) throws IOException
    {
        tenderTemplateLogic.export1(response, bidId,historyFlag,thresholdFlag,bargaining);
    }

    /**
     * 导入报价数据
     *
     * @return 供应商报价列表
     */
    @RequestMapping("/import1")
    public ResponseEntity<List<Map<String, String>>> import1(@RequestPart("file") MultipartFile file) {
        try {
            List<Map<String, String>> dataList = EasyExcelImportUtils.parseExcelToData(file.getInputStream(), 2);
            return ResponseEntity.ok(tenderTemplateLogic.handleImport1Data(dataList));
        } catch (IOException e) {
            throw new DaoException("读取Excel失败");
        }
    }
}
