/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.tdcarrecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.parkinglot.entity.ParkingLot;
import org.springblade.community.parkinglot.service.IParkingLotService;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.tdcarrecord.dto.TdTdCarRecordDTO;
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.core.tool.utils.Func;
import org.springblade.smartapplication.carenterrecord.entity.CarEnterRecord;
import org.springblade.smartapplication.carenterrecord.service.ICarEnterRecordService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.community.tdcarrecord.entity.TdCarRecord;
import org.springblade.community.tdcarrecord.vo.TdCarRecordVO;
import org.springblade.community.tdcarrecord.wrapper.TdCarRecordWrapper;
import org.springblade.community.tdcarrecord.service.ITdCarRecordService;
import org.springblade.core.boot.ctrl.BladeController;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-06-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("community/tdcarrecord")
@Api(value = "", tags = "接口")
public class TdCarRecordController extends BladeController {

	private final ITdCarRecordService carRecordService;
	private final IParkingLotService parkingLotService;
	private final ICarEnterRecordService ankuaiCarEnterRecordService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入carRecord")
	public R<TdCarRecordVO> detail(TdCarRecord tdCarRecord) {
		TdCarRecord detail = carRecordService.getOne(Condition.getQueryWrapper(tdCarRecord));
		return R.data(TdCarRecordWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入carRecord")
	public R<IPage<TdCarRecordVO>> list(TdTdCarRecordDTO tdCarRecord, Query query, BladeUser bladeUser) {
		String tenantId = bladeUser.getTenantId();
		LambdaQueryWrapper<TdCarRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		ParkingLot parkingLot = null;
		if(tenantId.equals("000000")){
			if(tdCarRecord.getResidentialId() == null){
				return R.data(null);
			}
			parkingLot = parkingLotService.getByResidentialId(tdCarRecord.getResidentialId());
			Assert.notNull(parkingLot, "该小区下没有创建车场");
		}else{
			if(tdCarRecord.getAgencyId() == null){
				return R.data(null);
			}
			parkingLot = parkingLotService.getByAgencyId(tdCarRecord.getAgencyId());
			Assert.notNull(parkingLot, "该园区下没有创建车场");
		}
		if(tdCarRecord.getInOrOut() == 1){
			lambdaQueryWrapper.eq(TdCarRecord::getInMac, parkingLot.getParkKey());
		}else{
			lambdaQueryWrapper.eq(TdCarRecord::getOutMac, parkingLot.getParkKey());
		}
		lambdaQueryWrapper.orderByDesc(TdCarRecord::getCreateTime);
		IPage<TdCarRecord> pages = carRecordService.page(Condition.getPage(query), lambdaQueryWrapper);
		return R.data(TdCarRecordWrapper.build().pageVO(pages));
	}

	/**
	 * 分页
	 */
	@GetMapping("/screenGetRecord")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入carRecord")
	public R<IPage<TdCarRecordVO>> getRecord(TdTdCarRecordDTO tdCarRecord, Query query, BladeUser bladeUser) {
		String tenantId = bladeUser.getTenantId();
		LambdaQueryWrapper<TdCarRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		ParkingLot parkingLot = null;
		if(tenantId.equals("000000")){
			if(tdCarRecord.getResidentialId() == null){
				return R.data(null);
			}
			parkingLot = parkingLotService.getByResidentialId(tdCarRecord.getResidentialId());
			if(parkingLot == null){
				return R.data(new Page<TdCarRecordVO>());
			}
		}else if(tenantId.equals("804981")){
			LambdaQueryWrapper<CarEnterRecord> lambda = new LambdaQueryWrapper<>();
			lambda.orderByDesc(CarEnterRecord::getCreateTime);
			lambda.eq(CarEnterRecord::getTenantId, tenantId);
			IPage<CarEnterRecord> page = ankuaiCarEnterRecordService.page(Condition.getPage(query), lambda);
			IPage<TdCarRecord> pages = new Page<>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<TdCarRecord> list = new ArrayList();
			for(CarEnterRecord carEnterRecord : page.getRecords()){
				TdCarRecord tdCarRecord1 = new TdCarRecord();
				tdCarRecord1.setPlateNumber(carEnterRecord.getCarNo());
				tdCarRecord1.setEntrance(carEnterRecord.getEnterGateName());
				tdCarRecord1.setEnterTime(sdf.format(carEnterRecord.getEnterTime()));
				tdCarRecord1.setInMac("ak923784322134");
				tdCarRecord1.setEnterPic(carEnterRecord.getEnterImgPath());
				list.add(tdCarRecord1);
			}
			pages.setRecords(list);
			pages.setTotal(list.size());
			return R.data(TdCarRecordWrapper.build().pageVO(pages));
		} else{
			LambdaQueryWrapper<ParkingLot> parkLogWrapper = new LambdaQueryWrapper<>();
			parkLogWrapper.eq(ParkingLot::getTenantId, bladeUser.getTenantId());
			parkingLot = parkingLotService.getOne(parkLogWrapper);
			if(parkingLot == null){
				return R.data(new Page<TdCarRecordVO>());
			}
		}
		if(tdCarRecord.getInOrOut() != null){
			if(tdCarRecord.getInOrOut() == 1){
				lambdaQueryWrapper.eq(TdCarRecord::getInMac, parkingLot.getParkKey());
			}else{
				lambdaQueryWrapper.eq(TdCarRecord::getOutMac, parkingLot.getParkKey());
			}
		}
		lambdaQueryWrapper.orderByDesc(TdCarRecord::getCreateTime);
		IPage<TdCarRecord> pages = carRecordService.page(Condition.getPage(query), lambdaQueryWrapper);
		return R.data(TdCarRecordWrapper.build().pageVO(pages));
	}



	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入carRecord")
	public R<IPage<TdCarRecordVO>> page(TdCarRecordVO carRecord, Query query) {
		IPage<TdCarRecordVO> pages = carRecordService.selectCarRecordPage(Condition.getPage(query), carRecord);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入carRecord")
	public R save(@Valid @RequestBody TdCarRecord tdCarRecord) {
		return R.status(carRecordService.save(tdCarRecord));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入carRecord")
	public R update(@Valid @RequestBody TdCarRecord tdCarRecord) {
		return R.status(carRecordService.updateById(tdCarRecord));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入carRecord")
	public R submit(@Valid @RequestBody TdCarRecord tdCarRecord) {
		return R.status(carRecordService.saveOrUpdate(tdCarRecord));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(carRecordService.deleteLogic(Func.toLongList(ids)));
	}


}
