package com.jwipc.inquiry.controller.rest;

import cn.hutool.core.collection.ListUtil;
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.inquiry.constant.ModuleConst;
import com.jwipc.inquiry.dto.*;
import com.jwipc.inquiry.logic.InquiryLogic;
import com.jwipc.inquiry.logic.PriceWarningLogic;
import com.jwipc.inquiry.model.query.InquiryQuery;
import com.jwipc.inquiry.model.query.PriceWarningQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

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

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2024/2/18 15:35
 * @desc 询价
 */
@RestController
@RequestMapping(MODULE_ROOT + "/create/warning/rest")
@RequiredArgsConstructor
public class PriceWarningRestController {
    private final PriceWarningLogic priceWarningLogic;
    private final InquiryLogic inquiryLogic;
    /**
     * 列表
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(PriceWarningQuery query) {
        query.setUserId(LoginUtil.getUserId());
        return priceWarningLogic.findByPage(query);
    }

    /**
     * 询价编辑供应商列表
     * @param inquiryId 询价id
     * @return
     */
    @RequestMapping("/supplierListByInquiryId")
    public ResponseEntity<List<InquiryEditSupplierDTO>> supplierListByInquiryId(Long inquiryId) {
        return ResponseEntity.ok(inquiryLogic.supplierListByInquiryId(inquiryId));
    }

    /**
     * 稽核列表导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        //先解析前端传过来的字段信息
        List<ExportDataDTO> exportDataDTOS = JSON.parseArray(exportFiled, ExportDataDTO.class);
        exportDataDTOS.forEach(exportDataDTO -> {
            //如果字段为采购类型，则生成下拉框
            if ("purchaseType".equals(exportDataDTO.getField())){
                //插入下拉框数据，也可以从字典里取数据，只插入字典名称
                exportDataDTO.setSelectList(ListUtil.toList("标准","寄售","委外"));
            }
        });
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        PriceWarningQuery query =  JSON.parseObject(queryStr, PriceWarningQuery.class);
        query.setUserId(LoginUtil.getUserId());
        final List<PriceWarningDTO> exportData = priceWarningLogic.listAll(query);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"价格预警导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportDataDTOS);
    }

    /**
     * 导入物料信息
     * @param file 导入文件
     * @return
     */
    @PostMapping("importMaterialData")
    public ResponseEntity<List<InquiryMaterialImportDTO>> importMaterialData(@RequestParam(value = "file") MultipartFile file) {
        List<InquiryMaterialImportDTO> inquiryMaterialImportDTOS = inquiryLogic.importMaterialData(file);
        return ResponseEntity.ok(inquiryMaterialImportDTOS);
    }

    /**
     * 导入供应商信息
     * @param file 导入文件
     * @return
     */
    @PostMapping("importSupplierData")
    public ResponseEntity<List<InquirySupplierImportDTO>> importSupplierData(@RequestParam(value = "file") MultipartFile file,String companyCode) {
        List<InquirySupplierImportDTO> inquirySupplierImportDTOS = inquiryLogic.importSupplierData(file,companyCode);
        return ResponseEntity.ok(inquirySupplierImportDTOS);
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody InquiryParamDTO param) {
        inquiryLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 发布
     *
     * @param id 询价id
     */
    @PostMapping("/publish")
    public ResponseEntity publish(Long id) {
        inquiryLogic.publish(id);
        return ResponseEntity.ok(true);
    }


    /**
     * 截止
     *
     * @param id 询价id
     */
    @PostMapping("/end")
    public ResponseEntity end(Long id) {
        inquiryLogic.end(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 作废
     *
     * @param id 询价id
     */
    @PostMapping("/cancel")
    public ResponseEntity cancel(Long id,String cancelRemark) {
        inquiryLogic.cancel(id,cancelRemark);
        return ResponseEntity.ok(true);
    }


    /**
     * 删除
     *
     * @param id 询价id
     */
    @PostMapping("/remove")
    public ResponseEntity remove(Long id) {
        inquiryLogic.remove(id);
        return ResponseEntity.ok(true);
    }


    /**
     * 获取物料报价情况统计
     * @param inquiryId 询价id
     * @return 物料报价情况统计
     */
    @RequestMapping("/material/quote/list")
    public ResponseEntity<List<InquiryMaterialQuoteDTO>> materialQuoteList(Long inquiryId) {
        return ResponseEntity.ok(inquiryLogic.materialQuoteList(inquiryId));
    }
}
