package com.community.huiju.service.impl;

import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.TpShopImgMapper;
import com.community.huiju.dao.TpShopMapper;
import com.community.huiju.dao.TpShopTypeMapper;
import com.community.huiju.model.TpAnnouncement;
import com.community.huiju.model.TpShop;
import com.community.huiju.model.TpShopImg;
import com.community.huiju.model.TpShopType;
import com.community.huiju.service.TpShopServicel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
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;

/**
 * version V1.0
 * class_name: $METHOD_NAME$
 * param: $METHOD_PARAM$
 * describe: TODO
 * creat_user:fannaixi
 * creat_time: 2019/5/10
 **/
@Service("tpShopServicel")
public class TpShopServicelmpl implements TpShopServicel {
    @Autowired
    private TpShopMapper tpShopMapper;

    @Autowired
    private TpShopTypeMapper tpShopTypeMapper;

    @Autowired
    private TpShopImgMapper tpShopImgMapper;
    @Override
    public ResponseBean getShop(UserElement userElement,String houseName,Integer tpShopTypeId,Integer pageNum,Integer pageSize) {
        ResponseBean responseBean= new ResponseBean<>();
        Map<String, Object> parameter = Maps.newHashMap();
        //使用分页插件
        Page<TpShop> pageShopList = PageHelper.startPage(pageNum, pageSize);
        List<TpShop> tpShopList= tpShopMapper.selectcommunityIdShop(userElement.getCommunityId(),houseName,tpShopTypeId);
        if (null != tpShopList) {
            for (TpShop tpShop : tpShopList) {
                // 查询当列表图片 2 为aap列表图
                List<TpShopImg> tpShopImgList = tpShopImgMapper.selectShopIdCommunity(userElement.getCommunityId(), tpShop.getId(),2);
                for (TpShopImg tpShopImg:tpShopImgList) {
                    tpShop.setShopImg(tpShopImg.getImgUrl());
                }
            }
        }
        List<TpShopType> tpShopTypeList = tpShopTypeMapper.selectCommunityIdShopAll(userElement.getCommunityId());
        parameter.put("total", pageShopList.getTotal());
        parameter.put("list", tpShopList);
        parameter.put("tpShopTypeList",tpShopTypeList);
        responseBean.addSuccess(parameter);
        return responseBean;
    }

    @Override
    public ResponseBean getShopDetails(UserElement userElement, Integer id) {
        ResponseBean response= new ResponseBean<>();
        TpShop tpShop = tpShopMapper.selectByPrimaryKey(id);
        // 3是app商铺轮播图
        HashMap hashMap= new HashMap<>();
        List<TpShopImg> tpShopImg= tpShopImgMapper.selectShopIdCommunity(userElement.getCommunityId(),id,3);
        hashMap.put("tpShop",tpShop);
        hashMap.put("tpShopImg",tpShopImg);

        response.addSuccess(hashMap);
        return response;
    }

    @Override
    public ResponseBean getShopCommunity(UserElement userElement) {
        ResponseBean response = new ResponseBean<>();
        // 当前线下商铺是否展示
        String shopSetting= tpShopMapper.shopSetting(userElement.getCommunityId());
        List<TpShop> tpShopList = new ArrayList<>();
        List<TpShopType> tpShopTypeList =new ArrayList<>();
        if (null != shopSetting) {
            tpShopList = tpShopMapper.selectShopCommunity(userElement.getCommunityId());
            for (TpShop tpShop : tpShopList) {
                // 1 表示查询当前首页图片
                List<TpShopImg> tpShopImgList = tpShopImgMapper.selectShopIdCommunity(userElement.getCommunityId(), tpShop.getId(), 1);
                if(tpShopImgList.size()>0) {
                    TpShopImg tpShopImg = tpShopImgList.get(0);
                    tpShop.setTpShopImgUrl(tpShopImg.getImgUrl());
                }
            }
            // 查询当前商铺类型
            tpShopTypeList = tpShopTypeMapper.selectCommunityIdShop(userElement.getCommunityId());
           for (TpShopType tpShopType:tpShopTypeList){
               int a=tpShopType.getSort().intValue();
             if (a == 1){
                 //美食图片
                 List<String> tpShopTypeImg = new ArrayList<>();
                 // 美食图片3x
                 tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1557816913843.png?Expires=1559644301&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                 // 美食图片2x
                 tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1557816963921.png?Expires=1559644351&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                 tpShopType.setImg(tpShopTypeImg);
             }
            if (a == 2){
                //购物
                List<String> tpShopTypeImg = new ArrayList<>();
                //购物车3x
                tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1557816503894.png?Expires=1559643891&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                //购物车2x
                tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1557816639291.png?Expires=1559644026&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                tpShopType.setImg(tpShopTypeImg);
            }
            if (a == 3){
                   //休闲
                   List<String> tpShopTypeImg = new ArrayList<>();
                   // 休闲3x
                tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1558077862814.png?Expires=1559905250&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                   // 休闲2x
                tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1558077918669.png?Expires=1559905306&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                tpShopType.setImg(tpShopTypeImg);
               }
               if (a == 4){
                   // 运动图片
                   List<String> tpShopTypeImg = new ArrayList<>();
                   // 运动图片3x
                   tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1557817239384.png?Expires=1559644626&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                   // 运动图片2x
                   tpShopTypeImg.add("http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1557817273156.png?Expires=1559644660&OSSAccessKeyId=LTAIkc75dpkJw8Lb");
                   tpShopType.setImg(tpShopTypeImg);
               }

           }

        }
        Map<String,Object> hashMap= new HashMap<>();
        hashMap.put("tpShopType",tpShopTypeList);
        hashMap.put("tpShopList",tpShopList);
        response.addSuccess(hashMap);
        return response;

    }
}
