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

import com.uas.platform.b2b.core.util.DateUtils;
import com.uas.platform.b2b.core.util.StringUtil;
import com.uas.platform.b2b.core.util.ThreadUtils;
import com.uas.platform.b2b.dao.ProductDao;
import com.uas.platform.b2b.dao.ProductUsersDao;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.ProductUsers;
import com.uas.platform.b2b.ps.ProductUtils;
import com.uas.platform.b2b.service.ProductUsersService;
import com.uas.platform.b2b.support.UsageBufferedLogger;
import com.uas.platform.core.logging.BufferedLoggerManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hejq on 2017-11-08.
 */
@Service
public class ProductUsersServiceImpl implements ProductUsersService {

    @Autowired
    private ProductUsersDao productUsersDao;

    @Autowired
    private ProductDao productDao;

    private final static UsageBufferedLogger logger = BufferedLoggerManager.getLogger(UsageBufferedLogger.class);


    @Override
    public void deleteById(Long id) throws Exception {
        ProductUtils.deleteProductUser(id);
    }

    /**
     * 将物料信息同步到个人信息库
     *
     * @param productId
     */
    @Override
    public void coverToMyProduct(final Long productId, final Long enuu, final Long useruu, final String method) {
        logger.log(useruu, enuu,"我的产品库", "更新我的物料库", method + ":" + productId);
        ThreadUtils.task(new Runnable() {

            @Override
            public void run() {
                ProductUsers productUsers = productUsersDao.findByEnuuAndUseruuAndPrid(enuu, useruu, productId);
                if (null == productUsers) {
                    try {
                        // 用来存供应商企业的对应物料id
                        Long prId;
                        Product product = productDao.findOne(productId);
                        // 复制到供应商企业的物料
                        Product newProduct = new Product();
                        BeanUtils.copyProperties(product, newProduct);
                        // 将客户物料转入到本企业
                        List<Product> products = productDao.findByEnUUAndCode(enuu, product.getCode());
                        if (CollectionUtils.isEmpty(products)) {
                            newProduct.setId(null);
                            newProduct.setSourceId(product.getId());
                            newProduct.setSourceApp("B2B");
                            newProduct.setUserUU(useruu);
                            newProduct.setEnUU(enuu);
                            prId = ProductUtils.updateOne(newProduct);
                        } else {
                            // 如果根据型号，title ，brand在供应商本企业找到一样的，确定是同一个，直接转，否则修改物料编号
                            List<Product> cmpProducts = productDao.findByTitleAndCmpCodeAndBrandAndEnUU(
                                    product.getTitle(), product.getCmpCode(), product.getBrand(), enuu);
                            if (!CollectionUtils.isEmpty(cmpProducts)) {
                                prId = cmpProducts.get(0).getId();
                            } else {
                                //生成随机编码
                                String codePrefix = DateUtils.format(new Date(), "yyMMddHHmmss") + StringUtil.getRandomNumber(3);
                                newProduct.setCode(codePrefix);
                                prId = ProductUtils.updateOne(product);
                            }
                        }
                        List<Long> idList = new ArrayList<>();
                        idList.add(prId);
                        ProductUtils.coverToUserProd(idList, enuu, useruu);
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.log("我的产品库", method + ":" + productId + "转入失败", useruu, null);
                    }
                }
            }
        }).run();
    }
}