package com.kingtom.shengtai.app.srm.excel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import com.kingtom.kirin.app.system.model.SystemUser;
import com.kingtom.kirin.app.system.service.ISystemUserService;
import com.kingtom.kirin.core.common.excel.ExcelDataImportTask;
import com.kingtom.kirin.core.common.utils.JsonUtils;
import com.kingtom.kirin.core.common.utils.SpringContextUtils;
import com.kingtom.shengtai.api.model.SrmSupplierImportDTO;
import com.kingtom.shengtai.app.srm.model.ProductDir;
import com.kingtom.shengtai.app.srm.model.SrmSupplier;
import com.kingtom.shengtai.app.srm.service.IProductDirService;
import com.kingtom.shengtai.app.srm.service.ISrmSupplierService;
import org.apache.commons.lang3.StringUtils;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Copyright: Copyright (C) 2024 KingTom
 * , Inc. All rights reserved. <p>
 * Company: 成都诚唐科技有限责任公司<p>
 *
 * @author wany
 * @since 2024/7/8
 */
public class SrmSupplierImportTask extends ExcelDataImportTask<SrmSupplierImportDTO>{

    private ISrmSupplierService srmSupplierService;

    private ISystemUserService  systemUserService;

    private IProductDirService  productDirService;

    public SrmSupplierImportTask(int i, CountDownLatch latch, SrmSupplierImportDTO perosn){
        super(i, perosn, latch);
    }

    public ISrmSupplierService getSrmSupplierService(){
        if(srmSupplierService == null){
            srmSupplierService = SpringContextUtils.getBean(ISrmSupplierService.class);
        }
        return srmSupplierService;
    }

    public ISystemUserService getSystemUserService(){
        if(systemUserService == null){
            systemUserService = SpringContextUtils.getBean(ISystemUserService.class);
        }
        return systemUserService;
    }

    public IProductDirService getProductDirService(){
        if(productDirService == null){
            productDirService = SpringContextUtils.getBean(IProductDirService.class);
        }
        return productDirService;
    }

    @Override
    public void importData(SrmSupplierImportDTO dto){
        String person = null;
        if(StringUtils.isNotBlank(dto.getPerson())){
            List<SystemUser> systemUsersByName = getSystemUserService().findSystemUsersByName(dto.getPerson());
            person = systemUsersByName.stream().map(SystemUser::getId).findFirst().orElse(null);
            if(person != null){
                SrmSupplier byPerson = getSrmSupplierService().findByPerson(person);
                if(byPerson != null){
                    return;
                }
            }
        }
        SrmSupplier supplier = new SrmSupplier();
        supplier.setName(dto.getName());
        supplier.setMark(dto.getMark());
        supplier.setPerson(person);
        supplier.setPhone(dto.getPhone());
        supplier.setType(dto.getType());
        supplier.setAddress(dto.getAddress());
        supplier.setIntro(dto.getIntro());
        supplier.setIntro(dto.getIntro());
        if(StringUtils.isNotBlank(dto.getProduct())){
            List<ProductDir> productDirs = getProductDirService().findListByNames(
                    Arrays.asList(dto.getProduct().split(",")));
            supplier.setProduct(JsonUtils.toJson(productDirs.stream().map(ProductDir::getId).toList()));
        } else{
            supplier.setProduct(JsonUtils.toJson(new ArrayList<>()));
        }
        supplier.setSuperiorProduct(dto.getSuperiorProduct());
        getSrmSupplierService().create(supplier);
    }

}
