package cn.freemud.service.impl

import cn.freemud.adapter.OrderAdapter
import cn.freemud.entities.bo.CreateOrderBONew
import cn.freemud.entities.dto.blacklist.Result
import cn.freemud.entities.dto.blacklist.resp.MemberBlacklistVO
import cn.freemud.entities.dto.shoppingCart.ShoppingCartGoodsDto
import cn.freemud.entities.dto.store.StoreMixResponseDto
import cn.freemud.entities.vo.CreateOrderVo
import cn.freemud.handler.MemberBlacklistHandler
import cn.freemud.service.business.impl.OrderBusinessServiceImpl
import cn.freemud.service.thirdparty.DataCenterClient
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.freemud.api.assortment.datamanager.entity.vo.AssortmentCustomerInfoVo
import com.freemud.application.sdk.api.ordercenter.request.OrderExtInfoDto
import com.freemud.sdk.api.assortment.order.adapter.OrderSdkAdapter
import spock.lang.Specification

/**
 * @author : xh.Z
 * @email : fisherman0510@163.com
 * @Date : 2022/2/23 17:17
 * @description : 
 */
class OrderAdapterServiceImplTest extends Specification {

    def orderServiceImpl = new OrderServiceImpl()

    def orderAdapter = new OrderAdapter(mcCafePartnerId: ["9999"], cocoPartnerId: ["9999"])

    def itemService = Mock(OrderItemServiceImpl.class)

    def dataCenterClient = Mock(DataCenterClient)
    def orderBusinessService = new OrderBusinessServiceImpl()

    def orderSdkAdapter = new OrderSdkAdapter()

    def memberBlacklistHandler = new MemberBlacklistHandler()

    void setup() {
        orderServiceImpl.orderAdapter = orderAdapter
        orderServiceImpl.itemService = itemService
        orderServiceImpl.memberBlacklistHandler = memberBlacklistHandler
        memberBlacklistHandler.dataCenterClient = dataCenterClient
        orderBusinessService.orderSdkAdapter = orderSdkAdapter
        orderServiceImpl.orderBusinessService = orderBusinessService

    }

    def "测试华莱士订单问题"() {
        given: "参数设置"
        def createOrderBO = new CreateOrderBONew()
        // 接口请求信息
        String createVOStr = "{\"buyType\":0,\"channel\":\"1\",\"channelType\":\"saas\",\"couponCode\":\"\",\"couponCodes\":[],\"enableSharing\":1,\"expectTime\":\"2022-06-29 17:15:00\",\"latitude\":\"39.901173231336806\",\"longitude\":\"116.55753119574653\",\"menuType\":\"saas\",\"mobile\":\"15801239380\",\"orderRemark\":\"\",\"orderType\":1,\"partnerId\":\"2419\",\"payChannelType\":1,\"reachStoreType\":4,\"sessionId\":\"bf4635a8baab114c7ca03f4d15c702974af862b9\",\"sessionKey\":\"ZWegai1GX1jFoiOo6lXOIA==\",\"shopId\":\"76186390\",\"storeAddress\":\"北京市朝阳区朝阳北路龙湖长楹天街购物中心西区4F\",\"storeName\":\"眉州东坡-长楹天街店\",\"storeNameEn\":\"\",\"tableNumber\":\"\",\"takeMealFlag\":\"1\",\"takeMealTime\":\"17:15\",\"thirdShopId\":\"76186390\",\"useCustomerScore\":2,\"userId\":\"3655291541765197207\",\"userName\":\"\",\"version\":\"2.0.69.2\"}"
        def createOrderVo = JSON.parseObject(createVOStr, CreateOrderVo.class)
        createOrderBO.setCreateOrderVo(createOrderVo)
        // 登录信息
        def userLoginInfoDto = JSON.parseObject("{\"accessToken\":\"\",\"alipayUserId\":\"\",\"appId\":\"wx3f7ca0c75b213380\",\"appSecret\":\"\",\"appsecret\":\"\",\"channel\":\"1\",\"customerId\":\"\",\"devBrand\":\"\",\"devModel\":\"\",\"iappId\":\"7\",\"memberId\":\"3614319825989629832\",\"mobile\":\"17327190802\",\"newMember\":false,\"nickName\":\"SWEET SUGA\",\"openId\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"openid\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"partnerId\":\"2581\",\"photoUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/zahIlicTNSM0IA0qjOryv6s0SzIhlwW9EpQKvL63GbPhBwficMAibDohDEibSCQU0I5KohibIpp0yL57TWRcVKrRH2A/132\",\"registerTime\":\"2021-02-26 14:10:26\",\"sessionId\":\"cef46527f632a961af7a84209bdb8fcd0997c936\",\"sessionKey\":\"hZtQ1Usx/NhEUyoyduF+FQ==\",\"session_key\":\"hZtQ1Usx/NhEUyoyduF+FQ==\",\"storeId\":\"\",\"tableNumber\":\"\",\"thirdPartyMemberID\":\"\",\"thirdPartyPartnerID\":\"\",\"unionId\":\"o7cEm68JPFhbrzqb0a0BcQE-dDVA\",\"wxAppId\":\"wx3f7ca0c75b213380\",\"wxAppid\":\"wx3f7ca0c75b213380\"}"
                , AssortmentCustomerInfoVo.class)
        createOrderBO.setUserLoginInfoDto(userLoginInfoDto)
        //  购物车信息
        def shoppingCartGoodsDto = JSON.parseObject("{\"activityDiscountsDtos\":[{\"activityCode\":\"JTMJ2022051312090439923\",\"activityName\":\"满减\",\"activityType\":12,\"discountAmount\":-6000,\"priority\":70}],\"isDiscountDelivery\":false,\"originalTotalAmount\":23800,\"packageAmount\":1000,\"packageAmountCollectType\":1,\"products\":[{\"activityDiscountsDtos\":[],\"cartGoodsUid\":\"337d8152-b7ca-47eb-bec7-254a5e3501e5\",\"categoryName\":\"必选项-餐具\",\"classificationId\":\"\",\"classificationName\":\"\",\"comboProducts\":[],\"customerCode\":\"17155\",\"extraList\":[],\"hasProductCoupon\":false,\"isB3S1Coupon\":0,\"isB3S1CouponGoods\":0,\"isMonthCard\":0,\"isSendGoods\":false,\"isTableware\":0,\"linkedId\":\"\",\"materialList\":[],\"originalPrice\":0,\"originalProductType\":1,\"picture\":\"https://picture.sandload.cn/1588219576180.jpg\",\"productCode\":\"17155\",\"qty\":1,\"skuForeignName\":\"\",\"skuId\":\"173451209882017045\",\"skuName\":\"不需要餐具\",\"specialExtra\":[],\"spuForeignName\":\"\",\"spuId\":\"173451209882017045\",\"spuName\":\"不需要餐具\",\"stockLimit\":false,\"tax\":0.01,\"taxId\":\"\",\"totalDiscountAmount\":0,\"unit\":\"\",\"weight\":0.01},{\"activityDiscountsDtos\":[{\"activityCode\":\"JTMJ2022051312090439923\",\"activityName\":\"满减\",\"activityType\":12,\"actualActivityGoodsNumber\":1,\"discountAmount\":6000}],\"cartGoodsUid\":\"236eed7d-a883-4a57-9de4-0fd28c470ebb\",\"categoryName\":\"招牌菜\",\"classificationId\":\"\",\"classificationName\":\"\",\"comboProducts\":[],\"customerCode\":\"1026\",\"extraList\":[],\"hasProductCoupon\":false,\"isB3S1Coupon\":0,\"isB3S1CouponGoods\":0,\"isMonthCard\":0,\"isSendGoods\":false,\"isTableware\":0,\"linkedId\":\"\",\"materialList\":[],\"originalPrice\":23800,\"originalProductType\":1,\"picture\":\"https://picture.sandload.cn/1643174237586.jpg\",\"productCode\":\"1026\",\"productType\":11,\"qty\":1,\"skuForeignName\":\"\",\"skuId\":\"170183609423072022\",\"skuName\":\"东坡烤鸭(只)\",\"specialExtra\":[],\"spuForeignName\":\"\",\"spuId\":\"170183609423072022\",\"spuName\":\"东坡烤鸭(只)\",\"stockLimit\":false,\"tax\":0.01,\"taxId\":\"\",\"totalDiscountAmount\":6000,\"unit\":\"g\",\"weight\":0.0}],\"reduceScore\":0,\"scoreReduceAmount\":0,\"stocks\":[{\"activityCode\":\"JTMJ2022051312090439923\",\"activityStock\":1,\"activityType\":12,\"goodsStock\":[],\"userGoodsStock\":[]}],\"totalAmount\":17800,\"totalDiscountAmount\":6000}"
                , ShoppingCartGoodsDto.class)
        createOrderBO.setShoppingCartGoodsDto(shoppingCartGoodsDto)

        //拓展信息
        def extInfo = new OrderExtInfoDto()
        createOrderBO.setExtInfo(extInfo)

        // 门店信息  /api/v2/organization/getStoreMix
        def storeMixResponseDto = JSON.parseObject("{\"parentUntilTop\":{\"orgList\":[{\"organizationCode\":\"kgdbzhsaasa6\",\"organizationId\":\"2459b322-a346-4ebc-bebb-a8557487ec93\",\"organizationName\":\"上海非码网络科技有限公司\",\"partnerId\":\"2725\",\"sort\":1,\"typeFlag\":1}]},\"storeInfo\":{\"activeFlag\":1,\"address\":\"沪宜公路1030号非码科技\",\"addressEn\":\"\",\"briefName\":\"\",\"businessHours\":\"00:00-23:59\",\"businessHoursDay\":\"00:00-23:59\",\"businessType\":\"1\",\"city\":\"上海市\",\"collection\":\"0\",\"delivery\":\"2\",\"deliveryHours\":\"00:00-23:59\",\"deliveryHoursDay\":\"00:00-23:59\",\"deliveryRemark\":\"2222\",\"distributionScope\":\"\",\"freeDeliveryPrice\":1.0,\"geohash\":\"wtw602\",\"isBusinessOpen\":\"1\",\"isSelfMention\":1,\"latitude\":\"31.293201\",\"limitType\":0,\"logoUrl\":\"https://picture.sandload.cn/1648625821468.jpg\",\"longitude\":\"121.304399\",\"parentCode\":\"kgdbzhsaasa6\",\"parentId\":\"2459b322-a346-4ebc-bebb-a8557487ec93\",\"parentName\":\"上海非码网络科技有限公司\",\"partnerId\":\"2725\",\"phone\":\"18868926846\",\"pickUpActiveFlag\":1,\"province\":\"上海市\",\"region\":\"嘉定区\",\"state\":\"1\",\"status\":1,\"storeCode\":\"JIALE001\",\"storeId\":\"ffe2fe77-e6d4-4f3a-b129-8e1893cb2d4c\",\"storeName\":\"晓航自测门店001\",\"storeNameEn\":\"\",\"storeNotice\":\"1111\",\"takeoutActiveFlag\":1,\"thirdPartCode\":\"\"}}"
                , StoreMixResponseDto.class)
        createOrderBO.setStoreMixResponseDto(storeMixResponseDto)

        // 黑名单校验
        def memberBlacklistVOResult = JSON.parseObject("{\"message\":\"成功\",\"result\":{\"exist\":false},\"status\":true,\"statusCode\":\"200\"}"
                , new TypeReference<Result<MemberBlacklistVO>>(){})
        dataCenterClient.queryByMemberId(_) >> memberBlacklistVOResult

        when: "调用方法"
        def order = orderServiceImpl.sdkCreateOrder(createOrderBO)

        then: "验证"

    }

}
