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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.data.entity.classify.ProductClassify;
import com.fjhx.data.mapper.classify.ProductClassifyMapper;
import com.fjhx.data.service.classify.ProductClassifyService;
import com.ruoyi.common.core.domain.entity.SysUser;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author zlj
 * @since 2023-03-15
 */
@DS(SourceConstant.DATA)
@Service
public class ProductClassifyServiceImpl extends ServiceImpl<ProductClassifyMapper, ProductClassify> implements ProductClassifyService {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 分类数据传输
     */
    @Override
    public void transferClass() {
        List<ProductClassify> list = this.list();
        Map<String,Long> map = list.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));
        for(ProductClassify p : list){
            if(StringUtils.equals(p.getYunfanParentId(),"0")){
                p.setParentId(0L);
            }else{
                p.setParentId(map.get(p.getYunfanParentId()));
            }
            if(StringUtils.isNotEmpty(p.getParentIdSet())){
                List<String> strList = Arrays.asList(p.getParentIdSet().split(","));
                for(String s : strList){
                    StringBuilder stringBuilder = new StringBuilder();
                    if(s.equals("0")){
                        stringBuilder.append("0");
                    }else{
                        stringBuilder.append(map.get(s));
                    }
                    p.setParentIdSet(stringBuilder.toString());
                }
            }
//            if(StringUtils.isNotEmpty(p.getCu())){
//                p.setCreateUser(userMap.get(p.getCu()));
//            }
//            if(StringUtils.isNotEmpty(p.getUu())){
//                p.setUpdateUser(userMap.get(p.getUu()));
//            }
        }
        this.updateBatchById(list);
    }

}
