package org.springblade.app.controller;

import lombok.RequiredArgsConstructor;
import org.springblade.community.feign.IAgencyClient;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.estate.dto.GuestRecordDTO;
import org.springblade.estate.entity.GuestRecord;
import org.springblade.estate.feign.IGuestRecordClient;
import org.springblade.estate.vo.GuestRecordVO;
import org.springblade.system.expand.feign.IConfClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/guestrecord")
public class GuestRecordController {

	private final IGuestRecordClient guestRecordClient;
	private final IConfClient confClient;

	@GetMapping(value = "/page")
	public R<BladePage<GuestRecordVO>> getPage(GuestRecord guestRecord, Query query) {
		GuestRecordDTO dto = BeanUtil.copy(guestRecord, GuestRecordDTO.class);
		dto.setQuery(query);
		BladePage<GuestRecordVO> page = guestRecordClient.page(dto);
		return R.data(page);
	}

	@GetMapping(value = "/detail")
	public R<GuestRecordVO> getOne(GuestRecord guestRecord){
		if(guestRecord == null){
			return R.fail("参数不能为空");
		}
		GuestRecordVO detail = guestRecordClient.detail(guestRecord);
		return R.data(detail);
	}

	@PostMapping(value = "/submit")
	public R submit(@RequestBody GuestRecord guestRecord){
		guestRecord.setRegisterSource(1);
		Long id = guestRecord.getId();
		Date endTime = guestRecord.getEndTime();
		if(endTime == null){
			guestRecord.setBeginTime(DateUtil.parse(guestRecord.getInterviewTime() + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
			guestRecord.setEndTime(DateUtil.parse(guestRecord.getInterviewTime() + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		}
		if(id == null){
			String imageUri = guestRecord.getFaceUrl();
			if(StringUtil.isNotBlank(imageUri)){
				String path = confClient.removePrefix(imageUri);
				guestRecord.setFaceUrl(path);
			}
			return R.status(guestRecordClient.save(guestRecord));
		}else{
			GuestRecord dbGuestRecord = guestRecordClient.getById(id);
			Assert.notNull(dbGuestRecord.getAuditStatus(), "审核状态异常");
			if(dbGuestRecord.getAuditStatus() == 1){
				return R.fail("访客记录已审核通过，请勿修改");
			}else if(dbGuestRecord.getAuditStatus() == 2){
				return R.fail("请勿修改未审核通过记录");
			}else{
				return R.status(guestRecordClient.update(guestRecord));
			}
		}
	}

	@PostMapping(value = "/delete")
	public R delete(@RequestParam String ids){
		List<Long> idList = Func.toLongList(ids);
		for(Long id : idList){
			GuestRecord guestRecord = guestRecordClient.getById(id);
			if(guestRecord.getAuditStatus() != null && guestRecord.getAuditStatus() == 0){
				guestRecordClient.remove(id.toString());
			}else{
				return R.fail("请勿删除已审核记录");
			}
		}
		return R.status(true);
	}

	@PostMapping(value = "/audit")
	public R audit(@RequestBody GuestRecord guestRecord){
		Assert.notNull(guestRecord.getAuditStatus(), "审核状态必传");
		GuestRecord dbGuestRecord = guestRecordClient.getById(guestRecord.getId());
		Assert.notNull(dbGuestRecord, "访客记录不存在");
		Assert.isTrue(dbGuestRecord.getAuditStatus() != 1, "访客记录已审核通过");
		Assert.isTrue(dbGuestRecord.getAuditStatus() != 2, "访客记录已审核不通过");
		return guestRecordClient.audit(guestRecord);
	}
}
