package com.neusoft.smart.pos.trade.controller;


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.trade.request.*;
import com.neusoft.smart.pos.dto.trade.response.*;
import com.neusoft.smart.pos.framework.constants.RoleConstant;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.utils.FileUtils;
import com.neusoft.smart.pos.order.repository.OrderMasterRepository;
import com.neusoft.smart.pos.trade.service.PayChannelService;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;

@Api(value = "商户支付渠道管理使用rest接口", description = "商户支付渠道管理使用rest接口")
@RestController
@RequestMapping(path = "/payChannel")
@Validated
public class PayChannelController {

	@Autowired
	private PayChannelService payChannelService;
	@Autowired
	private OrderMasterRepository orderMasterRepository;

	@ApiOperation(value = "商户支付渠道列表查询接口", notes = "未使用gateway, ota web使用")
	@GetMapping("/payChannels")
	public ResponseData<GetPayChannelsResponse> queryPayChannels(@Valid @ModelAttribute GetPayChannelsRequest payChannelsRequest, BindingResult bindingResult) {
		// 移动终端用户不属于店铺，不能再进行校验
		Integer shopId = RequestHeaderUtil.getStoreIdByRole(payChannelsRequest.getShopId());
		if(null == shopId){
			//throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
			shopId = payChannelsRequest.getShopId();
		}
		Integer orgId = RequestHeaderUtil.getOrgId();
		if(RequestHeaderUtil.getRole().equals(RoleConstant.ROLE_ENDUSER)) {	// 终端用户，根据传入的shopId获取orgId
			val shopOrgInfoMap = orderMasterRepository.queryShopNameAndOrgName(shopId);
			if(shopOrgInfoMap != null){
				orgId = (Integer)shopOrgInfoMap.get("org_id");
			}
		}
		return new ResponseData<>(payChannelService.queryPayChannels(orgId,shopId,null,null));
	}

	@ApiOperation(value = "POS支付渠道列表查询接口", notes = "未使用gateway, pos使用")
	@GetMapping("/posPayChannels")
	public ResponseData<GetPayChannelsResponse> queryPosPayChannels(@Valid @ModelAttribute GetPosPayChannelsRequest posPayChannelsRequest, BindingResult bindingResult) {
		return new ResponseData<>(payChannelService.queryPayChannelsOnShowPos(posPayChannelsRequest.getSerialNo()));
	}

	@ApiOperation(value = "支付渠道信息保存接口", notes = "未使用gateway, ota web使用")
	@PostMapping("/payChannel")
	public ResponseData<PostPayChannelResponse> savePayChannel(@Valid @RequestBody PostPayChannelRequest payChannelRequest, BindingResult bindingResult) {
	    return new ResponseData<>(payChannelService.savePayChannel(payChannelRequest));
	}

	@ApiOperation(value = "支付渠道信息修改接口", notes = "未使用gateway, ota web使用")
	@PutMapping("/payChannel")
	public ResponseData<PutPayChannelResponse> updatePayChannel(@Valid @RequestBody PutPayChannelRequest payChannelRequest, BindingResult bindingResult) {
		return new ResponseData<>(payChannelService.updatePayChannel(payChannelRequest));
	}

	@ApiOperation(value = "支付渠道信息删除接口", notes = "未使用gateway, ota web使用")
	@DeleteMapping("/payChannel")
	public ResponseData<DeletePayChannelResponse> deletePayChannel(@Valid DeletePayChannelRequest payChannelRequest, BindingResult bindingResult) {
		return new ResponseData<>(payChannelService.deletePayChannel(payChannelRequest));
	}

	@ApiOperation(value = "支付渠道信息查询接口", notes = "未使用gateway, ota web使用")
	@GetMapping("/payChannel")
	public ResponseData<GetPayChannelResponse> queryPayChannel(@Valid @ModelAttribute GetPayChannelRequest payChannelRequest, BindingResult bindingResult) {
		return new ResponseData<>(payChannelService.queryPayChannel(payChannelRequest));
	}
	
	@ApiOperation(value = "支付渠道信息查询接口", notes = "支付渠道信息查询方法")
	@GetMapping("/payChannelForPos")
	public ResponseData<GetPayChannelResponse> queryPayChannelForPos(@Valid @ModelAttribute GetPayChannelRequest payChannelRequest, BindingResult bindingResult) {
		return new ResponseData<>(payChannelService.queryPayChannelForPos(payChannelRequest));
	}

	@ApiOperation(value = "支付渠道停用接口", notes = "未使用gateway, ota web使用")
	@PutMapping("/payChannelDisable")
	public ResponseData<PutPayChannelDisableResponse> updatePayChannelDisable(@Valid @RequestBody PutPayChannelDisableRequest payChannelDisableRequest, BindingResult bindingResult) {
		val payChannelEnableResponse = new PutPayChannelDisableResponse(payChannelService.updatePayChannelEnable(payChannelDisableRequest.getShopId(),payChannelDisableRequest.getPayType(), DictionaryConstant.IS_ENABLE_TY.getCode()));
		return new ResponseData<>(payChannelEnableResponse);
	}

	@ApiOperation(value = "支付渠道启用接口", notes = "未使用gateway, ota web使用")
	@PutMapping("/payChannelEnable")
	public ResponseData<PutPayChannelEnableResponse> updatePayChannelEnable(@Valid @RequestBody PutPayChannelEnableRequest payChannelEnableRequest, BindingResult bindingResult) {
		val payChannelEnableResponse = new PutPayChannelEnableResponse(payChannelService.updatePayChannelEnable(payChannelEnableRequest.getShopId(),payChannelEnableRequest.getPayType(), DictionaryConstant.IS_ENABLE_QY.getCode()));
		return new ResponseData<>(payChannelEnableResponse);
	}

	@ApiOperation(value = "获取上传证书字节数组接口", notes = "未使用gateway, ota web使用")
	@PostMapping("/uploadCertFile")
	public ResponseData<byte[]> uploadCertFile(MultipartFile cert) {
		return new ResponseData<>(FileUtils.convertFileToByteArray(cert));
	}
}
	