package com.jeequan.jeepay.mgr.ctrl.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.bizcommons.manage.MchStoreTerminalManage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchStoreTerminal;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MchStoreTerminalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/***
* 终端设备管理
*
* @author terrfly
*
* @date 2022/4/26 17:34
*/
@Slf4j
@RestController
@RequestMapping("/api/mchTerminals")
public class TerminalController extends CommonCtrl {

	@Autowired private MchStoreTerminalService mchStoreTerminalService;
	@Autowired private MchStoreTerminalManage mchStoreTerminalManage;

	@PreAuthorize("hasAuthority('ENT_TERMINAL_LIST')")
	@GetMapping
	public ApiRes pages() {

		// 请求参数
		MchStoreTerminal queryObject = getObject(MchStoreTerminal.class);
		IPage<MchStoreTerminal> page = mchStoreTerminalService.getMchStoreTerminalList(getIPage(), queryObject);
		return ApiRes.page(page);
	}

	/** detail */
	@PreAuthorize("hasAuthority( 'ENT_TERMINAL_VIEW' )")
	@RequestMapping(value="/{recordId}", method = RequestMethod.GET)
	public ApiRes detail(@PathVariable("recordId") String recordId) {
		return ApiRes.ok(mchStoreTerminalService.getById(recordId));
	}



	/** add */
	@PreAuthorize("hasAuthority( 'ENT_TERMINAL_ADD' )")
	@MethodLog(remark = "添加商户终端信息")
	@RequestMapping(value="", method = RequestMethod.POST)
	public ApiRes add() {
		return mchStoreTerminalManage.add(getObject(MchStoreTerminal.class));
	}


	/** update */
	@PreAuthorize("hasAuthority( 'ENT_TERMINAL_EDIT' )")
	@RequestMapping(value="/{recordId}", method = RequestMethod.PUT)
	@MethodLog(remark = "更新商户终端信息")
	public ApiRes update(@PathVariable("recordId") Long recordId) {

		MchStoreTerminal record = getObject(MchStoreTerminal.class);
		record.setTrmId(recordId);
		return mchStoreTerminalManage.update(record);
	}


	/** update */
	@PreAuthorize("hasAuthority( 'ENT_TERMINAL_EDIT' )")
	@RequestMapping(value="/trmDefaults", method = RequestMethod.PUT)
	@MethodLog(remark = "更新商户终端默认状态")
	public ApiRes updateDefault() {

		return mchStoreTerminalManage.updateDefault(getValLongRequired("trmId"), getValByteRequired("defaultFlag"));
	}


	/** delete */
	@PreAuthorize("hasAuthority('ENT_TERMINAL_DEL')")
	@MethodLog(remark = "删除终端")
	@RequestMapping(value="/{recordId}", method = RequestMethod.DELETE)
	public ApiRes del(@PathVariable("recordId") String recordId) {
		mchStoreTerminalService.removeById(recordId);
		return ApiRes.ok();
	}


	/** 报备列表 */
	@PreAuthorize("hasAuthority('ENT_TERMINAL_SENDUP')")
	@RequestMapping(value="channelBindInfos/{recordId}", method = RequestMethod.GET)
	public ApiRes channelBindInfos(@PathVariable("recordId") Long recordId) {

		return mchStoreTerminalManage.getChannelBindInfos(recordId);
	}


	/** 终端报备 */
	@PreAuthorize("hasAuthority('ENT_TERMINAL_SENDUP')")
	@MethodLog(remark = "终端报备")
	@RequestMapping(value="channelSendup/{recordId}", method = RequestMethod.POST)
	public ApiRes channelSendup(@PathVariable("recordId") Long recordId) {

		String ifCode = getValStringRequired("ifCode");
		Byte state = getValByteRequired("state"); // 0 - 取消报备， 1- 申请报备

		return mchStoreTerminalManage.channelSendup(recordId, ifCode, state);
	}


	/** 修改终端报备信息 */
	@PreAuthorize("hasAuthority('ENT_TERMINAL_SENDUP')")
	@MethodLog(remark = "自定义终端报备信息")
	@RequestMapping(value="channelBindInfos/{recordId}", method = RequestMethod.POST)
	public ApiRes updChannelBindInfos(@PathVariable("recordId") Long recordId) {


		String ifCode = getValStringRequired("ifCode");
		String channelTrmNo = getValStringDefault("channelTrmNo", ""); // 报备信息
		Byte state = getValByteRequired("state");

		return mchStoreTerminalManage.updChannelBindInfos(recordId, ifCode, channelTrmNo, state);
	}


}
