package org.springblade.modules.external.ycbigdata.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.community.accessrecord.entity.AccessRecord;
import org.springblade.community.accessrecord.service.IAccessRecordService;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.building.entity.Building;
import org.springblade.community.building.service.IBuildingService;
import org.springblade.community.floor.entity.Floor;
import org.springblade.community.floor.service.IFloorService;
import org.springblade.community.houseuser.entity.Houseuser;
import org.springblade.community.houseuser.service.IHouseuserService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.room.entity.Room;
import org.springblade.community.room.service.IRoomService;
import org.springblade.community.unit.entity.Unit;
import org.springblade.community.unit.service.IUnitService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.modules.external.ycbigdata.community.vo.*;
import org.springblade.modules.external.ycbigdata.community.wrapper.*;
import org.springblade.system.orgposition.service.IOrgPositionService;
import org.springblade.utils.QueryWrapperUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * Created By lidexi in 2021/3/16
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/bigdata/estate")
@Api(value = "社区管理-对外（银川大数据）接口", tags = "社区管理-对外（银川大数据）接口")
public class EstateExternalController {

	private IOrgPositionService orgPositionService;
	private IAgencyService agencyService;
	private IResidentialService residentialService;
	private IBuildingService buildingService;
	private IUnitService unitService;
	private IFloorService floorService;
	private IRoomService roomService;
	private IHouseuserService houseuserService;
	private IAccessRecordService accessRecordService;

	/**
	 * 获取社区列表
	 */
	@GetMapping("/agency/get")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取社区列表", notes = "获取社区列表")
	public R<IPage<AgencyVO>> agencyList(Agency agency,Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Agency> agencyLambdaQueryWrapper = new QueryWrapperUtil<Agency>().getBasicQueryWrapper(Agency::getOrgPosition, agency, bladeUser, orgPositionService);
		IPage<Agency> page = agencyService.page(Condition.getPage(query), agencyLambdaQueryWrapper.eq(Agency::getTenantId, bladeUser.getTenantId()));
		return R.data(AgencyWrapper.build().pageVO(page));
	}

	/**
	 * 获取小区列表
	 */
	@GetMapping("/residential/get")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取小区列表", notes = "获取小区列表")
	public R<IPage<ResidentialVO>> residentialList(Residential residential,Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Residential> residentialLambdaQueryWrapper = new QueryWrapperUtil<Residential>().getBasicQueryWrapper(Residential::getOrgPosition, residential, bladeUser, orgPositionService);
		IPage<Residential> page = residentialService.page(Condition.getPage(query), residentialLambdaQueryWrapper.eq(Residential::getTenantId, bladeUser.getTenantId()));
		return R.data(ResidentialWrapper.build().pageVO(page));
	}

	/**
	 * 获取楼栋列表
	 */
	@GetMapping("/building/get")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "获取楼栋列表", notes = "获取楼栋列表")
	public R<IPage<BuildingVO>> buildingList(Building building, Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Building> buildingLambdaQueryWrapper = new QueryWrapperUtil<Building>().getBasicQueryWrapper(Building::getOrgPosition, building, bladeUser, orgPositionService);
		IPage<Building> page = buildingService.page(Condition.getPage(query), buildingLambdaQueryWrapper.eq(Building::getTenantId, bladeUser.getTenantId()));
		return R.data(BuildingWrapper.build().pageVO(page));
	}

	/**
	 * 获取单元列表
	 */
	@GetMapping("/unit/get")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取单元列表", notes = "获取单元列表")
	public R<IPage<UnitVO>> unitList(Unit unit, Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Unit> unitLambdaQueryWrapper = new QueryWrapperUtil<Unit>().getBasicQueryWrapper(Unit::getOrgPosition, unit, bladeUser, orgPositionService);
		IPage<Unit> page = unitService.page(Condition.getPage(query), unitLambdaQueryWrapper.eq(Unit::getTenantId, bladeUser.getTenantId()));
		return R.data(UnitWrapper.build().pageVO(page));
	}

	/**
	 * 获取楼层列表
	 */
	@GetMapping("/floor/get")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "获取楼层列表", notes = "获取楼层列表")
	public R<IPage<FloorVO>> floorList(Floor floor, Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Floor> floorLambdaQueryWrapper = new QueryWrapperUtil<Floor>().getBasicQueryWrapper(Floor::getOrgPosition, floor, bladeUser, orgPositionService);
		IPage<Floor> page = floorService.page(Condition.getPage(query), floorLambdaQueryWrapper.eq(Floor::getTenantId, bladeUser.getTenantId()));
		return R.data(FloorWrapper.build().pageVO(page));
	}

	/**
	 * 获取房间列表
	 */
	@GetMapping("/room/get")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取房间列表", notes = "获取房间列表")
	public R<IPage<RoomVO>> roomList(Room room, Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new QueryWrapperUtil<Room>().getBasicQueryWrapper(Room::getOrgPosition, room, bladeUser, orgPositionService);
		IPage<Room> page = roomService.page(Condition.getPage(query), roomLambdaQueryWrapper.eq(Room::getTenantId, bladeUser.getTenantId()));
		return R.data(RoomWrapper.build().pageVO(page));
	}

	/**
	 * 获取住户列表
	 */
	@GetMapping("/houseuser/get")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "获取住户列表", notes = "获取住户列表")
	public R<IPage<HouseUserVO>> userList(Houseuser houseuser, Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<Houseuser> userLambdaQueryWrapper = new QueryWrapperUtil<Houseuser>().getBasicQueryWrapper(Houseuser::getOrgPosition, houseuser, bladeUser, orgPositionService);
		IPage<Houseuser> page = houseuserService.page(Condition.getPage(query), userLambdaQueryWrapper);
		return R.data(HouseUserWrapper.build().pageVO(page));
	}

	/**
	 * 获取住户出入记录列表
	 */
	@GetMapping("/accessrecord/get")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取住户出入记录列表", notes = "获取住户出入记录列表")
	public R<IPage<AccessRecordVO>> roomList(AccessRecord accessRecord, @RequestParam(value = "beginTime",required = false) String beginTime,@RequestParam(value = "endTime",required = false) String endTime, Query query, BladeUser bladeUser) {
		LambdaQueryWrapper<AccessRecord> accessRecordLambdaQueryWrapper = new QueryWrapperUtil<AccessRecord>().getBasicQueryWrapper(AccessRecord::getOrgPosition, accessRecord, bladeUser, orgPositionService);
		if(beginTime != null){
			accessRecordLambdaQueryWrapper.gt(AccessRecord::getOpenTime,beginTime);
		}
		if(endTime != null){
			accessRecordLambdaQueryWrapper.lt(AccessRecord::getOpenTime,endTime);
		}

		IPage<AccessRecord> page = accessRecordService.page(Condition.getPage(query), accessRecordLambdaQueryWrapper.eq(AccessRecord::getTenantId, bladeUser.getTenantId()).orderByDesc(AccessRecord::getOpenTime));
		return R.data(AccessRecordWrapper.build().pageVO(page));
	}
}
