package com.uas.platform.b2b.service.impl;

import com.uas.platform.b2b.core.util.ContextUtils;
import com.uas.platform.b2b.dao.NotExistProductDao;
import com.uas.platform.b2b.erp.exception.NotFoundUtils;
import com.uas.platform.b2b.model.NotExistProduct;
import com.uas.platform.b2b.service.NotExistProductService;
import com.uas.platform.b2b.support.HttpUtils;
import com.uas.platform.b2b.support.SysConf;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Status;
import com.uas.ps.core.util.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询平台不存在的ERP的物料信息
 *
 * Created by hejq on 2018-03-14.
 */
@Service
public class NotExistProductServiceImpl implements NotExistProductService {

    @Autowired
    private NotExistProductDao notExistProductDao;

    /**
     * 通过企业UU和下载状态查询未上传的物料
     *
     * @param enuu   企业UU
     * @param status 下载状态
     * @return
     */
    @Override
    public List<NotExistProduct> findByEnuuAndStatus(Long enuu, Integer status) {
        return notExistProductDao.findByEnuuAndStatus(enuu, status);
    }

    /**
     * 更新下载状态
     *
     * @param idArray id信息
     */
    @Override
    public void updateDownloadStatus(String[] idArray) {
        if (idArray.length > 0) {
            for (String id : idArray) {
                notExistProductDao.updateStatus(Long.valueOf(id));
            }
        }
    }

    /**
     * 通过物料编号，单据来源，单据编号保存不存在物料信息
     *
     * @param prodCode   物料编号
     * @param source     单据来源
     * @param sourceCode 单据编号
     * @param detno 明细序号
     */
    @Override
    public void save(String prodCode, String source, String sourceCode, Short detno) {
        Long enUU = SystemSession.getUser().getEnterprise().getUu();
        List<NotExistProduct> productList = notExistProductDao.findByEnuuAndCode(enUU, prodCode);
        if (CollectionUtils.isEmpty(productList)) {
            notExistProductDao.save(new NotExistProduct(prodCode, enUU, source, sourceCode, detno, ""));
        } else {
            NotExistProduct prod = productList.get(0);
            prod.setStatus(Status.NOT_UPLOAD.value());
            notExistProductDao.save(prod);
        }
    }

    /**
     * 更新物料网站
     */
    private final String PRODUCT_SERVICE_URL = ContextUtils.getBean(SysConf.class).getProductServiceUrl();

    /**
     * 批量存储物料未找到信息
     *
     * @param productList 物料信息
     * @param paramString 异常信息
     * @param sign 标志，方便查询对应的异常
     */
    @Override
    public void batchSave(List<NotExistProduct> productList, String paramString, String sign) {
        notExistProductDao.save(productList);
        List<Map<String, Object>> miss = covert(productList);
        try {
            HttpUtils.doPost(PRODUCT_SERVICE_URL + "/product/reupload/b2b", miss);
        } catch (Exception e) {
            e.printStackTrace();
        }
        NotFoundUtils.ProductNotFound(paramString, sign);
    }

    /**
     * 封装调用接口信息
     *
     * @param productList 物料信息
     * @return
     */
    private List<Map<String, Object>> covert(List<NotExistProduct> productList) {
        List<Map<String, Object>> maps = new ArrayList<>();
        for (NotExistProduct product : productList) {
            Map<String, Object> map = new HashMap<>(productList.size());
            map.put("enuu", product.getEnuu());
            map.put("code", product.getCode());
            maps.add(map);
        }
        return maps;
    }
}
