package com.jeequan.jeepay.mch.ctrl.division;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.DivisionTask;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 分账任务控制器
 */
@RestController
@RequestMapping("/api/divisionTask")
public class DivisionTaskController extends CommonCtrl {

	@Autowired private DivisionTaskService divisionTaskService;

	/** list */
	@GetMapping
	public ApiRes list() {
		DivisionTask queryObject = getObject(DivisionTask.class);
		queryObject.setMchNo(getCurrentMchNo());
		IPage<DivisionTask> pages = divisionTaskService.pageList(getIPage(true), queryObject);
		return ApiRes.page(pages);
	}

	/** add */
//	@PreAuthorize("hasAuthority( 'ENT_DIVISION_TASK_ADD' )")
	@RequestMapping(method = RequestMethod.POST)
	@MethodLog(remark = "新增分账任务")
	public ApiRes add() {
		DivisionTask task = getObject(DivisionTask.class).setMchNo(getCurrentMchNo());
		divisionTaskService.addSave(task);
		return ApiRes.ok();
	}

	/** update */
	@PreAuthorize("hasAuthority( 'ENT_DIVISION_TASK_EDIT' )")
	@RequestMapping(value="/{taskId}", method = RequestMethod.PUT)
	@MethodLog(remark = "更新分账任务")
	public ApiRes update(@PathVariable("taskId") String taskId) {
		DivisionTask update = getObject(DivisionTask.class).setTaskId(taskId);
		DivisionTask ext = divisionTaskService.getById(taskId);
		Assert.notNull(ext,"非法参数");
		divisionTaskService.updateById(update);
		return ApiRes.ok();
	}

	/** delete */
	@PreAuthorize("hasAuthority('ENT_DIVISION_TASK_DELETE')")
	@RequestMapping(value="/{taskId}", method = RequestMethod.DELETE)
	@MethodLog(remark = "删除分账任务")
	public ApiRes del(@PathVariable("taskId") String taskId) {
		divisionTaskService.removeById(taskId);
		return ApiRes.ok();
	}

	/**
	 * 获取配置的分账对象
	 * @param taskId
	 * @return
	 */
//	@PreAuthorize("hasAuthority('ENT_DIVISION_TASK_DETAIL')")
	@RequestMapping(value="/subject/{taskId}", method = RequestMethod.GET)
	@MethodLog(remark = "获取分账对象")
	public ApiRes getSubject(@PathVariable("taskId") String taskId) {
		DivisionTask result = divisionTaskService.getDivisionSubject(taskId);
		return ApiRes.ok(result);
	}


	/**
	 * 新增绑定
	 * @return
	 */
//	@PreAuthorize("hasAuthority('ENT_DIVISION_TASK_BIND')")
//	@RequestMapping(value="/bind", method = RequestMethod.POST)
//	@MethodLog(remark = "分账关系绑定接口")
//	public ApiRes saveBindSubject() {
//		DivisionTask task = getObject(DivisionTask.class);
//		divisionTaskService.saveBindSubject(task);
//		return ApiRes.ok();
//	}
}
