package com.nantian.cloud.jljc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.ProductInfo;
import com.nantian.cloud.jljc.mapper.ProductInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.service.ProductInfoService;
import com.nantian.cloud.jljc.vo.ProductInfoTreeVo;
import com.nantian.cloud.jljc.vo.ProductInfoVo;
import com.nantian.cloud.jljc.vo.TreeVo;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static com.nantian.cloud.jljc.vo.util.TreeVoUtil.findTree;

/**
 * <p>
 * 产品信息 服务实现类
 * </p>
 *
 * @author stz
 * @since 2020-08-05
 */
@Service("productInfoService")
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {

    @Override
    public List<ProductInfo> getInfoByStr(String productStrr) {
        String[] list = productStrr.split(",");
        return baseMapper.getInfoByStr(list);
    }

    @Override
    public List<ProductInfoTreeVo> getTreeByTrade() {
        List<ProductInfoTreeVo> lits = baseMapper.getTreeByTrade();
        return findTree(lits);
    }

    @Override
    public ProductInfoVo getInfoByName(String name) {
        return baseMapper.getInfoByName(name);
    }

    @Override
    public IPage<ProductInfo> getPageScope(Page page, ProductInfo productInfo) {
        //return baseMapper.getPageScope(page,productInfo,new DataScope());
        return baseMapper.getPageScope(page,productInfo);
    }

    @Override
    public List<String> getTrade() {
        return baseMapper.getTrade();
    }

    @Override
    public String getCustomCode(String prevProductNum) {
        List<String> productNumList = baseMapper.getCustomCode(prevProductNum);
        if(productNumList.size()==0){ //该产品下无自定义编码
            return "Z01";
        }else{
            //字符串集合排序
            Collections.sort(productNumList, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    String str1=(String) o1;
                    String str2=(String) o2;
                    if (str1.compareToIgnoreCase(str2)<0){
                        return -1;
                    }
                    return 1;
                }
            });
            String[] zs = productNumList.get(productNumList.size() - 1).split("Z");
            Integer num = Integer.parseInt(zs[zs.length-1])+1;  //最后一个自定义编码数字部分递增
            if(num<=9){
                return "Z0"+num;
            }else{
                return "Z"+num;
            }
        }
    }

    @Override
    public List<TreeVo> getTree() {
        List<TreeVo> lits = baseMapper.getTree();
        return findTree(lits);
    }

    @Override
    public List<Map<String,Object>> getProductInfoList() {
        return baseMapper.getProductInfoList();
    }
}
