package com.casic.missiles.service.listeners.register.data;

import com.casic.missiles.model.business.BusinessOrder;
import com.casic.missiles.model.customer.CustomerInfo;
import com.casic.missiles.model.customer.CustomerSampleInfo;
import com.casic.missiles.model.equipment.EquipmentFixedAssets;
import com.casic.missiles.service.business.IBusinessOrderService;
import com.casic.missiles.service.business.IBusinessSiteExecutiveInfoEquipmentRelationService;
import com.casic.missiles.service.customer.CustomerSampleService;
import com.casic.missiles.service.customer.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 参数填充创建类
 */
@Component
@Slf4j
public class FileParamDataProvider {
    @Autowired
    private IBusinessOrderService businessOrderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerSampleService customerSampleService;
    @Autowired
    private IBusinessSiteExecutiveInfoEquipmentRelationService equipmentRelationInfoService;
    private BusinessOrder businessOrder;
    private Map<String, Object> fileParamData;

    FileParamDataProvider builder() {
        fileParamData = new HashMap<>();
        return this;
    }

    /**
     * 基本年月日信息
     */
    FileParamDataProvider loadTime() {
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        fileParamData.put("y", now.get(Calendar.YEAR));
        fileParamData.put("m", now.get(Calendar.MONTH) + 1);
        fileParamData.put("d", now.get(Calendar.DAY_OF_MONTH));
        return this;
    }

    /**
     * 委托书信息
     */
    FileParamDataProvider businessOrder(Long orderId) {
        businessOrder = businessOrderService.orderDetail(orderId);
        fileParamData.put("customerName", businessOrder.getCustomerName());
        fileParamData.put("customerAddress", businessOrder.getCustomerAddress());
        fileParamData.put("customerName", businessOrder.getCustomerName());
        fileParamData.put("customerAddress", businessOrder.getCustomerAddress());
        return this;
    }

    /**
     * 样品信息
     */
    FileParamDataProvider customerSample(Long sampleId) {
        CustomerSampleInfo customerSampleInfo = customerSampleService.sampleDetail(sampleId);
        fileParamData.put("customerSampleName", customerSampleInfo.getSampleName());
        fileParamData.put("sampleModel", customerSampleInfo.getSampleModel());
        fileParamData.put("manufacturingNo", customerSampleInfo.getManufacturingNo());
        fileParamData.put("sampleNo", customerSampleInfo.getSampleNo());
        fileParamData.put("sampleName", customerSampleInfo.getSampleName());
        fileParamData.put("customerSampleNo", customerSampleInfo.getSampleNo());
        fileParamData.put("customerAddress", customerSampleInfo.getCustomerAddress().replace("null",""));
        fileParamData.put("deliverer", customerSampleInfo.getDeliverer());
        fileParamData.put("delivererTel", customerSampleInfo.getDelivererTel());
        return this;
    }

    /**
     * 客户信息
     */
    FileParamDataProvider customerInfo() {
        CustomerInfo customerInfo = customerService.customerDetail(businessOrder.getCustomerId());
        fileParamData.put("director", customerInfo.getCustomerName());
        fileParamData.put("customerPhone", customerInfo.getPhone());
        fileParamData.put("customerName", customerInfo.getCustomerName());
        fileParamData.put("customerNo", customerInfo.getCustomerNo());
        return this;
    }
    /**
     * 外场检定查询测量设备
     */
    FileParamDataProvider equipmentFixedAssets(Long siteExecutiveLogId) {
        try {
            List<EquipmentFixedAssets> equipmentFixedAssetList = equipmentRelationInfoService.getEquipmentsByExecutiveInfoId(siteExecutiveLogId);
            List<Map<String, Object>> assetList = new ArrayList<>();
            equipmentFixedAssetList.forEach(
                    equipmentFixedAsset -> {
                        Map<String, Object> equipmentFixedAssetsMap = new HashMap<>();
                        equipmentFixedAssetsMap.put("assetName", equipmentFixedAsset.getEquipmentName());
                        equipmentFixedAssetsMap.put("assetNo", equipmentFixedAsset.getEquipmentNo());
                        equipmentFixedAssetsMap.put("assetModel", equipmentFixedAsset.getModelNo());
                        equipmentFixedAssetsMap.put("validDate", equipmentFixedAsset.getValidDate());
                        assetList.add(equipmentFixedAssetsMap);
                    }
            );
            fileParamData.put("assetList", assetList);
        } catch (Exception ex) {
            log.error("query EquipmentFixedAssets table is fail,the id is {},the message  is {}", siteExecutiveLogId, ex);
        }
        return this;
    }

    Map<String, Object> build() {
        return this.fileParamData;
    }

}
