/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.gateway.goods_gatway.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.common.enums.*;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.gateway.goods_gatway.dto.AppGoodsBillsDTO;
import org.springblade.gateway.goods_gatway.service.IAppGoodsBillsService;
import org.springblade.ldt.bills.entity.Bills;
import org.springblade.ldt.bills.entity.GoodsBills;
import org.springblade.ldt.bills.mapper.GoodsBillsMapper;
import org.springblade.ldt.bills.service.IBillsService;
import org.springblade.ldt.bills.service.IGoodsBillsService;
import org.springblade.ldt.bills.vo.GoodsBillsVO;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.payment.entity.SuccessParams;
import org.springblade.payment.handle.Trade;
import org.springblade.payment.handle.entity.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单 服务实现类
 *
 * @author huangmp
 * @since 2021-09-26
 */
@Service
public class AppGoodsBillsServiceImpl extends BaseServiceImpl<GoodsBillsMapper, GoodsBills> implements IAppGoodsBillsService {


	@Resource
	private ILoginUserService loginUserService;
	@Resource
	private Trade trade;
	@Resource
	private IShopService shopService;


	@Override
	public Map order(AppGoodsBillsDTO appGoodsBillsDTO) {
		LoginUser loginUser = loginUserService.getById(appGoodsBillsDTO.getPayId());
		Assert.notNull(loginUser,() -> {throw  new ServiceException(ResCode.USER_NOT_FOUNT);});
		Shop shop = shopService.getById(appGoodsBillsDTO.getReceiveId());
		Assert.notNull(shop,() -> {throw  new ServiceException("找不到下单商户");});

		//处理订单
		Order order = Order.builder()
			.appId(appGoodsBillsDTO.getAppId())
			.billsTitle("用户订餐")
			.loginUser(loginUser)
			//商品价格单位为分，bills账单单位为元
			.money(BigDecimal.valueOf(appGoodsBillsDTO.getTotalPrice()).divide(BigDecimal.valueOf(100)))
			.expireTime(appGoodsBillsDTO.getExpireTime())
			.shopId(appGoodsBillsDTO.getReceiveId())
			.openId(loginUser.getOpenid())
			.mallId(shop.getMallId())
			.build();

		SuccessParams successParams;
		try {
			successParams= trade.tradeForScanPay(order,appGoodsBillsDTO.getChannelId());
		} catch (Exception e) {
			e.printStackTrace();
			throw  new ServiceException(ResCode.TRADE_ERROR);
		}
		//插入商品订单
		appGoodsBillsDTO.setBillsId(successParams.getBills().getId());
		//取手机号作为取餐号
		String verifyNum = StringUtil.sub(appGoodsBillsDTO.getUserPhone(), 7, 11);
		appGoodsBillsDTO.setVerifyNum(Integer.valueOf(verifyNum));
		appGoodsBillsDTO.setPayStatus(successParams.getStatus());
		appGoodsBillsDTO.setTenantId(successParams.getTenantId());
		if (appGoodsBillsDTO.getPayStatus().equals(AppConstant.BillPayStatus.付款成功) ){
			appGoodsBillsDTO.setOrderStatus(GoodsConstant.GOODS_STATUS.制作中.name());
		}
		this.save(appGoodsBillsDTO);
		Map resMap = new HashMap(2);
		resMap.put("successParams", successParams);
		resMap.put("goodsBills", appGoodsBillsDTO);
		return resMap;
	}

}
