package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmBaseStoreDao;
import com.ssm.manage.entity.BaseStaff;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.BaseStores;
import com.ssm.manage.mapper.BaseStoreMapper;
import com.ssm.manage.query.StoreRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class SsmBaseDaoImpl extends BaseDaoImpl<BaseStore> implements SsmBaseStoreDao {
    @Autowired
    private BaseStoreMapper mapper;

    @Override
    public BaseStoreMapper getMapper() {
        return mapper;
    }

    @Override
    public PageInfo<BaseStore> selectPage(StoreRequest storeRequest) {
        Example example = new Example(BaseStore.class);
        Example.Criteria criteria = example.createCriteria();
//        if (StringUtils.isNotBlank(storeRequest.getStoreNum())){
//            criteria.orLike("storeNum","%" + storeRequest.getStoreNum() + "%");
//        }
        if (StringUtils.isNotBlank(storeRequest.getStoreName())){
            criteria.andLike("storeName","%" + storeRequest.getStoreName() + "%");
        }
        if (StringUtils.isNotBlank(storeRequest.getRegistrantMobilePhone())){
            criteria.andLike("registrantMobilePhone","%" + storeRequest.getRegistrantMobilePhone() + "%");
        }
        if (StringUtils.isNotBlank(storeRequest.getTelephone())){
            criteria.andLike("telephone","%" + storeRequest.getTelephone() + "%");
        }
        if (storeRequest.getState() != null){
            criteria.andEqualTo("state",storeRequest.getState());
        }
        if (null != storeRequest.getStartTime() && null != storeRequest.getEndTime()){
            criteria.andBetween("funderDate",storeRequest.getStartTime(), storeRequest.getEndTime());
        }
        if(storeRequest.getType()==1){
            criteria.andIsNull("parentId");
        }

        PageHelper.startPage(storeRequest.getPageNum(),storeRequest.getPageSize(),storeRequest.getOrderAndSort());
        List<BaseStore> list = selectByExample(example);
        PageInfo<BaseStore> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询一个门店下的所有子门店
     * @param storeRequest
     * @return
     */
    @Override
    public PageInfo<BaseStores> selectPages(StoreRequest storeRequest) {
        BaseStore baseStore = new BaseStore();
        baseStore.setId(Long.valueOf(storeRequest.getId()));
        List<BaseStores> allRecursion = mapper.findAllRecursion(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursion(allRecursion,baseStores);
        PageInfo<BaseStores> pageInfo = new PageInfo<>(recursion);
        return pageInfo;
    }


    /**
     * 递归评级
     * @param allRecursion
     * @return
     */
    public List<BaseStores> recursion(List<BaseStores> allRecursion,List<BaseStores> baseStore){
        for (BaseStores baseStores : allRecursion) {
            if (baseStores.getChildren().size() > 0) {
                recursion(baseStores.getChildren(), baseStore);
            }
            if (baseStores != null) {
                //allRecursion.remove(baseStores);
                baseStore.add(baseStores);
            }
        }
        return baseStore;
    }


    /**
     * 查询一个门店的所有父门店
     * @param storeRequest
     * @return
     */
    @Override
    public PageInfo<BaseStores> selectPagesparent(StoreRequest storeRequest) {
        BaseStore baseStore = new BaseStore();
        baseStore.setId(Long.valueOf(storeRequest.getId()));
        List<BaseStores> allRecursion = mapper.findAllRecursions(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursionparent(allRecursion,baseStores);
        PageInfo<BaseStores> pageInfo = new PageInfo<>(recursion);
        return pageInfo;
    }

    /**
     * 递归评级
     * @param allRecursion
     * @return
     */
    public List<BaseStores> recursionparent(List<BaseStores> allRecursion,List<BaseStores> baseStore){
        for (BaseStores baseStores : allRecursion) {
            if(null != baseStores.getParent()){
                recursionparent(baseStores.getParent(),baseStore);
            }
            if(baseStores != null){
                //allRecursion.remove(baseStores);
                baseStore.add(baseStores);
            }
        }
        return baseStore;
    }

    /**
     * 查询当前门店所在组织下的所有门店
     */
    public List<BaseStores> pagesorganization(StoreRequest storeRequest){
        BaseStore baseStore = new BaseStore();
        baseStore.setId(Long.valueOf(storeRequest.getId()));
        List<BaseStores> allRecursion = mapper.findAllRecursions(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursionparent(allRecursion,baseStores);
        BaseStores baseStores1 = null;
        for (BaseStores stores : recursion) {
            if(null == stores.getParentId()){
                baseStores1=stores;
            }
        }

        baseStore.setId(Long.valueOf(baseStores1.getId()));

        List<BaseStores> allRecursions = mapper.findAllRecursion(baseStore);
        List<BaseStores> baseStoress = new ArrayList<>();
        List<BaseStores> recursions = this.recursion(allRecursions,baseStoress);
        for (int i = 0; i < recursions.size(); i++) {
            if(storeRequest.getId().longValue() == recursions.get(i).getId().longValue()){
                recursions.remove(i);
                continue;
            }
        }


        return recursions;
    }

    /**
     * 查询当前门店所在组织下的所有门店
     */
    public List<BaseStores> pagesorganizationim(StoreRequest storeRequest){
        BaseStore baseStore = new BaseStore();
        baseStore.setId(Long.valueOf(storeRequest.getId()));
        List<BaseStores> allRecursion = mapper.findAllRecursions(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursionparent(allRecursion,baseStores);
        BaseStores baseStores1 = null;
        for (BaseStores stores : recursion) {
            if(null == stores.getParentId()){
                baseStores1=stores;
            }
        }

        baseStore.setId(Long.valueOf(baseStores1.getId()));

        List<BaseStores> allRecursions = mapper.findAllRecursion(baseStore);
        List<BaseStores> baseStoress = new ArrayList<>();
        List<BaseStores> recursions = this.recursion(allRecursions,baseStoress);
        for (int i = 0; i < recursions.size(); i++) {
            if(storeRequest.getId().longValue() == recursions.get(i).getId().longValue()){
                recursions.remove(i);
                continue;
            }
        }
        //recursions.add(0,baseStores1);

        return recursions;
    }

    public List<BaseStores> pagesorganizationimZONDDIAN(StoreRequest storeRequest){
        BaseStore baseStore = new BaseStore();
        baseStore.setId(Long.valueOf(storeRequest.getId()));
        List<BaseStores> allRecursion = mapper.findAllRecursions(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursionparent(allRecursion,baseStores);
        BaseStores baseStores1 = null;
        for (BaseStores stores : recursion) {
            if(null == stores.getParentId()){
                baseStores1=stores;
            }
        }

        baseStore.setId(Long.valueOf(baseStores1.getId()));

        List<BaseStores> allRecursions = mapper.findAllRecursion(baseStore);
        List<BaseStores> baseStoress = new ArrayList<>();
        List<BaseStores> recursions = this.recursion(allRecursions,baseStoress);
        for (int i = 0; i < recursions.size(); i++) {
            if(storeRequest.getId().longValue() == recursions.get(i).getId().longValue()){
                recursions.remove(i);
                continue;
            }
        }
        recursions.add(0,baseStores1);

        return recursions;
    }

    /**
     * 查询当前门店所在组织下的所有门店
     */
    public List<BaseStores> pagesorganizations(StoreRequest storeRequest){
        if(storeRequest.getStoreName()!=null&&storeRequest.getStoreName().trim().length()>0){
            BaseStore baseStore = new BaseStore();
            baseStore.setStoreName(storeRequest.getStoreName().trim());
            return mapper.selectByStoreName(baseStore);
        }
        BaseStore baseStore = new BaseStore();
        baseStore.setId(storeRequest.getId());
        List<BaseStores> allRecursion = mapper.findAllRecursions(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursionparent(allRecursion,baseStores);
        BaseStores baseStores1 = null;
        for (BaseStores stores : recursion) {
            if(null == stores.getParentId()){
                baseStores1=stores;
            }
        }
        if(baseStores1!=null && null != baseStores1.getId()){
            baseStore.setId(Long.valueOf(baseStores1.getId()));
        }
        List<BaseStores> allRecursions = mapper.findAllRecursion(baseStore);
        List<BaseStores> baseStoress = new ArrayList<>();
        List<BaseStores> recursions = this.recursion(allRecursions,baseStoress);

        return recursions;
    }

    /**
     * 查询当前门店所在组织下的所有门店
     */
    public BaseStores pagesorganizationone(StoreRequest storeRequest){
        BaseStore baseStore = new BaseStore();
        baseStore.setId(Long.valueOf(storeRequest.getId()));
        List<BaseStores> allRecursion = mapper.findAllRecursions(baseStore);
        List<BaseStores> baseStores = new ArrayList<>();
        List<BaseStores> recursion = this.recursionparent(allRecursion,baseStores);
        BaseStores baseStores1 = null;
        for (BaseStores stores : recursion) {
            if(null == stores.getParentId()){
                baseStores1=stores;
            }
        }
        return baseStores1;
    }

    @Override
    public List<BaseStore> selects(BaseStore baseStore) {
        Example example = new Example(BaseStore.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobilePhone",baseStore.getRegistrantMobilePhone());
        if(StringUtils.isNotBlank(baseStore.getStoreName())){
            criteria.andLike("storeName","%" + baseStore.getStoreName()+ "%");
        }
        if (StringUtils.isNotBlank(String.valueOf(baseStore.getStoreNum()))){
            criteria.andEqualTo("storeNum",baseStore.getStoreNum());
        }
        List<BaseStore> baseStaffs = mapper.selectByExample(example);
        return baseStaffs;
    }

    //修改冲值表、疗程表的充值名称
    @Override
    public int uodateName(String memberName){
        List<String> tableName1=mapper.selectTableName1();
        List<String> tableName2=mapper.selectTableName2();
        List<String> tableName3=mapper.selectTableName3();
        List<String> tableName4=mapper.selectTableName4();
        int i=0;
        if(tableName1.size()>0){

        }
        if(tableName2.size()>0){

        }
        if(tableName3.size()>0){

        }
        if(tableName4.size()>0){

        }


        return i;
    }

    @Override
    public List<BaseStore> selectChildren(Long storeId) {
        return mapper.selectChildren(storeId);
    }

    @Override
    public List<BaseStores> selectStoreS(List<Long> storeIds) {
        return mapper.selectStoreS(storeIds);
    }

    @Override
    public List<BaseStore> selectByIds(List<Long> storeIds) {
        return mapper.selectByIds(storeIds);
    }

}
