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(value="", method = RequestMethod.POST)
	@MethodLog(remark = "新增分账任务")
	public ApiRes add() {
		DivisionTask template = getObject(DivisionTask.class);
		divisionTaskService.addSave(template,getCurrentMchNo());
		return ApiRes.ok();
	}

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

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

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


	/**
	 * 新增配置
	 * @return
	 */
	@PreAuthorize("hasAuthority('ENT_DIVISION_SUBJECT_ADD')")
	@RequestMapping(value="/saveSubject", method = RequestMethod.POST)
	@MethodLog(remark = "新增分账任务配置")
	public ApiRes saveSubject() {
		DivisionTask template = getObject(DivisionTask.class);
		divisionTaskService.saveConfig(template);
		return ApiRes.ok();
	}
}
