package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxMall;
import com.iformall.domain.po.WxMallBuilding;
import com.iformall.domain.po.WxMallFloor;
import com.iformall.mapper.WxMallBuildingMapper;
import com.iformall.mapper.WxMallFloorMapper;
import com.iformall.mapper.WxMallMapper;
import com.iformall.service.WxMallBuildingService;
import com.iformall.service.WxMallService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WxMallServiceImpl implements WxMallService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxMallMapper wxMallMapper;

    @Autowired
    WxMallBuildingMapper wxMallBuildingMapper;

    @Autowired
    WxMallFloorMapper wxMallFloorMapper;

    @Override
    public PageInfo<WxMall> listAsPage(WxMall record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxMallMapper.findList(record));
    }

    @Override
    public WxMall getById(Long id) {
        return wxMallMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(WxMall record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
			Long id = idWorker.nextId();
        	record.setId(id);
        	record.setTenantId(String.valueOf(id));
            wxMallMapper.insertSelective(record);
        } else {
            wxMallMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        wxMallMapper.deleteByPrimaryKey(id);
    }

    @Override
    public WxMall getByTenantId(String id) {
        WxMall wxMall = new WxMall();
        wxMall.setTenantId(id);
        return wxMallMapper.findList(wxMall).get(0);
    }

    @Override
    public WxMall getByTenantIdExt(String id) {
        WxMall wxMallParam = new WxMall();
        wxMallParam.setTenantId(id);
        WxMall wxMall = wxMallMapper.findList(wxMallParam).get(0);

        WxMallBuilding wxMallBuilding = new WxMallBuilding();
        wxMallBuilding.setTenantId(id);
        List<WxMallBuilding> buildings = wxMallBuildingMapper.findList(wxMallBuilding);
        List<WxMallBuilding> wxMallBuildings = buildings.stream().map(b -> {
            WxMallBuilding tempb = new WxMallBuilding();
            tempb.setId(b.getId());
            tempb.setName(b.getName());
            return tempb;
        }).collect(Collectors.toList());

        for(WxMallBuilding building:wxMallBuildings){
            WxMallFloor wxMallFloor = new WxMallFloor();
            wxMallFloor.setBuildingId(building.getId());
            List<WxMallFloor> wxMallFloors = wxMallFloorMapper.findList(wxMallFloor).stream().map(f -> {
                WxMallFloor tempf = new WxMallFloor();
                tempf.setId(f.getId());
                tempf.setFloorName(f.getFloorName());
                return tempf;
            }).collect(Collectors.toList());
            building.setFloors(wxMallFloors);

        }
        wxMall.setBuildings(wxMallBuildings);
        return wxMall;
    }


}
