package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.TpRentalHouseImgMapper;
import com.community.huiju.dao.TpRentalHouseMapper;
import com.community.huiju.model.TpRentalHouse;
import com.community.huiju.model.TpRentalHouseImg;
import com.community.huiju.service.TpRentalHouseServicel;
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.*;

/**
 * version V1.0
 * class_name: $METHOD_NAME$
 * param: $METHOD_PARAM$
 * describe: TODO
 * creat_user:fannaixi
 * creat_time: 2019/5/9
 **/
@Service("tpRentalHouseServicel")
public class TpRentalHouseServicelImpl implements TpRentalHouseServicel {
    @Autowired
    private TpRentalHouseMapper tpRentalHouseMapper;

    @Autowired
    private TpRentalHouseImgMapper tpRentalHouseImgMapper;
    @Override
    public ResponseBean getRentalHouse(UserElement userElement, String houseName, Integer rentalType, Integer houseType, Integer rentalPrice, Integer priceFluctuate,Integer pageNum,Integer pageSize) {
        ResponseBean response= new ResponseBean<>();
        Map<String, Object> parameter = Maps.newHashMap();
        //使用分页插件
        Page<TpRentalHouse> pageTpRentalHouseList = PageHelper.startPage(pageNum, pageSize);
        List<TpRentalHouse> tpRentalHouseList= tpRentalHouseMapper.getRentalHouse(houseName,rentalType,houseType,rentalPrice,priceFluctuate,userElement.getCommunityId());
            // 把字符串转数组
        for (TpRentalHouse tpRentalHouse:tpRentalHouseList){
            String houseLabel= tpRentalHouse.getHouseLabel();
            String houseLabelarr= houseLabel.replace("，", ",");
            String [] result = houseLabelarr.split(",");
            if (result[0].equals("")){
                tpRentalHouse.setHouseLabelArr(null);
            }else {
                tpRentalHouse.setHouseLabelArr(Arrays.asList(result));
            }
            // 根据 tpRentalHouse.getId()和img_type为2查询列表图
            List<TpRentalHouseImg> tpRentalHouseImgList= tpRentalHouseImgMapper.selectRentalHouseImg(tpRentalHouse.getId(),1);
             if (null != tpRentalHouseImgList){
                 for (TpRentalHouseImg tpRentalHouseImg:tpRentalHouseImgList) {
                     tpRentalHouse.setHomeImg(tpRentalHouseImg.getImgUrl());
                 }
             }
        }
        parameter.put("total", pageTpRentalHouseList.getTotal());
        parameter.put("list", tpRentalHouseList);
        response.addSuccess(parameter);
        return response;
    }

    @Override
    public ResponseBean  getRentalHouseDetails(UserElement userElement, String id) {
        ResponseBean response= new ResponseBean<>();
        TpRentalHouse tpRentalHouse = tpRentalHouseMapper.selectByPrimaryKey(Integer.valueOf(id));
        List<TpRentalHouseImg> tpRentalHouseImgList= tpRentalHouseImgMapper.selectRentalHouseImg(tpRentalHouse.getId(),2);

            Map map= new HashMap<>();
            List<String> img= new ArrayList<>();
            if (null != tpRentalHouseImgList){
                for (TpRentalHouseImg tpRentalHouseImg:tpRentalHouseImgList) {
                    img.add(tpRentalHouseImg.getImgUrl());
                }
            }
            String houseLabel= tpRentalHouse.getHouseLabel();
            String houseLabelarr= houseLabel.replace("，", ",");
            String [] result = houseLabelarr.split(",");
            if (result[0].equals("")){
                tpRentalHouse.setHouseLabelArr(null);
            }else {
                tpRentalHouse.setHouseLabelArr(Arrays.asList(result));
            }


            map.put("tpRentalHouse",tpRentalHouse);
            map.put("bannerImg",img);
            response.addSuccess(map);
            return response;
    }

    @Override
    public ResponseBean getRentalHouseDictionaries() {
    ResponseBean responseBean= new ResponseBean<>();
        List<Object> object= new ArrayList<>();
        Map<Object,Object> map= new  HashMap<>();
        // 整租，合租数据组装
        map.put("rentalType","整租");
        map.put("rentalTypeValue","1");
        map.put("houseType","一居室");
        map.put("houseTypeValue","1");
        map.put("rentalPrice","1000以内");
        map.put("rentalPriceValue","1");
        map.put("priceFluctuate","价格由低到高");
        map.put("priceFluctuateValue","1");

        Map<Object,Object> mapTwo= new  HashMap<>();
        mapTwo.put("rentalType","合租");
        mapTwo.put("rentalTypeValue","2");
        mapTwo.put("houseType","二居室");
        mapTwo.put("houseTypeValue","2");
        mapTwo.put("rentalPrice","1000～1500");
        mapTwo.put("rentalPriceValue","2");
        mapTwo.put("priceFluctuate","价格由高到低");
        mapTwo.put("priceFluctuateValue","2");

        Map<Object,Object> maptTree= new  HashMap<>();
        maptTree.put("rentalType","不限");
        maptTree.put("rentalTypeValue","3");
        maptTree.put("houseType","三居室");
        maptTree.put("houseTypeValue","3");
        maptTree.put("rentalPrice","1500～2000");
        maptTree.put("rentalPriceValue","3");
        maptTree.put("priceFluctuate","默认排序");
        maptTree.put("priceFluctuateValue","3");

        Map<Object,Object> mapfour= new  HashMap<>();
        mapfour.put("houseType","四居室及以上");
        mapfour.put("houseTypeValue","4");
        mapfour.put("rentalPrice","2000～3000");
        mapfour.put("rentalPriceValue","4");

        Map<Object,Object> mapfive= new  HashMap<>();
        mapfive.put("rentalPrice","3000～4000");
        mapfive.put("rentalPriceValue","5");
        mapfive.put("houseType","不限");
        mapfive.put("houseTypeValue","5");

        Map<Object,Object> mapSix= new  HashMap<>();
        mapSix.put("rentalPrice","4000以上");
        mapSix.put("rentalPriceValue","6");

        Map<Object,Object> mapSeven= new  HashMap<>();
        mapSeven.put("rentalPrice","不限");
        mapSeven.put("rentalPriceValue","7");

        object.add(map);
        object.add(mapTwo);
        object.add(maptTree);
        object.add(mapfour);
        object.add(mapfive);
        object.add(mapSix);
        object.add(mapSeven);
        responseBean.addSuccess(object);
        return responseBean;
    }
}
