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

import cn.hutool.core.collection.CollectionUtil;
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.MchAppEntity;
import com.jeequan.jeepay.db.entity.MchStoreTerminal;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MchAppService;
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.*;

import java.util.List;

/***
* 终端设备管理
*
* @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;
	@Autowired private MchAppService mchAppService;

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

		// 请求参数
		MchStoreTerminal queryObject = getObject(MchStoreTerminal.class);
		queryObject.setMchNo(getCurrentMchNo()); //查询当前商户的信息

		LambdaQueryWrapper<MchStoreTerminal> gw = MchStoreTerminal.gw();

		gw.eq(StringUtils.isNotEmpty(queryObject.getAgentNo()), MchStoreTerminal::getAgentNo, queryObject.getAgentNo());
		gw.eq(StringUtils.isNotEmpty(queryObject.getMchNo()), MchStoreTerminal::getMchNo, queryObject.getMchNo());
		gw.eq(queryObject.getStoreId() != null, MchStoreTerminal::getStoreId, queryObject.getStoreId());
		gw.like(StringUtils.isNotEmpty(queryObject.getTrmNo()), MchStoreTerminal::getTrmNo, queryObject.getTrmNo());
		gw.like(StringUtils.isNotEmpty(queryObject.getTrmName()), MchStoreTerminal::getTrmName, queryObject.getTrmName());
		gw.eq(queryObject.getTrmType() != null, MchStoreTerminal::getTrmType, queryObject.getTrmType());
		gw.eq(queryObject.getState() != null, MchStoreTerminal::getState, queryObject.getState());

		// 商户号 / 设备名称 / 设备编号
		if(StringUtils.isNotEmpty(getValString("unionSearchId"))){
			gw.and(wr ->
					wr.like(MchStoreTerminal::getMchNo, getValString("unionSearchId"))
							.or().like(MchStoreTerminal::getTrmName, getValString("unionSearchId"))
							.or().like(MchStoreTerminal::getTrmNo, getValString("unionSearchId"))
			);
		}

		// 门店条件
		List<String> storeIdList = getCurrentUser().getStoreIdList();
		if (CollectionUtil.isNotEmpty(storeIdList)) {
			gw.in(MchStoreTerminal::getStoreId, storeIdList);
		}

		gw.orderByDesc(MchStoreTerminal::getCreatedAt);


		//返回数据
		IPage<MchStoreTerminal> page = mchStoreTerminalService.page(getIPage(), gw);
		return ApiRes.page(page);
	}

	/** detail */
	@PreAuthorize("hasAuthority( 'ENT_TERMINAL_VIEW' )")
	@RequestMapping(value="/{recordId}", method = RequestMethod.GET)
	public ApiRes detail(@PathVariable("recordId") String recordId) {
		MchStoreTerminal terminal = mchStoreTerminalService.getById(recordId);
		MchAppEntity mchAppEntity = mchAppService.getById(terminal.getAppId());
		terminal.addExt("appName", mchAppEntity.getAppName());
		return ApiRes.ok(terminal);
	}



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

		MchStoreTerminal mchStoreTerminal = getObject(MchStoreTerminal.class);
		mchStoreTerminal.setMchNo(getCurrentMchNo());

		// 商户的默认门店
		if(StringUtils.isEmpty(mchStoreTerminal.getAppId())){
			mchStoreTerminal.setAppId(mchAppService.getDefaultApp(getCurrentMchNo()).getAppId());
		}

		return mchStoreTerminalManage.add(mchStoreTerminal);
	}


	/** 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);
	}


}
