package com.hzs.sale.order.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.RepeatSubmit;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.constant.msg.SaOrderMsgConstants;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.sale.order.controller.ParentOrderController;
import com.hzs.sale.order.param.*;
import com.hzs.sale.shopping.vo.ShoppingCartVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 嗨粉订单
 */
@RestController
@RequestMapping("/api/order")
@Slf4j
public class ApiFanSaOrderController extends ParentOrderController {

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 报单中心确认嗨粉订单，生成待支付订单,只有注册订单有待支付订单
     *
     * @param orderParam 订单参数
     */
    @PostMapping("/confirm-fun-order")
    @RepeatSubmit
    public AjaxResult confirmHaiFunOrder(@Validated @RequestBody OrderParam orderParam) {
        if (orderParam.getDeleteList() == null || orderParam.getDeleteList().size() == 0) {
            return AjaxResult.error(TransactionUtils.getContent(SaOrderMsgConstants.SPECIAL_AREA_NOT_EMPTY));
        }
        initSecurityOrderParam(orderParam);
        // 验证必输项
        if (validateFieldNotNull(orderParam, orderParam.getValidConfirmField()) ||
                !checkRecMsgBoolean(orderParam)) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        orderParam.setIsHaiFun(true);
        AjaxResult result = validateSaveMember(orderParam);
        if ((Integer) result.get(AjaxResult.CODE_TAG) == HttpStatus.ERROR) {
            return result;
        }
        convertShoppingCarToSku(orderParam);
        if (getOrderService().confirmRegSaOrderByOrderParam(orderParam)) {
            //删除购物车下单的数据
            if (CollectionUtil.isNotEmpty(orderParam.getDeleteList())) {
                ShoppingCartVO sop = new ShoppingCartVO();
                sop.setDeleteList(orderParam.getDeleteList());
                shoppingCartService.batchDelShopping(SecurityUtils.getUserId(), sop);
            }
            return AjaxResult.success(orderParam);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 报单中心确认嗨粉订单，生成待支付订单,只有注册订单有待支付订单
     *
     * @param orderParam 订单参数 自注册
     */
    @PostMapping("/confirm-self-fun-order")
    @RepeatSubmit
    public AjaxResult confirmSelfFunOrder(@Validated @RequestBody OrderParam orderParam) {
        initSecurityOrderParam(orderParam);
        // 验证必输项
        if (!checkRecMsgBoolean(orderParam)) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        orderParam.setIsHaiFun(true);

        CuMember cuMember = iMemberServiceApi.queryMember(orderParam.getCenterCodeId()).getData();
        if (cuMember == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.PARENT_NOT_EXISTS));
        }

        if (getOrderService().confirmSelfFunOrderByOrderParam(orderParam, cuMember)) {
            return AjaxResult.success(orderParam);
        } else {
            return AjaxResult.error();
        }
    }

}
