package com.zhiqiyun.open.router.apis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dliyun.oap.framework.annotation.ServiceMethod;
import com.dliyun.oap.framework.annotation.ServiceMethodBean;
import com.dliyun.oap.framework.response.OapResponse;
import com.zhiqiyun.open.core.models.statistics.TravelReport;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.core.service.TravelReportService;
import com.zhiqiyun.open.router.request.sentiment.IdsRequest;
import com.zhiqiyun.open.router.request.statistics.QueryTravelReportRequest;
import com.zhiqiyun.open.router.request.statistics.SaveTravelReportRequest;
import com.zhiqiyun.open.router.request.statistics.UpdateTravelReportRequest;
import com.zhiqiyun.open.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@ServiceMethodBean
public class TravelReportApi {

	@Autowired
	private TravelReportService travelReportService;

	@Autowired
	private SequenceService sequenceService;

	@ServiceMethod(method = "travel.report.findPage", title = "查询招徕奖励申报")
	public OapResponse findPage(QueryTravelReportRequest request) {
		QueryWrapper<TravelReport> wrapper = new QueryWrapper<>();

		Page<TravelReport> page = new Page<>(request.getCurrent(), request.getPageSize());
		page.addOrder(OrderItem.desc("id"));

		Page<TravelReport> resultData = this.travelReportService.page(page, wrapper);

		return OapResponse.success().setBody(resultData);
	}

	@ServiceMethod(method = "travel.report.add", title = "新建招徕奖励申报")
	public OapResponse save(SaveTravelReportRequest request) {

		TravelReport entity = new TravelReport();
		BeanUtils.copyProperties(request, entity);

		entity.setCreateDate(DateUtil.current());
		entity.setId(this.sequenceService.nextId());

		this.travelReportService.save(entity);

		return OapResponse.success().setBody(entity);
	}

	@ServiceMethod(method = "travel.report.updateById", title = "更新招徕奖励申报")
	public OapResponse updateById(UpdateTravelReportRequest request) {
		TravelReport entity = new TravelReport();
		BeanUtils.copyProperties(request, entity);

		this.travelReportService.updateById(entity);

		return OapResponse.success().setBody(entity);
	}

	@ServiceMethod(method = "travel.report.deleteByIds", title = "删除招徕奖励申报")
	public OapResponse deleteByIds(IdsRequest request) {
		this.travelReportService.removeByIds(request.getIds());
		return OapResponse.success();
	}
}
