package org.springblade.estate.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.dto.GuestRecordDTO;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.estate.entity.GuestRecord;
import org.springblade.estate.vo.GuestRecordVO;
import org.springblade.estate.wrapper.GuestRecordWrapper;
import org.springblade.estate.service.IGuestRecordService;
import org.springblade.core.mp.support.Query;

import java.util.List;

/**
 * client
 *
 * @author lianghanqiang
 * @since 2021-03-30
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/guest")

public class GuestRecordRecordClient implements IGuestRecordClient {

	private final IGuestRecordService guestService;

	/**
	 * 详情
	 */
	@Override
	@GetMapping("/detail")
	public GuestRecordVO detail(@SpringQueryMap GuestRecord guestRecord) {
		GuestRecord detail = guestService.getOne(Condition.getQueryWrapper(guestRecord));
		return GuestRecordWrapper.build().entityVO(detail);
	}

	/**
	 * 查询
	 */
	@Override
	@GetMapping("/list")
	public List<GuestRecordVO> list(@SpringQueryMap GuestRecord guestRecord) {
		return GuestRecordWrapper.build().listVO(guestService.list(Condition.getQueryWrapper(guestRecord)));
	}

	@Override
	@GetMapping("/count")
	public Integer count(@SpringQueryMap GuestRecord guestRecord) {
		return guestService.count(Condition.getQueryWrapper(guestRecord));
	}


	@Override
	@PostMapping("/page")
	public BladePage<GuestRecordVO> page(@RequestBody GuestRecordDTO guestRecordDTO) {
		GuestRecord guestRecord = BeanUtil.copyProperties(guestRecordDTO, GuestRecord.class);
		LambdaQueryWrapper<GuestRecord> queryWrapper = Condition.getQueryWrapper(guestRecord).lambda();
		if (guestRecordDTO.getUserIds() != null && guestRecordDTO.getUserIds().size() > 0) {
			queryWrapper.in(GuestRecord::getUserId, guestRecordDTO.getUserIds());
		}
		queryWrapper.orderByDesc(GuestRecord::getBeginTime, GuestRecord::getCreateTime);
		IPage<GuestRecord> pages = guestService.page(Condition.getPage(guestRecordDTO.getQuery()), queryWrapper);
		IPage<GuestRecordVO> guestRecordVOIPage = GuestRecordWrapper.build().pageVO(pages);
		return BladePage.of(guestRecordVOIPage);
	}


	/**
	 * 新增
	 */
	@Override
	@PostMapping("/save")
	public boolean save(@Valid @RequestBody GuestRecord guestRecord) {
		return guestService.save(guestRecord);
	}

	/**
	 * 修改
	 */
	@Override
	@PostMapping("/update")
	public boolean update(@Valid @RequestBody GuestRecord guestRecord) {
		return guestService.updateById(guestRecord);
	}

	/**
	 * 新增或修改
	 */
	@Override
	@PostMapping("/submit")
	public boolean submit(@Valid @RequestBody GuestRecord guestRecord) {
		return guestService.saveOrUpdate(guestRecord);
	}


	/**
	 * 删除
	 */
	@Override
	@PostMapping("/remove")
	public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return guestService.deleteLogic(Func.toLongList(ids));
	}

	@Override
	@GetMapping("/getById")
	public GuestRecord getById(@RequestParam Long id) {
		return guestService.getById(id);
	}

	@Override
	@PostMapping(value = "/audit")
	public R audit(@RequestBody GuestRecord guestRecord) {
		return guestService.audit(guestRecord.getId(), guestRecord.getAuditStatus(), guestRecord.getOpinion());
	}


}
