package com.fjhx.data.service.t.impl;

import com.alibaba.fastjson.JSON;
import com.fjhx.data.entity.t.po.TErpProduct;
import com.fjhx.data.mapper.t.TErpProductMapper;
import com.fjhx.data.service.t.TErpProductService;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.service.product.ProductClassifyService;
import com.fjhx.item.service.product.ProductInfoService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
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;


/**
 * <p>
 * 产品信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-06
 */
@Service
public class TErpProductServiceImpl implements TErpProductService {

    @Autowired
    private ProductClassifyService productClassifyService;

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private TErpProductMapper tErpProductMapper;


    @Override
    public void transferProduct() {
        List<TErpProduct> list = tErpProductMapper.getList();
//        List<ProductClassify> classifyList = productClassifyService.list();
//        Map<String,Long> map = classifyList.stream().distinct().collect(Collectors.toMap(ProductClassify::getYunfanId,ProductClassify::getId));
//        List<SysUser> userList = iSysUserService.list(Wrappers.<SysUser>query().lambda().isNotNull(SysUser::getYfUserId));
//        Map<String,Long> userMap = userList.stream().distinct().collect(Collectors.toMap(SysUser::getYfUserId,SysUser::getUserId));
        List<ProductInfo> productInfoList = new ArrayList<>();
        for(TErpProduct p:list){
            ProductInfo productInfo = new ProductInfo();
            productInfo.setDefinition(1);
//            productInfo.setProductClassifyId(map.getOrDefault(p.getCategoryId(),null));
            productInfo.setCode(p.getCode());
            productInfo.setCustomCode(p.getCustomsCode());
            productInfo.setName(p.getNameChinese());
            productInfo.setSpec(p.getProductModelChinese());
            productInfo.setUnit(p.getUnit());
            productInfo.setRemark(p.getRemark());
//            productInfo.setYfId(p.getId());
//            productInfo.setYfTenantId(p.getYfTenantId());
            productInfo.setType("finish");
            productInfo.setCreateTime(p.getCreateTime());
            productInfo.setUpdateTime(p.getUpdateTime());
//            if(ObjectUtils.isNotEmpty(p.getCreateUser())){
//                productInfo.setCu(userMap.getOrDefault(p.getCreateUser(),null));
//            }
//            if(StringUtils.isNotEmpty(p.getUpdateUser())){
//                productInfo.setUu(userMap.getOrDefault(p.getUpdateUser(),null));
//            }
            Map<String,Object> standard_json = new HashMap<>();
            standard_json.put("englishName",p.getNameEnglish());
            standard_json.put("code",p.getCode());
            standard_json.put("netWeight",p.getNetWeight());
            standard_json.put("customsCode",p.getCustomsCode());
            productInfo.setStandardJson(JSON.toJSONString(standard_json));
//            Map<String,Object> ehsd_json = new HashMap<>();
//            ehsd_json.put("status",p.getStatus());
//            ehsd_json.put("assessStatus",p.getAssessStatus());
//            ehsd_json.put("customerId",p.getCustomerId());
//            productInfo.setEhsdJson(JSON.toJSONString(ehsd_json));
            if(StringUtils.equals(p.getYfTenantId(),"TIANHONG")||StringUtils.equals(p.getYfTenantId(),"GOLDSUN")){
                productInfoList.add(productInfo);
            }
        }
        productInfoService.saveBatch(productInfoList);
    }

}
