package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.common.ResultData;
import com.iformall.domain.dto.WxMallBuildingFloorDto;
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.service.WxMallBuildingService;
import com.iformall.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WxMallBuildingServiceImpl implements WxMallBuildingService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxMallBuildingMapper wxMallBuildingMapper;

    @Autowired
    WxMallFloorMapper wxMallFloorMapper;

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

    @Override
    public WxMallBuilding getById(Long id) {
        return wxMallBuildingMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(WxMallBuilding record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxMallBuildingMapper.insert(record);
        } else {
            wxMallBuildingMapper.updateById(record);
        }
    }

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

    @Override
    public ResultData getbuildinglist(String tenantId) {
        WxMallBuilding wxMallBuilding = new WxMallBuilding();
        wxMallBuilding.setTenantId(tenantId);
        List<WxMallBuilding> list = wxMallBuildingMapper.findList(wxMallBuilding);
        return new ResultData(list);
    }

    @Override
    public ResultData getbuildingfloorlist(String tenantId) {
        WxMallBuilding wxMallBuilding = new WxMallBuilding();
        wxMallBuilding.setTenantId(tenantId);
        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());
                if (f.getTotalArea() != null) {
                    tempf.setTotalArea(f.getTotalArea());
                }
                if (f.getOperatingArea() != null) {
                    tempf.setOperatingArea(f.getOperatingArea());
                }
                return tempf;
            }).collect(Collectors.toList());
            building.setFloors(wxMallFloors);

        }

        return new ResultData(wxMallBuildings);
    }

    @Override
    public void addBuildingAndFloors(WxMallBuildingFloorDto record) {
        final IdWorker idWorker = IdWorker.get();

        String [] floors = record.getFloors().split(",");

        // 楼座
        WxMallBuilding mallBuilding = new WxMallBuilding();
        mallBuilding.setId(idWorker.nextId());
        mallBuilding.setTenantId(record.getTenantId());
        mallBuilding.setName(record.getBuilding());
        mallBuilding.setFloorNumber(floors.length);
        wxMallBuildingMapper.insert(mallBuilding);

        // 楼层
        for(String floor: floors) {
            WxMallFloor mallFloor = new WxMallFloor();
            mallFloor.setId(idWorker.nextId());
            mallFloor.setTenantId(record.getTenantId());
            mallFloor.setBuildingId(mallBuilding.getId());
            mallFloor.setFloorName(floor);
            mallFloor.setBackgroundImg(Constant.floor_back_img);
            wxMallFloorMapper.insert(mallFloor);
        }
    }

    @Override
    public void saveFloorArea(WxMallFloor record) {
        WxMallFloor mallFloor = new WxMallFloor();
        mallFloor.setId(record.getId());
        mallFloor.setTenantId(record.getTenantId());
        if (record.getTotalArea() != null) {
            mallFloor.setTotalArea(record.getTotalArea());
        }
        if (record.getOperatingArea() != null) {
            mallFloor.setOperatingArea(record.getOperatingArea());
        }
        wxMallFloorMapper.updateById(mallFloor);
    }


}
