package com.bjtdba.wares.service.impl;

import com.bjtdba.entity.wares.Merchant;
import com.bjtdba.entity.wares.MerchantCategory;
import com.bjtdba.utils.ResponseUtil;
import com.bjtdba.wares.dao.MerchantCategoryDao;
import com.bjtdba.wares.dao.MerchantDao;
import com.bjtdba.wares.dto.SearchCriteriaDto;
import com.bjtdba.wares.service.MerchantService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MerchantServiceImpl implements MerchantService {

    /**
     * 店铺
     */
    @Autowired
    private MerchantDao merchantDao;

    /**
     * 店铺分类
     */
    @Autowired
    private MerchantCategoryDao merchantCategoryDao;
    /**
     * 店铺分类检索
     */
    @Override
    public Object SelectMerchantSort() {
        Map<String, Object> data = new HashMap<>();
        //List<MerchantCategory> merchantCategoryList = merchantCategoryDao.selectMerchantSort();
        List<MerchantCategory> merchantCategoryList = SelectMerchantSortList();
        if(merchantCategoryList.size()==0){
            merchantCategoryList = SelectMerchantSortList();
        }
        data.put("merchantCategory",merchantCategoryList);
        return ResponseUtil.ok(data);
    }
    /**
     * 查询商户分类的方法
     */
    private List<MerchantCategory> SelectMerchantSortList(){
        List<MerchantCategory> merchantCategoryList = merchantCategoryDao.selectMerchantSort();
        return merchantCategoryList;
    }

    /**
     * 1、商户关键字模糊检索检索
     * 2、距离检索
     * 3、人气检索
     * 4、最新检索（新商铺）
     * 5、商户类型检索（推荐、大仓）
     * 6、店铺分类检索
     */
    @Override
    public Object selectMerchant(Integer pageNum, Integer pageSize, SearchCriteriaDto searchCriteriaDto) {
        Map<String, Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Merchant> merchantList=merchantDao.selectMerchant(searchCriteriaDto);


        PageInfo<Merchant> pageInfo = null;
        pageInfo = new PageInfo<Merchant>(merchantList);
        if(pageInfo.getPages()<pageNum){
            PageHelper.startPage(0,0);
            List<Merchant> order = new ArrayList<>();
            pageInfo = new PageInfo<>(order);
        }

        if(pageNum==1){
            if(searchCriteriaDto.getUid()!=null&&searchCriteriaDto.getUid()!=0){
                Merchant merchantObj= merchantDao.selectByUId(searchCriteriaDto);
                if(merchantObj!=null){
                    List<Merchant> merchantsInfo=new ArrayList<Merchant>();
                    merchantsInfo.add(merchantObj);
                    List<Merchant> merchantsListInfo= pageInfo.getList();
                    merchantsInfo.addAll(merchantsListInfo);
                    pageInfo.setList(merchantsInfo);
                }

            }
        }

        data.put("pageInfo",pageInfo);
        return ResponseUtil.ok(data);
    }



    /**
     * 增加rrx_merchant_intention 表关联  status 字段处理状态 1通过 ，2拒绝'  等于1
     * 1、商户关键字模糊检索检索
     * 2、距离检索
     * 3、人气检索
     * 4、最新检索（新商铺）
     * 5、商户类型检索（推荐、大仓）
     * 6、店铺分类检索
     */
    @Override
    public Object selectMerchantLeft(Integer pageNum, Integer pageSize, SearchCriteriaDto searchCriteriaDto) {
        Map<String, Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Merchant> merchantList=merchantDao.selectMerchantLeft(searchCriteriaDto);


        PageInfo<Merchant> pageInfo = null;
        pageInfo = new PageInfo<Merchant>(merchantList);
        if(pageInfo.getPages()<pageNum){
            PageHelper.startPage(0,0);
            List<Merchant> order = new ArrayList<>();
            pageInfo = new PageInfo<>(order);
        }

        if(pageNum==1){
            if(searchCriteriaDto.getUid()!=null&&searchCriteriaDto.getUid()!=0){
                Merchant merchantObj= merchantDao.selectByUId(searchCriteriaDto);
                if(merchantObj!=null){
                    List<Merchant> merchantsInfo=new ArrayList<Merchant>();
                    merchantsInfo.add(merchantObj);
                    List<Merchant> merchantsListInfo= pageInfo.getList();
                    merchantsInfo.addAll(merchantsListInfo);
                    pageInfo.setList(merchantsInfo);
                }

            }
        }

        data.put("pageInfo",pageInfo);
        return ResponseUtil.ok(data);
    }

}
