package com.jwipc.tender.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.page.PageView;
import com.jwipc.tender.dto.BidQuoteLogDTO;
import com.jwipc.tender.logic.BidQuoteLogLogic;
import com.jwipc.tender.model.query.BidQuoteLogQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import static com.jwipc.tender.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(MODULE_ROOT + "/bidQuote/rest/")
@RequiredArgsConstructor
public class BidQuoteLogRestController {
    private final BidQuoteLogLogic bidQuoteLogLogic;


    @GetMapping("list")
    public PageView list(BidQuoteLogQuery query) {
        return bidQuoteLogLogic.getReport(query);
    }

    @PostMapping("export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr)  throws IOException {
        final BidQuoteLogQuery tenderQuery = JSON.parseObject(queryStr, BidQuoteLogQuery.class);
        List<BidQuoteLogDTO> scoreExportDTOS = bidQuoteLogLogic.getAll(tenderQuery);
        //先解析前端传过来的字段信息
        List<ExportDataDTO> exportDataDTOS = JSON.parseArray(exportFiled, ExportDataDTO.class);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("user_data.xlsx", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        EasyExcelUtils.trendsModelWrite(response,
                "报价日志-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet",
                scoreExportDTOS, exportDataDTOS);
    }
}
