package com.heketai.website.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.common.result.PageHelper;
import com.heketai.website.common.utils.ListUtils;
import com.heketai.website.common.vo.AllProductRequest;
import com.heketai.website.common.vo.AttrScope;
import com.heketai.website.common.vo.ProductCategoryVO;
import com.heketai.website.common.vo.ProductVO;
import com.heketai.website.entity.Product;
import com.heketai.website.entity.ProductCategory;
import com.heketai.website.entity.ProductCategoryAttr;
import com.heketai.website.entity.dto.ProductAttrsDto;
import com.heketai.website.entity.dto.ProductDto;
import com.heketai.website.entity.dto.SearchDto;
import com.heketai.website.mapper.ProductCategoryAttrMapper;
import com.heketai.website.mapper.ProductMapper;
import com.heketai.website.service.IProductCategoryService;
import com.heketai.website.service.IProductService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author xjl
 * @since 2019-12-17
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    private static final Logger log = LoggerFactory.getLogger(ProductServiceImpl.class);
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IProductCategoryService productCategoryService;

    @Autowired
    private ProductCategoryAttrMapper productCategoryAttrMapper;

    @Override
    public List<Product> findProducts(Page<ProductDto> page) {
        ProductDto p = page.getRecords().get(0);
        List<Product> tempList = productMapper.queryProductVo(p, (page.getCurrent() - 1) * page.getSize(), page.getSize());
        return tempList;
    }

    /**
     * 筛选器取值范围得到数据查询实现
     */
    @Override
    public Map<String, Object> findAllScope(Page<ProductAttrsDto> page) {
        //创建返回map
        Map<String, Object> map = new HashMap<>();
        ProductAttrsDto productAttrsDto = page.getRecords().get(0);
        //判断是否有此分类
        ProductCategory category = new ProductCategory();
        category.setId(productAttrsDto.getCategoryId());
        category = productCategoryService.selectById(category);

        //拿到所有数据
        Product pro = new Product();
        pro.setCategoryId(productAttrsDto.getCategoryId());
        EntityWrapper<Product> productWrapper = new EntityWrapper<>(pro);
        List<Product> products = productMapper.selectList(productWrapper);


        //拿到attr介绍数据
        ProductCategoryAttr findAttr = new ProductCategoryAttr();
        findAttr.setCategoryId(productAttrsDto.getCategoryId());
        EntityWrapper<ProductCategoryAttr> wrapper = new EntityWrapper<>();
        wrapper.setEntity(findAttr);
        List<ProductCategoryAttr> attrs = productCategoryAttrMapper.selectList(wrapper);

        /** 得到所有范围内的list */
        List<Product> pDict = scopeProductList(attrs, products, productAttrsDto);
        //创建判断是否有attrMap
        Map<Integer, Boolean> tempMap = new HashedMap();
        Collections.sort(attrs, (ProductCategoryAttr a, ProductCategoryAttr b) -> b.getSort().compareTo(a.getSort()));
        for (ProductCategoryAttr attr : attrs) {
            tempMap.put(attr.getSort(), true);
        }
        Product Head1 = new Product();
        Product Head2 = new Product();
        Product Head3 = new Product();

        Head1.setImg("产品图片");
        Head2.setImg("Picture");

        Head1.setManual("规格书");
        Head2.setManual("Datasheet");

        Head1.setPackageSpecification("封装形式");
        Head2.setPackageSpecification("Package");

        Head1.setManualCh("英文网站规格书");
        Head2.setManualCh("Datasheet");

        Head1.setSilkScreen("丝印");
        Head2.setSilkScreen("Marking");

        Head1.setRemarks("备注");
        Head2.setRemarks("Datasheet");

        if (category.getIsSummary() == 1) {
            //汇总报表
            map.put("Summary", true);
            Head1.setGoodsType("名称");
            Head2.setGoodsType("Name");

            for (ProductCategoryAttr attr : attrs) {
                Head1.SetAttr(attr.getSort(), attr.getNameCh());
                Head2.SetAttr(attr.getSort(), attr.getNameEn());
                Head3.SetAttr(attr.getSort(), attr.getUnits());
            }

            Product PT = new Product();
            PT.setCategoryId(productAttrsDto.getCategoryId());
            EntityWrapper<Product> productEntityWrapper = new EntityWrapper<>();
            productEntityWrapper.setEntity(PT);
            List<Product> productList = productMapper.selectList(productEntityWrapper);
            Product product = null;
            if (!CollectionUtils.isEmpty(productList)) {
                product = productList.get(0);
            }

            Page<Product> productPage = new Page<>();
            if (product != null) {
                product.setGoodsType(product.getGoodsType().replace("|", "<br/>"));
                product.setPackageSpecification(product.getPackageSpecification().replace("|", "<br/>"));

                for (Map.Entry<Integer, Boolean> entry : tempMap.entrySet()) {
                    product.SetAttr(entry.getKey(), product.GetAttr(entry.getKey()).replace("|", "<br/>"));
                }

                if (com.heketai.website.common.utils.StringUtils.isNull(product.getImg())) {
                    product.setImg(category.getImg());
                }


                productPage.setTotal(1);
                productPage.setCurrent(1);
                productPage.setSize(1);
                List<Product> tempList = new ArrayList<>();
                tempList.add(product);
                productPage.setRecords(tempList);
            }

            map.put("page", productPage);
        } else {
            //产品明细
            map.put("Summary", false);

            Head1.setGoodsType("型号");
            Head2.setGoodsType("Type");

            Head1.setGoodsSn("合科泰型号");
            Head2.setGoodsSn("Hottech Type");
            Map<String, List<String>> tempMapList = new HashedMap();
            for (ProductCategoryAttr attr : attrs) {
                Head1.SetAttr(attr.getSort(), attr.getNameCh());
                Head2.SetAttr(attr.getSort(), attr.getNameEn());
                Head3.SetAttr(attr.getSort(), attr.getUnits());
                tempMapList.put("attr" + attr.getSort(), new ArrayList<>());
            }
            tempMapList.put("packageSpecification", new ArrayList<>());
            tempMapList.put("goodsType", new ArrayList<>());

            //代表所有数字的正则表达式
            String regex = "^(-?\\d+)(\\.\\d+)?$";
            for (Product pr : pDict) {
                //把所有封装放入tempMapList中
                String temp = pr.getPackageSpecification();
                List<String> tempList1 = tempMapList.get("packageSpecification");
                if (!tempList1.contains(temp)) {
                    tempList1.add(temp);
                }
                //把所有型号放入tempMapList中
                String goodsType = pr.getGoodsType();
                List<String> goodsTypes = tempMapList.get("goodsType");
                if (!goodsTypes.contains(goodsType)) {
                    goodsTypes.add(goodsType);
                }
                //把所有arrt数据放入tempMapList中
                for (ProductCategoryAttr attr : attrs) {
                    temp = pr.GetAttr(attr.getSort());
                    String keyCode = "attr" + attr.getSort();
                    tempList1 = tempMapList.get(keyCode);
                    if (StringUtils.isNotBlank(temp) && temp.matches(regex)) {
                        if (!tempList1.contains(temp)) tempList1.add(temp);
                        ListUtils.sortList(tempList1);
                    } else {
                        if (org.apache.commons.lang3.StringUtils.isNotBlank(temp)) {
                            if (!tempList1.contains(temp)) tempList1.add(temp);
                            Collections.sort(tempList1);
                        }
                    }
                }

            }
            map.put("SortData", tempMapList);

            Page<Product> productPage = new Page<>();
            productPage.setTotal(pDict.size());
            productPage.setCurrent(page.getCurrent());
            productPage.setSize(page.getSize());

            //拿到分页数据
            List<Product> tempList = ListUtils.subList(pDict, page.getCurrent(), page.getSize());

            for (Product pp : tempList) {
                if (com.heketai.website.common.utils.StringUtils.isNull(pp.getImg())) {
                    pp.setImg(category.getImg());
                }
            }

            productPage.setRecords(tempList);
            map.put("page", productPage);
        }
        map.put("typeHead1", Head1);
        map.put("typeHead2", Head2);
        map.put("typeHead3", Head3);
        map.put("category", category);
        map.put("productAttrsDto", productAttrsDto);
        return map;
    }

    public Page<ProductAttrsDto> onsetScope(Integer id) {
        Product product = new Product();
        product.setCategoryId(id);
        product.setSort(2);
        EntityWrapper<Product> productEntityWrapper = new EntityWrapper<>(product);
        List<Product> productList = productMapper.selectList(productEntityWrapper);
        if (CollectionUtils.isEmpty(productList)) {
            throw new RuntimeException("没有此分类");
        }
        ProductAttrsDto pd = new ProductAttrsDto();
        pd = addProductAttrsDto(pd, productList);
        List<ProductAttrsDto> list = new ArrayList<>();
        list.add(pd);
        Page<ProductAttrsDto> page = new Page<>();
        page.setRecords(list);
        page.setTotal(productList.size());
        return page;
    }

    /**
     * 搜索模块2022/8/22
     */
    @Override
    public Map<String, Map<String, Object>> search(Page<SearchDto> page) {
        String searchValue = page.getRecords().get(0).getSearchValue();
        String categoryId = page.getRecords().get(0).getCategoryId();
        if (StringUtils.isBlank(searchValue)) {
            throw new RuntimeException("搜索数据为空，请输入搜索数据");
        }
        Map<String, Map<String, Object>> map = new HashMap<>();
        //搜索到所有数据
        List<Product> productList = productMapper.queryBySearch(searchValue);
        //过滤掉所有分类重复数据，
        ArrayList<Product> collect = productList.stream().collect(Collectors.collectingAndThen
                (Collectors.toCollection(() -> new TreeSet<>
                        (Comparator.comparing(Product::getCategoryId))), ArrayList::new));

        if (StringUtils.isBlank(categoryId)) {
            for (Product pt : collect) {
                List<Product> pDict = new ArrayList<>();
                for (Product product : productList) {
                    if (pt.getCategoryId().equals(product.getCategoryId())) {
                        pDict.add(product);
                    }
                }
                Map<String, Object> map1 = addProductMpa(pt.getCategoryId().toString(), pDict, page);
                map.put("productList" + pt.getCategoryId(), map1);

            }
        } else {
            List<Product> pDict = new ArrayList<>();
            for (Product product : productList) {
                if (categoryId.equals(product.getCategoryId().toString())) {
                    pDict.add(product);
                }
            }
            Map<String, Object> map1 = addProductMpa(categoryId, pDict, page);
            map.put("productList" + categoryId, map1);
        }


        return map;
    }

    /**
     * 搜索模块2022/4/1
     * categoryId：二级分类Id
     * seek:要搜索的内容
     * seekFields:下拉框内容
     * seekType:搜索的类别(1:表示阻容。2:表示分离器件)
     * page:页码
     * size:每一页条数
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseResult findByDevice(AllProductRequest request) {
        int page = request.getPage();
        int size = request.getSize();
        size = size <= 0 ? 10 : size;
        int start = (page - 1) * size < 0 ? 0 : (page - 1) * size;
        Map map = new HashMap();
        try {

            //查询数据库查询所有数据
            List<ProductVO> productVOList = productMapper.findByAll(request);
            if (CollectionUtils.isEmpty(productVOList) || productVOList.size() <= 0) {
                return BaseResult.successWithData(map);
            }
            Map map1 = new HashMap();
            //如果是阻容处理数据并返回
            if ("1".equals(request.getSeekType())) {
                List<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
                for (ProductVO productVO : productVOList) {
                    ProductCategoryVO productCategoryVO = new ProductCategoryVO();
                    ProductCategoryVO vfpProductCategory = productVO.getVFPProductCategory();
                    productCategoryVO.setId(productVO.getCategoryId());
                    productCategoryVO.setNameCh(vfpProductCategory.getNameCh());
                    productCategoryVO.setNameEn(vfpProductCategory.getNameEn());
                    String name = "productVOS" + productVO.getCategoryId();
                    productCategoryVO.setProductCategoryName(name);
                    if (productCategoryVO != null && null != productCategoryVO.getId()) {
                        //把分类Attr详情返回给前端
                        EntityWrapper<ProductCategoryAttr> wrapper = new EntityWrapper<>();
                        ProductCategoryAttr attr = new ProductCategoryAttr();
                        attr.setCategoryId(productCategoryVO.getId());
                        wrapper.setEntity(attr);
                        List<ProductCategoryAttr> productCategoryAttrs = productCategoryAttrMapper.selectList(wrapper);
                        productCategoryVO.setProductCategoryAttrList(productCategoryAttrs);
                        productCategoryVOList.add(productCategoryVO);
                    }

                    map1.put(name, productVO);

                }
                //把请求显示分类放入map
                map.put("productCategoryVOList", productCategoryVOList);
                //把请求显示数据放入map
                map.put("productVOMap", map1);
                return BaseResult.successWithData(map);
            }

            //如果是分离器件则处理数据并返回
            /** 1.拿到搜索到的所有分类信息  */
            ArrayList<ProductVO> collect = productVOList.stream().collect(Collectors.collectingAndThen
                    (Collectors.toCollection(() -> new TreeSet<>
                            (Comparator.comparing(ProductVO::getCategoryId))), ArrayList::new));

            Map<String, List<ProductVO>> mapList = new HashMap<>();
            List<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
            /** 2.把所有数据按照分类，进行整理，整理得到一个mapList。和分类数据productCategoryVOList */
            List<ProductVO> productVOS = new ArrayList<>();
            for (ProductVO vo : collect) {
                ProductCategoryVO productCategoryVO = new ProductCategoryVO();
                productCategoryVO.setId(vo.getCategoryId());
                for (ProductVO productVO : productVOList) {
                    if (vo.getCategoryId().toString().trim().equals(productVO.getCategoryId().toString().trim())) {
                        productVOS.add(productVO);
                    }
                }
                String name = "productVOS" + vo.getCategoryId();
                ProductCategoryVO vfpProductCategory = vo.getVFPProductCategory();
                if (null != vfpProductCategory && null != vfpProductCategory.getId()) {
                    productCategoryVO.setNameCh(vfpProductCategory.getNameCh());
                    productCategoryVO.setNameEn(vfpProductCategory.getNameEn());
                } else {
                    productCategoryVO.setNameCh(vo.getGoodsType().replaceAll("[^\u4e00-\u9fa5]", " ").trim());
                    productCategoryVO.setNameEn(vo.getGoodsType().replaceAll("[^a-z^A-z_]", " ").trim());
                }
                productCategoryVO.setProductCategoryName(name);

                if (productCategoryVO != null && null != productCategoryVO.getId()) {
                    //把分类Attr详情返回给前端
                    EntityWrapper<ProductCategoryAttr> wrapper = new EntityWrapper<>();
                    ProductCategoryAttr attr = new ProductCategoryAttr();
                    attr.setCategoryId(productCategoryVO.getId());
                    wrapper.setEntity(attr);
                    List<ProductCategoryAttr> productCategoryAttrs = productCategoryAttrMapper.selectList(wrapper);
                    productCategoryVO.setProductCategoryAttrList(productCategoryAttrs);
                    productCategoryVOList.add(productCategoryVO);
                }


                mapList.put(name, productVOS);
            }

            /** 4.判断是否是第一次搜索如果是第一次搜索request.getCategoryId()就为空 */
            if (StringUtils.isBlank(request.getCategoryId())) {
                /** 4.1如果是第一次搜索就把所有分类的分页数据放入返回的map中 */
                Map map2 = putMap(mapList, size);
                map.put("productVOMap", map2);
                /** 3.把所有分类数据productCategoryVOList放入到要返回分map */
                map.put("productCategoryVOList", productCategoryVOList);

            } else {
                /**4.2如果是带分类搜索，就把本分类的分页数据放入返回的map中*/
                Map<String, PageHelper> stringPageHelperMap = setMap(request.getCategoryId(), mapList, size, start);
                map.put("productVOMap", stringPageHelperMap);
            }

            return BaseResult.successWithData(map);
        } catch (Exception e) {
            log.error("ES局查询失败，失败原因：[{}]", e.getLocalizedMessage());
            return BaseResult.fail("es全局查询");
        }
    }


    /**
     * 拿到分离器件所有封装信息
     */
    @Override
    public BaseResult findByFz(int sort) {
        List<String> byFz = productMapper.findByFz(sort);
        return BaseResult.successWithData(byFz);
    }

    /**
     * 拿到电阻电容所有精度信息
     */
    @Override
    public BaseResult findByJd(int sort) {
        List<String> byJd = productMapper.findByJd(sort);
        List<String> stringList = new ArrayList<>();
        for (String s : byJd) {
            String replace = s.replace("|", ",");
            String[] split = replace.split(",");
            for (String s1 : split) {
                stringList.add(s1.trim());
            }
        }
        List<String> collect = stringList.stream().distinct().collect(Collectors.toList());
        return BaseResult.successWithData(collect);
    }


    /**
     * 如果前端没有传categoryId参数，则返回默认数据
     */
    public Map putMap(Map<String, List<ProductVO>> mapList, int size) {
        Map<String, PageHelper> map = new HashMap<>();
        for (String s : mapList.keySet()) {
            List<ProductVO> productVOS = mapList.get(s);
            if (!CollectionUtils.isEmpty(productVOS) && productVOS.size() > 0) {
                int count = productVOS.size();
                int pageCount = count % size == 0 ? count / size : count / size + 1;
                List<ProductVO> productVOSN = new ArrayList<>();
                int m = productVOS.size() < size ? productVOS.size() : size;
                for (int i = 0; i < m; i++) {
                    productVOSN.add(productVOS.get(i));
                }
                map.put(s, new PageHelper(count, pageCount, productVOSN));
            } else {
                map = null;
            }
        }
        return map;
    }

    /**
     * 如果categoryId有值，则返回本分类下的数据
     */
    public Map<String, PageHelper> setMap(String categoryId, Map<String, List<ProductVO>> mapList, int size, int start) {
        Map map = new HashMap();
        String name = "productVOS" + categoryId;
        List<ProductVO> productVOS = mapList.get(name);
        if (CollectionUtils.isEmpty(productVOS) || productVOS.size() <= 0) {
            map = null;
        } else {
            int count = productVOS.size();
            int pageCount = count % size == 0 ? count / size : count / size + 1;
            int n = size + start > productVOS.size() ? productVOS.size() : size + start;
            List<ProductVO> productVOSN = new ArrayList<>();
            for (int i = start; i < n; i++) {
                productVOSN.add(productVOS.get(i));
            }
            map.put(name, new PageHelper(count, pageCount, productVOSN));
        }
        return map;
    }

    /**
     * 得到所有attr最大最小数据
     */
    public ProductAttrsDto addProductAttrsDto(ProductAttrsDto pd, List<Product> productList) {
        String regex = "^(-?\\d+)(\\.\\d+)?$";
        for (Product pr : productList) {
            String attr1 = pr.getAttr1();
            AttrScope scope1 = pd.getAttr1();
            AttrScope attrScope1 = disposedAttr(attr1, scope1, regex);

            String attr2 = pr.getAttr2();
            AttrScope scope2 = pd.getAttr2();
            AttrScope attrScope2 = disposedAttr(attr2, scope2, regex);

            String attr3 = pr.getAttr3();
            AttrScope scope3 = pd.getAttr3();
            AttrScope attrScope3 = disposedAttr(attr3, scope3, regex);

            String attr4 = pr.getAttr4();
            AttrScope scope4 = pd.getAttr4();
            AttrScope attrScope4 = disposedAttr(attr4, scope4, regex);

            String attr5 = pr.getAttr5();
            AttrScope scope5 = pd.getAttr5();
            AttrScope attrScope5 = disposedAttr(attr5, scope5, regex);

            String attr6 = pr.getAttr6();
            AttrScope scope6 = pd.getAttr6();
            AttrScope attrScope6 = disposedAttr(attr6, scope6, regex);

            String attr7 = pr.getAttr7();
            AttrScope scope7 = pd.getAttr7();
            AttrScope attrScope7 = disposedAttr(attr7, scope7, regex);

            String attr8 = pr.getAttr8();
            AttrScope scope8 = pd.getAttr8();
            AttrScope attrScope8 = disposedAttr(attr8, scope8, regex);

            String attr9 = pr.getAttr9();
            AttrScope scope9 = pd.getAttr9();
            AttrScope attrScope9 = disposedAttr(attr9, scope9, regex);

            String attr10 = pr.getAttr10();
            AttrScope scope10 = pd.getAttr10();
            AttrScope attrScope10 = disposedAttr(attr10, scope10, regex);

            pd.setAttr1(attrScope1);
            pd.setAttr2(attrScope2);
            pd.setAttr3(attrScope3);
            pd.setAttr4(attrScope4);
            pd.setAttr5(attrScope5);
            pd.setAttr6(attrScope6);
            pd.setAttr7(attrScope7);
            pd.setAttr8(attrScope8);
            pd.setAttr9(attrScope9);
            pd.setAttr10(attrScope10);
        }
        return pd;
    }

    /**
     * 得到一条attr最大最小数据
     */
    public AttrScope disposedAttr(String attr, AttrScope scope, String regex) {
        if (StringUtils.isNotBlank(attr)) {
            if (scope == null) {
                scope = new AttrScope();
            }
            String smallAttr = scope.getSmallAttr() == null ? "500000" : scope.getSmallAttr();
            String maxAttr = scope.getMaxAttr() == null ? "-500000" : scope.getMaxAttr();
            if (attr.matches(regex)) {
                if (Double.valueOf(attr) > Double.valueOf(maxAttr)) {
                    scope.setMaxAttr(attr);
                }
                if (Double.valueOf(attr) < Double.valueOf(smallAttr)) {
                    scope.setSmallAttr(attr);
                }
            } else if (attr.contains("|")) {
                String[] split = attr.split("\\|");
                for (String s : split) {
                    if (Double.valueOf(s) > Double.valueOf(maxAttr)) {
                        scope.setMaxAttr(s);
                    }
                    if (Double.valueOf(s) < Double.valueOf(smallAttr)) {
                        scope.setSmallAttr(s);
                    }
                }
            } else if (attr.contains("/")) {
                String[] split = attr.split("/");
                for (String s : split) {
                    if (Double.valueOf(s) > Double.valueOf(maxAttr)) {
                        scope.setMaxAttr(s);
                    }
                    if (Double.valueOf(s) < Double.valueOf(smallAttr)) {
                        scope.setSmallAttr(s);
                    }
                }
            } else if (attr.contains("~")) {
                String[] split = attr.split("~");
                for (String s : split) {
                    if (Double.valueOf(s) > Double.valueOf(maxAttr)) {
                        scope.setMaxAttr(s);
                    }
                    if (Double.valueOf(s) < Double.valueOf(smallAttr)) {
                        scope.setSmallAttr(s);
                    }
                }
            }

        }
        return scope;
    }


    /**
     * 把所有范围筛选的数据放入list并返回
     */
    public List<Product> scopeProductList(List<ProductCategoryAttr> attrs,
                                          List<Product> products, ProductAttrsDto attrsDto) {
        String regex = "^(-?\\d+)(\\.\\d+)?$";
        List<Product> pDict = new ArrayList<>();
        for (Product pr : products) {
            Boolean bl = true;
            //把所有arrt范围的数据放入pDict中
            for (ProductCategoryAttr attr : attrs) {
                String temp = pr.GetAttr(attr.getSort());
                AttrScope scope = attrsDto.GetAttr(attr.getSort());
                if (scope != null && StringUtils.isNotBlank(scope.getMaxAttr())
                        && StringUtils.isNotBlank(scope.getSmallAttr())) {
                    if (StringUtils.isBlank(temp)) {
                        Page<ProductAttrsDto> dtoPage = onsetScope(attrsDto.getCategoryId());
                        ProductAttrsDto productAttrsDto = dtoPage.getRecords().get(0);
                        Map<String, AttrScope> dtoMap = JSON.parseObject(JSON.toJSONString(productAttrsDto),
                                new TypeReference<Map<String, AttrScope>>() {
                                });
                        AttrScope scope1 = dtoMap.get(attr.getAttrValue());
                        if (scope.getMaxAttr().equals(scope1.getMaxAttr()) && scope.getSmallAttr().equals(scope1.getSmallAttr())) {
                            continue;
                        } else {
                            bl = false;
                            break;
                        }

                    }
                    if (temp.matches(regex)) {
                        Double temp1 = Double.valueOf(temp);
                        Double maxAttr = Double.valueOf(scope.getMaxAttr());
                        Double smallAttr = Double.valueOf(scope.getSmallAttr());
                        if (temp1 > maxAttr || temp1 < smallAttr) {
                            bl = false;
                            break;
                        }
                    } else if (temp.contains("|")) {
                        int i = 0;
                        String[] split = temp.split("\\|");
                        for (String s : split) {
                            if (s.matches(regex)) {
                                Double temp1 = Double.valueOf(s);
                                Double maxAttr = Double.valueOf(scope.getMaxAttr());
                                Double smallAttr = Double.valueOf(scope.getSmallAttr());
                                if (temp1 > maxAttr || temp1 < smallAttr) {
                                    i++;
                                }
                            }
                        }
                        if (bl) {
                            bl = i == 2 ? false : true;
                        }
                    } else if (temp.contains("/")) {
                        int i = 0;
                        String[] split = temp.split("/");
                        for (String s : split) {
                            if (s.matches(regex)) {
                                Double temp1 = Double.valueOf(s);
                                Double maxAttr = Double.valueOf(scope.getMaxAttr());
                                Double smallAttr = Double.valueOf(scope.getSmallAttr());
                                if (temp1 > maxAttr || temp1 < smallAttr) {
                                    i++;
                                }
                            }
                        }
                        if (bl) {
                            bl = i == 2 ? false : true;
                        }
                    } else if (temp.contains("~")) {
                        int i = 0;
                        String[] split = temp.split("~");
                        for (String s : split) {
                            if (s.matches(regex)) {
                                Double temp1 = Double.valueOf(s);
                                Double maxAttr = Double.valueOf(scope.getMaxAttr());
                                Double smallAttr = Double.valueOf(scope.getSmallAttr());
                                if (temp1 > maxAttr || temp1 < smallAttr) {
                                    i++;
                                }
                            }
                        }
                        if (bl) {
                            bl = i == 2 ? false : true;
                        }
                    }
                }
            }
            if (bl) {
                pDict.add(pr);
            }
        }
        return pDict;

    }


    public Map<String, Object> addProductMpa(String categoryId, List<Product> pDict, Page<SearchDto> page) {
        //创建返回map
        Map<String, Object> map = new HashMap<>();
        //拿到attr介绍数据
        ProductCategoryAttr findAttr = new ProductCategoryAttr();
        findAttr.setCategoryId(Integer.valueOf(categoryId));
        EntityWrapper<ProductCategoryAttr> wrapper = new EntityWrapper<>();
        wrapper.setEntity(findAttr);
        List<ProductCategoryAttr> attrs = productCategoryAttrMapper.selectList(wrapper);

        ProductCategory category = productCategoryService.selectById(categoryId);

        //创建判断是否有attrMap
        Map<Integer, Boolean> tempMap = new HashedMap();
        Collections.sort(attrs, (ProductCategoryAttr a, ProductCategoryAttr b) -> b.getSort().compareTo(a.getSort()));

        for (ProductCategoryAttr attr : attrs) {
            tempMap.put(attr.getSort(), true);
        }

        Product Head1 = new Product();
        Product Head2 = new Product();

        Head1.setImg("产品图片");
        Head2.setImg("Picture");

        Head1.setPackageSpecification("封装形式");
        Head2.setPackageSpecification("Package");

        Head1.setManualCh("英文网站规格书");
        Head2.setManualCh("Datasheet");

        Head1.setSilkScreen("丝印");
        Head2.setSilkScreen("Marking");

        Head1.setRemarks("备注");
        Head2.setRemarks("Datasheet");
        Product Head3 = new Product();

        if (category.getIsSummary() == 1) {
            //汇总报表
            map.put("Summary", true);
            Head1.setGoodsType("名称");
            Head2.setGoodsType("Name");

            for (ProductCategoryAttr attr : attrs) {
                Head1.SetAttr(attr.getSort(), attr.getNameCh());
                Head2.SetAttr(attr.getSort(), attr.getNameEn());
                Head3.SetAttr(attr.getSort(), attr.getUnits());
            }

            Product PT = new Product();
            PT.setCategoryId(Integer.valueOf(categoryId));
            EntityWrapper<Product> productEntityWrapper = new EntityWrapper<>();
            productEntityWrapper.setEntity(PT);
            Product product = selectOne(productEntityWrapper);

            Page<Product> productPage = new Page<>();
            if (product != null) {
                product.setGoodsType(product.getGoodsType().replace("|", "<br/>"));
                product.setPackageSpecification(product.getPackageSpecification().replace("|", "<br/>"));

                for (Map.Entry<Integer, Boolean> entry : tempMap.entrySet()) {
                    product.SetAttr(entry.getKey(), product.GetAttr(entry.getKey()).replace("|", "<br/>"));
                }

                if (com.heketai.website.common.utils.StringUtils.isNull(product.getImg())) {
                    product.setImg(category.getImg());
                }


                productPage.setTotal(1);
                productPage.setCurrent(1);
                productPage.setSize(1);
                List<Product> tempList = new ArrayList<>();
                tempList.add(product);
                productPage.setRecords(tempList);
            }

            map.put("page", productPage);
        } else {
            //产品明细
            map.put("Summary", false);

            Head1.setGoodsType("型号");
            Head2.setGoodsType("Type");

            Head1.setGoodsSn("合科泰型号");
            Head2.setGoodsSn("Hottech Type");
            Map<String, List<String>> tempMapList = new HashedMap();
            for (ProductCategoryAttr attr : attrs) {
                Head1.SetAttr(attr.getSort(), attr.getNameCh());
                Head2.SetAttr(attr.getSort(), attr.getNameEn());
                Head3.SetAttr(attr.getSort(), attr.getUnits());
                tempMapList.put("attr" + attr.getSort(), new ArrayList<>());
            }
            tempMapList.put("packageSpecification", new ArrayList<>());
            tempMapList.put("goodsType", new ArrayList<>());

            //代表所有数字的正则表达式
            String regex = "^(-?\\d+)(\\.\\d+)?$";
            for (Product pr : pDict) {
                Page<Product> productPage = new Page<>();
                productPage.setTotal(pDict.size());
                productPage.setCurrent(page.getCurrent());
                productPage.setSize(page.getSize());
                //拿到分页数据
                List<Product> tempList = ListUtils.subList(pDict, page.getCurrent(), page.getSize());
                for (Product pp : tempList) {
                    if (com.heketai.website.common.utils.StringUtils.isNull(pp.getImg())) {
                        pp.setImg(category.getImg());
                    }
                }
                productPage.setRecords(tempList);
                map.put("page", productPage);
            }

            map.put("typeHead1", Head1);
            map.put("typeHead2", Head2);
            map.put("typeHead3", Head3);
            map.put("category", category);


        }
        return map;
    }
}