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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.entity.CompanyType;
import com.nantian.cloud.jljc.entity.EnterpriseInfo;
import com.nantian.cloud.jljc.mapper.EnterpriseInfoMapper;
import com.nantian.cloud.jljc.service.CompanyTypeService;
import com.nantian.cloud.jljc.service.EnterpriseInfoService;
import com.nantian.cloud.jljc.vo.Query;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 *
 * @date 2018-09-27
 */
@Service("enterpriseInfoService")
public class EnterpriseInfoServiceImpl extends ServiceImpl<EnterpriseInfoMapper, EnterpriseInfo> implements EnterpriseInfoService {

    @Autowired
    private CompanyTypeService companyTypeService;

    @Override
    public EnterpriseInfo getInfoByCode(String epc) {
        //return baseMapper.getInfoByCode(epc);
        EnterpriseInfo info = baseMapper.getInfoByCode(epc);
        if (info != null) {
            return info;
        } else {
            EnterpriseInfo e = new EnterpriseInfo();
            e.setEpc(epc);
            e.setDelFlag("1");
            List<EnterpriseInfo> enterpriseInfoList = baseMapper.selectList(Wrappers.query(e));
            /*EnterpriseInfo enterpriseInfo = baseMapper.selectOne(new QueryWrapper<EnterpriseInfo>()
                    .eq("del_flag", "1")
                    .eq("epc", epc));
            if (enterpriseInfo != null){
                return enterpriseInfo;
            }else {
                return null;
            }*/
            if (enterpriseInfoList.size() > 0) {
                return enterpriseInfoList.get(enterpriseInfoList.size() - 1);
            } else {
                return null;
            }
        }
    }

    @Override
    public EnterpriseInfo getInfoNameByCode(String epc) {
        EnterpriseInfo info = baseMapper.getInfoNameByCode(epc);
        if(info!=null && info.getCompanyType()!=null){
            info.setCompanyTypeName(getTypeNameByStr(info.getCompanyType()));
        }
        return info;
    }

    @Override
    public String getNameByCode(String epc) {
        return baseMapper.getNameByCode(epc);
    }

    public IPage<EnterpriseInfo> selectEnterpriseTradeType(Page page, EnterpriseInfo enterpriseInfo) {
        return baseMapper.selectEnterpriseTradeType(page,enterpriseInfo);
    }

    @Override
    public IPage<EnterpriseInfo> getListBylevel(Page page, Query query) {
        IPage<EnterpriseInfo> levelPage = baseMapper.getListByLevel(page, query);
        List<EnterpriseInfo> newList = new ArrayList<>();
        if(levelPage.getRecords().size()>0){
            for (EnterpriseInfo record : levelPage.getRecords()) {
                if(record.getCompanyType()!=null){
                    record.setCompanyTypeName(getTypeNameByStr(record.getCompanyType()));
                }
                newList.add(record);
            }
        }
        levelPage.setRecords(newList);
        return levelPage;
    }

    @Override
    public List<Map<String,String>> queryList(Query query) {
        List<Map<String, String>> list = new ArrayList<>();
        if(query.getType()!=null){
            String[] split = query.getType().split(",");
            list = baseMapper.queryList(query, new DataScope(), split);
        } else {
            list = baseMapper.queryList(query, new DataScope(), null);
        }
        for (Map<String, String> map : list) {

        }
        return list;
    }

    public String getTypeNameByStr(String str) {
        //企业类别
        StringBuilder epcTypeStr = new StringBuilder();
        String[] splits = str.split(";");
        for (String split : splits) {
            if(split!=null && !"".equals(split)){
                CompanyType type = companyTypeService.getById(Long.valueOf(split));
                if(type!=null){
                    epcTypeStr.append(type.getTypeName());
                    epcTypeStr.append("，");
                }
            }
        }

        return  epcTypeStr.deleteCharAt(epcTypeStr.length() - 1).toString();
    }
}


