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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.device.IPrinterService;
import com.jeequan.jeepay.core.interfaces.device.ISpeakerService;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.core.utils.DateKit;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.db.entity.*;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.mch.service.MarketAdvertService;
import com.jeequan.jeepay.service.impl.*;
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.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备厂商配置管理类
 *
 * @author zx
 *
 * @date 2021-12-28 09:15
 */
@Slf4j
@RestController
@RequestMapping("/api/store/device")
public class MchStoreDeviceController extends CommonCtrl {

	@Autowired private DeviceProvideConfigService deviceProvideConfigService;
	@Autowired private MchStoreDeviceService mchStoreDeviceService;
	@Autowired private MchInfoService mchInfoService;
	@Autowired private MchAppService mchAppService;
	@Autowired private MchStoreService mchStoreService;
	@Autowired private MchQrcDeviceRelaService mchQrcDeviceRelaService;
	@Autowired private MchQrcodeCardService mchQrcodeCardService;
	@Autowired private MarketAdvertService marketAdvertService;
	@Autowired private MchApplymentService mchApplymentService;

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 门店设备列表
	 */
	@PreAuthorize("hasAnyAuthority('ENT_DEVICE_SPEAKER_LIST', 'ENT_DEVICE_PRINTER_LIST', 'ENT_DEVICE_POS_LIST', 'ENT_DEVICE_PLUGIN_CDKEY_LIST'," +
			"'ENT_DEVICE_AUTO_POS_LIST', 'ENT_DEVICE_FACE_APP_LIST', 'ENT_DEVICE_RUYI_LIST')")
	@GetMapping
	public ApiRes pages() {
		MchStoreDevice mchStoreDevice = getObject(MchStoreDevice.class);
		mchStoreDevice.setMchNo(getCurrentMchNo());
//		mchStoreDevice.setBindState(CS.YES);

		mchStoreDevice.addExt("appSearchData", getValString("appSearchData"));
		// 门店条件
		List<String> storeIdList = getCurrentUser().getStoreIdList();
		if (CollectionUtil.isNotEmpty(storeIdList)) {
			mchStoreDevice.setStoreIdList(storeIdList);
		}
		IPage<MchStoreDevice> page = mchStoreDeviceService.selectPage(getIPage(), mchStoreDevice);
		//返回数据
		return ApiRes.page(page);
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 检查设备是否允许绑定
	 */
	@PreAuthorize("hasAnyAuthority('ENT_DEVICE_SPEAKER_ADD', 'ENT_DEVICE_PRINTER_ADD', 'ENT_DEVICE_POS_ADD', 'ENT_DEVICE_AUTO_POS_ADD'," +
			"'ENT_DEVICE_FACE_APP_ADD', 'ENT_DEVICE_RUYI_ADD')")
	@MethodLog(remark = "检查设备是否允许绑定")
	@PostMapping("/check/{deviceNo}")
	public ApiRes getProvider(@PathVariable("deviceNo") String deviceNo) {

		Byte deviceType = getValByteRequired("deviceType"); // 1-喇叭  2-打印机

		// 校验是否可绑定，并返回可绑定设备ID
		JSONObject checkJSON = checkDeviceBindInfo(deviceNo, null, deviceType);

		return ApiRes.ok(checkJSON);
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 新增门店设备
	 */
	@PreAuthorize("hasAnyAuthority('ENT_DEVICE_SPEAKER_ADD', 'ENT_DEVICE_PRINTER_ADD', 'ENT_DEVICE_POS_ADD', 'ENT_DEVICE_AUTO_POS_ADD'," +
			"'ENT_DEVICE_FACE_APP_ADD', 'ENT_DEVICE_RUYI_ADD')")
	@MethodLog(remark = "新增门店设备")
	@PostMapping
	public ApiRes add() {

		MchStoreDevice mchStoreDevice = getObject(MchStoreDevice.class);
		Byte deviceType = getValByteRequired("deviceType");

		MchInfo mchInfo = mchInfoService.getById(getCurrentMchNo());

		// 校验是否可绑定，并返回可绑定设备ID
		JSONObject checkJSON = checkDeviceBindInfo(mchStoreDevice.getDeviceNo(), mchStoreDevice.getProvider(), deviceType);
		if (checkJSON.getInteger("checkCode") != 1) {
			return ApiRes.customFail(checkJSON.getString("checkMsg"));
		}

		Long deviceId = checkJSON.getLong("deviceId");

		// 扫码POS需绑定商户应用，未传入商户appId 则使用默认应用
		String appId = mchStoreDevice.getAppId();
		if (deviceType == MchStoreDevice.DEVICE_TYPE_POS && StringUtils.isEmpty(appId)) {
			appId = mchAppService.getDefaultApp(mchInfo.getMchNo()).getAppId();
		}

		MchStoreDevice updateRecord = new MchStoreDevice();
		updateRecord.setDeviceId(deviceId);
		updateRecord.setDeviceName(mchStoreDevice.getDeviceName());
		updateRecord.setBindState(CS.YES);
		updateRecord.setMchNo(mchInfo.getMchNo());
		updateRecord.setAppId(appId);
		updateRecord.setStoreId(mchStoreDevice.getStoreId());
		updateRecord.setBindType(mchStoreDevice.getBindType());
		updateRecord.setAgentNo(mchInfo.getAgentNo());
		//获取商户信息
		MchStore mchStore = mchStoreService.getById(mchStoreDevice.getStoreId());
		if(mchStore == null){
			throw new BizException("门店不存在！");
		}
		if (StringUtils.isNotEmpty(mchStore.getMchApplyId())){
			MchApplyment applyment = mchApplymentService.getById(mchStore.getMchApplyId());
			if (applyment != null){
				updateRecord.setMchApplyName(applyment.getMchShortName());
				updateRecord.setMchApplyId(mchStore.getMchApplyId());
			}
		}

		// 云喇叭  同步绑定码牌
		if (deviceType == MchStoreDevice.DEVICE_TYPE_SPEAKER) {

			String qrcIdListStr = getValString("qrcIdList");

			// 绑定类型 为码牌时，需传入绑定的码牌id
			List<Long> qrcList = new LinkedList<>();
			if (null != mchStoreDevice.getBindType() && mchStoreDevice.getBindType() == MchStoreDevice.DEVICE_BIND_TYPE_QRC && StringUtils.isNotBlank(qrcIdListStr)) {
				qrcList = JSONArray.parseArray(qrcIdListStr, Long.class);
			}

			Long bindQrcId = checkJSON.getLong("bindQrcId"); // 初始绑定的码牌ID
			boolean result = mchStoreDeviceService.bindSpeakerDevice(updateRecord, bindQrcId, qrcList);
			if (!result) {
				return ApiRes.customFail("绑定失败");
			}
			return ApiRes.ok();
		}

		// 云打印、扫码pos执行更新
		if (mchStoreDeviceService.updateById(updateRecord)) {
			return ApiRes.ok();
		}
		return ApiRes.customFail("绑定失败");
	}


	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 门店设备修改
	 */
	@PreAuthorize("hasAnyAuthority('ENT_DEVICE_SPEAKER_EDIT', 'ENT_DEVICE_PRINTER_EDIT', 'ENT_DEVICE_POS_EDIT', 'ENT_DEVICE_PLUGIN_CDKEY_EDIT'," +
			"'ENT_DEVICE_AUTO_POS_EDIT', 'ENT_DEVICE_FACE_APP_EDIT', 'ENT_DEVICE_RUYI_EDIT')")
	@MethodLog(remark = "门店设备修改")
	@PutMapping("/{deviceId}")
	public ApiRes update(@PathVariable("deviceId") Long deviceId) {
		MchStoreDevice mchStoreDevice = getObject(MchStoreDevice.class);
		mchStoreDevice.setDeviceId(deviceId);

		// 不能修改项
		mchStoreDevice.setDeviceNo(null);
		mchStoreDevice.setDeviceParams(null);
		mchStoreDevice.setBatchId(null);
		mchStoreDevice.setDeviceType(null);
		mchStoreDevice.setCreatedAt(null);
		mchStoreDevice.setUpdatedAt(null);

		MchStoreDevice dbRecord = mchStoreDeviceService.getById(deviceId);
		if (!getCurrentMchNo().equals(dbRecord.getMchNo())) {
		    return ApiRes.fail(ApiCodeEnum.SYS_PERMISSION_ERROR);
        }

		mchStoreDevice.setMchNo(dbRecord.getMchNo());
		mchStoreDevice.setBindType(dbRecord.getBindType());

		//获取商户信息
		MchStore mchStore = mchStoreService.getById(dbRecord.getStoreId());
		if(mchStore == null){
			throw new BizException("门店不存在！");
		}
		if (StringUtils.isNotEmpty(mchStore.getMchApplyId())){
			MchApplyment applyment = mchApplymentService.getById(mchStore.getMchApplyId());
			if (applyment != null){
				mchStoreDevice.setMchApplyName(applyment.getMchShortName());
				mchStoreDevice.setMchApplyId(mchStore.getMchApplyId());
			}
		}

		// 云喇叭  同步绑定码牌
		if (dbRecord.getDeviceType() == MchStoreDevice.DEVICE_TYPE_SPEAKER) {

			// 绑定类型 为码牌时，需传入绑定的码牌id
			List<Long> qrcList = new LinkedList<>();
			if (null != mchStoreDevice.getBindType() && mchStoreDevice.getBindType() == MchStoreDevice.DEVICE_BIND_TYPE_QRC) {
				qrcList = JSONArray.parseArray(getValStringRequired("qrcIdList"), Long.class);
			}

			boolean result = mchStoreDeviceService.bindSpeakerDevice(mchStoreDevice, dbRecord.getBindQrcId(), qrcList);
			if (!result) {
				return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
			}
			//如果更新金额则需要关联到码牌表进行同步更新
			//判断是否绑定了码牌
			if (mchStoreDevice.getBindQrcId() != null){
				MchQrcodeCard mchQrcodeCard = mchQrcodeCardService.getById(mchStoreDevice.getBindQrcId());
				if (mchQrcodeCard != null){
					mchQrcodeCard.setFixedFlag(mchStoreDevice.getFixedFlag());
					mchQrcodeCard.setFixedPayAmount(mchStoreDevice.getFixedPayAmount());
					mchQrcodeCardService.updateById(mchQrcodeCard);
				}
			}
			return ApiRes.ok();
		}

		// 云打印、扫码pos执行更新
		if (mchStoreDeviceService.updateById(mchStoreDevice)) {
			return ApiRes.ok();
		}
		return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 解绑设备
	 */
	@PreAuthorize("hasAnyAuthority('ENT_DEVICE_SPEAKER_EDIT', 'ENT_DEVICE_PRINTER_EDIT', 'ENT_DEVICE_POS_EDIT', 'ENT_DEVICE_AUTO_POS_EDIT'," +
			"'ENT_DEVICE_FACE_APP_EDIT', 'ENT_DEVICE_RUYI_EDIT')")
	@PostMapping("/unbind/{deviceId}")
	public ApiRes unbind(@PathVariable("deviceId") Long deviceId) {

		MchStoreDevice dbRecord = mchStoreDeviceService.getById(deviceId);
		if (dbRecord == null || !dbRecord.getMchNo().equals(getCurrentMchNo()) || dbRecord.getBindState() != CS.YES) {
			return ApiRes.customFail("解绑失败");
		}

		mchStoreDeviceService.unbind(dbRecord);

		return ApiRes.ok();
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 根据ID获取门店设备
	 */
	@PreAuthorize("hasAnyAuthority('ENT_DEVICE_SPEAKER_VIEW', 'ENT_DEVICE_PRINTER_VIEW', 'ENT_DEVICE_POS_VIEW', 'ENT_DEVICE_PLUGIN_CDKEY_VIEW'," +
			"'ENT_DEVICE_FACE_APP_VIEW', 'ENT_DEVICE_RUYI_VIEW')")
	@GetMapping("/{deviceId}")
	public ApiRes get(@PathVariable("deviceId") Long deviceId) {
        MchStoreDevice dbRecord = mchStoreDeviceService.getById(deviceId);
        if (!getCurrentMchNo().equals(dbRecord.getMchNo())) {
            return ApiRes.fail(ApiCodeEnum.SYS_PERMISSION_ERROR);
        }

		// 门店名称
		if (dbRecord.getStoreId() != null) {
			MchStore mchStore = mchStoreService.getById(dbRecord.getStoreId());
			dbRecord.addExt("storeName", mchStore.getStoreName());
		}

		// 应用名称
		if (StringUtils.isNotBlank(dbRecord.getAppId())) {
			MchAppEntity mchAppEntity = mchAppService.getById(dbRecord.getAppId());
			dbRecord.addExt("appName", mchAppEntity.getAppName());
		}

		List<MchQrcDeviceRela> relaList = mchQrcDeviceRelaService.list(MchQrcDeviceRela.gw().eq(MchQrcDeviceRela::getDeviceId, deviceId));
		if (!CollectionUtils.isEmpty(relaList)) {
			// 已绑定的码牌集合
			List<Long> qrcIdList = relaList.stream().map(MchQrcDeviceRela::getQrcId).collect(Collectors.toList());
			List<MchQrcodeCard> qrcList = mchQrcodeCardService.list(MchQrcodeCard.gw()
					.select(MchQrcodeCard::getQrcId, MchQrcodeCard::getQrcAlias)
					.in(MchQrcodeCard::getQrcId, qrcIdList));

			dbRecord.addExt("qrcIdList", qrcIdList);
			dbRecord.addExt("qrcList", qrcList);
		}

		return ApiRes.ok(dbRecord);
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 播报测试
	 */
	@PreAuthorize("hasAuthority('ENT_DEVICE_SPEAKER_TEST')")
	@PostMapping("/speak/{deviceId}")
	public ApiRes speak(@PathVariable("deviceId") Long deviceId) {

		Long amountL = getRequiredAmountL("amount");

		MchStoreDevice mchStoreDevice = mchStoreDeviceService.getOne(MchStoreDevice.gw().eq(MchStoreDevice::getMchNo, getCurrentMchNo()).eq(MchStoreDevice::getDeviceId, deviceId));
		if (mchStoreDevice == null) {
			return ApiRes.customFail("设备不存在");
		}
		if(mchStoreDevice.getState() != CS.YES || mchStoreDevice.getBindState() != CS.YES){
			return ApiRes.customFail("设备已停用或还未绑定");
		}
		DeviceProvideConfig provideConfig = deviceProvideConfigService.getById(mchStoreDevice.getConfigId());
		if (provideConfig == null || provideConfig.getState() != CS.YES) {
			return ApiRes.customFail("设备厂商未配置");
		}
		// 设备类型
		String provider = mchStoreDevice.getProvider();
		ISpeakerService speakerService = SpringBeansUtil.getBean(provider + "SpeakerService", ISpeakerService.class);

		// 设备参数
		JSONObject deviceParams = new JSONObject();
		deviceParams.put("deviceParams", mchStoreDevice.getDeviceParams());
		deviceParams.put("bizConfigParams", mchStoreDevice.getBizConfigParams());
		deviceParams.put("providerParams", provideConfig.getProviderParams());

		// 消息内容
		PayOrderInfo4Device speakPayOrderInfo = new PayOrderInfo4Device();
		speakPayOrderInfo.setAmount(amountL);
		speakPayOrderInfo.setWayCodeType(CS.PAY_WAY_CODE_TYPE.WECHAT);
		speakPayOrderInfo.setPayOrderId(String.valueOf(DateKit.currentTimeMillis()));

		try {
			speakerService.send(deviceParams, speakPayOrderInfo);
			return ApiRes.ok();
		}catch (BizException e) {
			throw e;
		}
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 打印测试
	 */
	@PreAuthorize("hasAuthority('ENT_DEVICE_PRINTER_TEST')")
	@PostMapping("/print/{deviceId}")
	public ApiRes print(@PathVariable("deviceId") Long deviceId) {

		Long amountL = getRequiredAmountL("amount");

		MchStoreDevice mchStoreDevice = mchStoreDeviceService.getOne(MchStoreDevice.gw().eq(MchStoreDevice::getMchNo, getCurrentMchNo()).eq(MchStoreDevice::getDeviceId, deviceId));
		if (mchStoreDevice == null || mchStoreDevice.getState() != CS.YES) {
			return ApiRes.customFail("设备不存在");
		}

		DeviceProvideConfig provideConfig = deviceProvideConfigService.getById(mchStoreDevice.getConfigId());
		if (provideConfig == null || provideConfig.getState() != CS.YES) {
			return ApiRes.customFail("设备厂商未配置");
		}

		// 设备类型
		String provider = mchStoreDevice.getProvider();
		IPrinterService printerService = SpringBeansUtil.getBean(provider + "PrinterService", IPrinterService.class);

		// 设备参数
		JSONObject deviceParams = new JSONObject();
		deviceParams.put("deviceParams", mchStoreDevice.getDeviceParams());
		deviceParams.put("bizConfigParams", mchStoreDevice.getBizConfigParams());
		deviceParams.put("providerParams", provideConfig.getProviderParams());

		// 消息内容
		PayOrderInfo4Device printPayOrderInfo = new PayOrderInfo4Device();
		printPayOrderInfo.setAmount(amountL);
		printPayOrderInfo.setWayCodeType(CS.PAY_WAY_CODE_TYPE.WECHAT);
		printPayOrderInfo.setPayOrderId(String.valueOf(DateKit.currentTimeMillis()));
		printPayOrderInfo.setCreatedAt(DateUtil.date());

		try {
			printerService.send(deviceParams, printPayOrderInfo);
			return ApiRes.ok();
		}catch (BizException e) {
			throw e;
		}
	}

	/**
	 * @author: zx
	 * @date: 2021-12-28 09:15
	 * @describe: 清空打印队列
	 */
	@PreAuthorize("hasAuthority('ENT_DEVICE_PRINTER_CLEAR')")
	@PostMapping("/clear/{deviceId}")
	public ApiRes clear(@PathVariable("deviceId") Long deviceId) {

		MchStoreDevice mchStoreDevice = mchStoreDeviceService.getOne(MchStoreDevice.gw().eq(MchStoreDevice::getMchNo, getCurrentMchNo()).eq(MchStoreDevice::getDeviceId, deviceId));
		if (mchStoreDevice == null || mchStoreDevice.getState() != CS.YES) {
			return ApiRes.customFail("设备不存在");
		}

		DeviceProvideConfig provideConfig = deviceProvideConfigService.getById(mchStoreDevice.getConfigId());
		if (provideConfig == null || provideConfig.getState() != CS.YES) {
			return ApiRes.customFail("设备厂商未配置");
		}

		// 设备类型
		String provider = mchStoreDevice.getProvider();
		IPrinterService printerService = SpringBeansUtil.getBean(provider + "PrinterService", IPrinterService.class);

		// 设备参数
		JSONObject deviceParams = new JSONObject();
		deviceParams.put("deviceParams", mchStoreDevice.getDeviceParams());
		deviceParams.put("bizConfigParams", mchStoreDevice.getBizConfigParams());
		deviceParams.put("providerParams", provideConfig.getProviderParams());

		try {
			printerService.clearPrinter(deviceParams);
			return ApiRes.ok();
		}catch (Exception e) {
			return ApiRes.customFail(e.getMessage());
		}
	}

	/** 校验设备能否绑定 */
	private JSONObject checkDeviceBindInfo(String deviceNo, String provider, Byte deviceType) {

		if (StringUtils.isBlank(deviceNo)) {
			throw new BizException("deviceNo必填");
		}

		MchInfo mchInfo = mchInfoService.getById(getCurrentMchNo());
		String agentNo = mchInfo.getAgentNo();

		LambdaQueryWrapper<MchStoreDevice> wrapper = MchStoreDevice.gw()
				.eq(MchStoreDevice::getDeviceNo, deviceNo)
				.eq(MchStoreDevice::getDeviceType, deviceType)
				.eq(MchStoreDevice::getBindState, CS.NO);

		// 当前商户存在上级代理，查询上级服务商或平台未划拨的设备
		if (StringUtils.isNotBlank(agentNo)) {
			wrapper.and(wr -> {
				wr.eq(StringUtils.isNotBlank(agentNo), MchStoreDevice::getAgentNo, agentNo)
						.or().eq(MchStoreDevice::getAgentNo, "")
						.or().isNull(MchStoreDevice::getAgentNo);
			});

		// 查询未划拨的设备
		}else {
			wrapper.and(wr -> {
				wr.eq(MchStoreDevice::getAgentNo, "")
						.or().isNull(MchStoreDevice::getAgentNo);
			});
		}

		if (StringUtils.isNotBlank(provider)) {
			wrapper.eq(MchStoreDevice::getProvider, provider);
		}

		List<MchStoreDevice> deviceList = mchStoreDeviceService.list(wrapper);

		// 方法返回内容  { checkCode: "", checkMsg: "" }
		// checkCode：0-不存在，无法绑定  1-仅存在一个，可直接绑定  2-存在多个，需选择厂商
		// checkMsg  错误原因
		JSONObject res = new JSONObject();

		// 未查询到设备
		if (CollectionUtils.isEmpty(deviceList)) {
			res.put("checkCode", "0");
			res.put("checkMsg", "当前设备无法绑定");
		}

		// 查询到多个厂商
		else if (deviceList.size() > 1) {
			res.put("checkCode", "2");
			res.put("deviceList", deviceList);
			res.put("checkMsg", "请选择厂商");
		}

		else {
			res.put("checkCode", "1");
			res.put("checkMsg", "当前设备允许绑定");
			res.put("deviceId", deviceList.get(0).getDeviceId()); // 可绑定的设备号
			res.put("bindQrcId", deviceList.get(0).getBindQrcId()); // 初始绑定码牌ID
		}

		return res;
	}

	@MethodLog(remark = "获取营销设备广告详情")
	@RequestMapping(value="/getMarketAdvertInfo/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getMarketAdvertInfo(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONObject result = marketAdvertService.getMarketAdvertInfo(record);
		return ApiRes.ok(result);
	}

	/**
	 * 获取跑马灯广告
	 * @return
	 */
	@MethodLog(remark = "获取设备跑马灯广告配置")
	@RequestMapping(value="/getMarqueeInfo/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getMarqueeInfo(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONObject result = marketAdvertService.getMarqueeInfo(record);
		return ApiRes.ok(result);
	}

	/**
	 * 设置跑马灯广告
	 * @return
	 */
	@MethodLog(remark = "设备跑马灯广告配置")
	@RequestMapping(value="/setMarqueeInfo", method = RequestMethod.POST)
	public ApiRes setMarqueeInfo() {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setMchNo(getCurrentMchNo());
		marketAdvertService.saveMarqueeInfo(record);
		return ApiRes.ok();
	}


	/**
	 * 获取设备坐标信息
	 * @return
	 */
	@MethodLog(remark = "获取设备营销区坐标信息")
	@RequestMapping(value="/getMarketPoint/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getMarketLocation(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONObject result = marketAdvertService.getMarketLocation(record);
		return ApiRes.ok(result);
	}


	/**
	 * 获取设备图片信息
	 * @return
	 */
	@MethodLog(remark = "获取设备营销区图片信息")
	@RequestMapping(value="/getMarketPic/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getMarketPic(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONArray result = marketAdvertService.getMarketPic(record);
		return ApiRes.ok(result);
	}


	/**
	 * 设置营销区域的图片信息
	 * @return
	 */
	@MethodLog(remark = "设置营销区域的图片信息")
	@RequestMapping(value="/setMarketPic", method = RequestMethod.POST)
	public ApiRes setMarketPic() {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setMchNo(getCurrentMchNo());
		marketAdvertService.saveMarketPic(record);
		return ApiRes.ok();
	}


	/**
	 * 设置营销区域的图片信息
	 * @return
	 */
	@MethodLog(remark = "查询设备活动码信息")
	@RequestMapping(value="/getActivityQrCode/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getActivityQrCode(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONObject result = marketAdvertService.getActivityQrCode(record);
		return ApiRes.ok(result);
	}


	/**
	 * 获取设备付款码坐标
	 * @return
	 */
	@MethodLog(remark = "获取设备付款区域坐标")
	@RequestMapping(value="/getMarketPayCodeInfo/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getMarketPayCodeInfo(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONObject result = marketAdvertService.getMarketPayCodeInfo(record);
		return ApiRes.ok(result);
	}


	/**
	 * 获取设备付款区域图片信息
	 * @return
	 */
	@MethodLog(remark = "获取设备付款区域图片信息")
	@RequestMapping(value="/getMarketPayCodePic/{deviceNo}", method = RequestMethod.GET)
	public ApiRes getMarketPayCodePic(@PathVariable("deviceNo") String deviceNo) {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setDeviceNo(deviceNo);
		record.setMchNo(getCurrentMchNo());
		JSONObject result = marketAdvertService.getMarketPayCodePic(record);
		return ApiRes.ok(result);
	}

	/**
	 * 设置设备付款区域的图片信息
	 * @return
	 */
	@MethodLog(remark = "设置设备付款区域的图片信息")
	@RequestMapping(value="/setMarketPayCodePic", method = RequestMethod.POST)
	public ApiRes setMarketPayCodePic() {
		MchStoreDevice record = getObject(MchStoreDevice.class);
		record.setMchNo(getCurrentMchNo());
		marketAdvertService.setMarketPayCodePic(record);
		return ApiRes.ok();
	}
	/*
	* // 飞鹅云打印  同步修改上游
		String provider = mchStoreDevice.getProvider();
		if ("fe".equals(provider)) {
			IPrinterService printerService = SpringBeansUtil.getBean(provider + "PrinterService", IPrinterService.class);

			// 设备参数
			JSONObject deviceParams = new JSONObject();

			// 添加上游所需设备名称字段
			JSONObject deviceJSON = JSONObject.parseObject(mchStoreDevice.getDeviceParams());
			deviceJSON.put("deviceName", mchStoreDevice.getDeviceName());

			deviceParams.put("deviceParams", deviceJSON.toJSONString());
			deviceParams.put("bizConfigParams", mchStoreDevice.getBizConfigParams());
			deviceParams.put("providerParams", deviceProvideConfig.getProviderParams());

			try {
				String editResult = printerService.editPrinter(deviceParams);
				if (StringUtils.isBlank(editResult)) {
					return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
				}
			}catch (Exception e) {
				return ApiRes.customFail(e.getMessage());
			}
		}
	* */
}
