package com.jwipc.inquiry.controller.rest;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.exception.DaoException;
import com.jwipc.inquiry.dto.*;
import com.jwipc.inquiry.entity.Inquiry;
import com.jwipc.inquiry.logic.InquiryLogic;
import com.jwipc.inquiry.logic.InquiryMaterialSupplierLogic;
import com.jwipc.inquiry.logic.InquirySupplierLogic;
import com.jwipc.inquiry.logic.InquiryTemplateLogic;
import com.jwipc.inquiry.model.query.InquiryQuery;
import com.jwipc.inquiry.model.query.InquiryQuoteQuery;
import com.jwipc.inquiry.model.query.InquirySupplierQuery;
import com.jwipc.inquiry.model.query.PurchaseApplyBillDetailQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierAvlBaseDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.dto.DictDTO;
import com.jwipc.util.EasyExcelImportUtils;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.IndexedColors;
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.math.BigDecimal;
import java.util.*;

import static com.jwipc.inquiry.constant.ModuleConst.MODULE_ROOT;
import static org.apache.calcite.linq4j.tree.ExpressionType.New;

/**
 * @author shenyang.ou
 * @date 2024/2/4 19:48
 * @desc 供应商报价页面
 */
@RestController
@RequestMapping(MODULE_ROOT+ "/supplier/quote/rest")
@RequiredArgsConstructor
public class InquirySupplierQuoteRestController {
    private final InquiryLogic inquiryLogic;
    private final InquirySupplierLogic inquirySupplierLogic;
    private final InquiryMaterialSupplierLogic inquiryMaterialSupplierLogic;
    private final InquiryTemplateLogic inquiryTemplateLogic;
    private final SupplierClient supplierClient;
    private final DictClient dictClient;

    /**
     * 列表
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(InquirySupplierQuery query) {
        query.setSupplierId(Objects.requireNonNull(LoginUtil.getCurrentUser()).supplierId);
        return inquirySupplierLogic.findByPage(query);
    }

    /**
     * 获取供应商报价列表
     * @param inquiryId 询价id
     * @return 供应商报价列表
     */
    @RequestMapping("/quote/list")
    public ResponseEntity<List<InquiryMaterialSupplierDTO>> quoteList(Long inquiryId) {
        InquiryQuoteQuery query = new InquiryQuoteQuery();
        query.setInquiryId(inquiryId);
        query.setSupplierId(Objects.requireNonNull(LoginUtil.getCurrentUser()).supplierId);
        return ResponseEntity.ok(inquiryMaterialSupplierLogic.quoteList(query));
    }

    /**
     * 动态导出供应商报价列表
     * @param inquiryId 询价id
     * @return 供应商报价列表
     */
    @RequestMapping("/quote/export")
    public void quoteExport(HttpServletResponse response,Long inquiryId) throws IOException{
        inquiryMaterialSupplierLogic.quoteExport(response,inquiryId);
    }

    @RequestMapping("/quote/import")
    public ResponseEntity<List<Map<String,String>>> quoteImport(@RequestPart("file") MultipartFile file,Long inquiryId){
        List<Map<String,String>> dataList = inquiryMaterialSupplierLogic.quoteImport(file,inquiryId);
        return ResponseEntity.ok(dataList);

    }

    /**
     * 获取供应商报价列表(带foot)
     * @param inquiryId 询价id
     * @param productGroupWithCategoryFlag 是否查询物料组品类关系
     * @return 供应商报价列表
     */
    @RequestMapping("/quote/foot/list")
    public ResponseEntity<Map<String, Object>> quoteFootList(Long inquiryId,Integer productGroupWithCategoryFlag) {
        InquiryQuoteQuery query = new InquiryQuoteQuery();
        query.setInquiryId(inquiryId);
        query.setProductGroupWithCategoryFlag(productGroupWithCategoryFlag);
        query.setSupplierId(Objects.requireNonNull(LoginUtil.getCurrentUser()).supplierId);
        List<InquiryMaterialSupplierDTO> inquiryMaterialSupplierDTOS = inquiryMaterialSupplierLogic.quoteList(query);
        inquiryMaterialSupplierDTOS.forEach(inquiryMaterialSupplierDTO -> {
            //赋值Avl信息
            if (StringUtils.isEmpty(inquiryMaterialSupplierDTO.getMpn())&&StringUtils.isNotEmpty(inquiryMaterialSupplierDTO.getMaterialCode())&&StringUtils.isEmpty(inquiryMaterialSupplierDTO.getCountryName())){
                SupplierAvlBaseDTO supplierAvlBaseDTO = supplierClient.findSupplierAvlItem(inquiryMaterialSupplierDTO.getSupplierCode(),inquiryMaterialSupplierDTO.getMaterialCode());
                if (ObjectUtil.isNotNull(supplierAvlBaseDTO)){
                    inquiryMaterialSupplierDTO.setMpn(supplierAvlBaseDTO.getMpn());
                    inquiryMaterialSupplierDTO.setManufacturer(supplierAvlBaseDTO.getProductName());
                    inquiryMaterialSupplierDTO.setManufacturerEn(supplierAvlBaseDTO.getProductNameEn());
                    inquiryMaterialSupplierDTO.setCountryName(supplierAvlBaseDTO.getCountryCode());
                    inquiryMaterialSupplierDTO.setMsl(supplierAvlBaseDTO.getMsl());
                    inquiryMaterialSupplierDTO.setEccn(supplierAvlBaseDTO.getEccn());
                    inquiryMaterialSupplierDTO.setMpq(StrUtil.isEmpty(supplierAvlBaseDTO.getMpq())?null: (long) Double.parseDouble(supplierAvlBaseDTO.getMpq()));
                    inquiryMaterialSupplierDTO.setMoq(StrUtil.isEmpty(supplierAvlBaseDTO.getMoq())?null:(long) Double.parseDouble(supplierAvlBaseDTO.getMoq()));
                    inquiryMaterialSupplierDTO.setConfirmDeliveryDate(StrUtil.isEmpty(supplierAvlBaseDTO.getCif())?null: Integer.valueOf(supplierAvlBaseDTO.getCif()));
                }
            }
        });
        Map<String, Object> data = MapUtil.newHashMap();
        data.put("total",inquiryMaterialSupplierDTOS.size());
        data.put("rows",inquiryMaterialSupplierDTOS);
        JSONArray footer = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("materialCode", "<b>合计</b>");
        obj.put("taxTotalPrice", BigDecimal.ZERO);
        obj.put("noTaxTotalPrice", BigDecimal.ZERO);
        obj.put("isFooter", true);
        footer.add(obj);
        data.put("footer",footer);
        return ResponseEntity.ok(data);
    }


    /**
     * 报价保存
     *
     * @param param 参数
     */
    @PostMapping("/saveQuote")
    public ResponseEntity saveQuote(@RequestBody InquirySupplierQuoteParamDTO param) {
        inquiryMaterialSupplierLogic.saveQuote(param);
        return ResponseEntity.ok(true);
    }

}
