package com.hzs.scm.store.controller.api;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EStoreStatus;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.store.ICuStoreServiceApi;
import com.hzs.member.store.dto.StoreDto;
import com.hzs.scm.store.param.OrderQueryParam;
import com.hzs.scm.store.param.StoreHandNumParam;
import com.hzs.scm.store.service.IIcStoreHandNumService;
import com.hzs.scm.store.service.IIcStoreOutHService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.util.ArrayList;

/**
 * @description: 店铺发货controller
 * @author: sui q
 * @time: 2024/5/15 14:26
 * @classname: ApiIcStoreOutController
 * @package_name: com.hzs.scm.store.controller.api
 * version 1.0.0
 */
@RestController
@RequestMapping("/api/store-out")
public class ApiIcStoreOutController extends BaseController {

    @DubboReference
    private ICuStoreServiceApi cuStoreServiceApi;

    @Autowired
    private IIcStoreOutHService storeOutService;

    @Autowired
    private IIcStoreHandNumService storeHandNumService;


    /**
     * 查询列表,根据店铺查询，根据登录账号查询，查询待收货订单
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list-receive")
    public TableDataInfo listReceiveStoreOrder(@RequestBody OrderQueryParam orderQueryParam) {
        if (null != orderQueryParam.getStartTime()) {
            orderQueryParam.setStartTime(DateUtils.getStartTime(orderQueryParam.getStartTime()));
        }
        if (null != orderQueryParam.getEndTime()) {
            orderQueryParam.setEndTime(DateUtils.getEndTime(orderQueryParam.getEndTime()));
        }

        // 根据登录账号查询店铺
        StoreDto storeDto = cuStoreServiceApi.getMemberStore(SecurityUtils.getUserId()).getData();
        if (storeDto == null || storeDto.getPkStoreLevel() == null) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        orderQueryParam.setPkCountry(SecurityUtils.getPkCountry());
        orderQueryParam.setPkCuStore(storeDto.getPkId());
        orderQueryParam.setOrderStatus(EStoreStatus.CORP_SEND.getValue());
        return getDataTable(storeOutService.queryStoreOutByCondition(orderQueryParam));
    }

    /**
     * 店铺接受货物
     *
     * @param orderQueryParam 入参
     */
    @PostMapping("/store-receive")
    @Log(module = EOperationModule.STORE_SEND, business = EOperationBusiness.STORE_SEND, method = EOperationMethod.RECEIVE)
    public AjaxResult receiveDeliver(@RequestBody OrderQueryParam orderQueryParam) {
        if (orderQueryParam.getItemIdList() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        // 根据登录账号查询店铺
        StoreDto storeDto = cuStoreServiceApi.getMemberStore(SecurityUtils.getUserId()).getData();
        if (storeDto == null || storeDto.getPkStoreLevel() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.STORE_NOT_EMPTY));
        }
        orderQueryParam.setPkCountry(SecurityUtils.getPkCountry());
        orderQueryParam.setPkCuStore(storeDto.getPkId());
        orderQueryParam.setOrderStatus(EStoreStatus.CORP_SEND.getValue());
        storeOutService.receiveDeliverByItemsIs(orderQueryParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 查询列表,根据店铺查询，根据登录账号查询,查询代发货订单
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list-send")
    public TableDataInfo listSendStoreOrder(@RequestBody OrderQueryParam orderQueryParam) {
        if (null != orderQueryParam.getStartTime()) {
            orderQueryParam.setStartTime(DateUtils.getStartTime(orderQueryParam.getStartTime()));
        }
        if (null != orderQueryParam.getEndTime()) {
            orderQueryParam.setEndTime(DateUtils.getEndTime(orderQueryParam.getEndTime()));
        }

        // 根据登录账号查询店铺
        StoreDto storeDto = cuStoreServiceApi.getMemberStore(SecurityUtils.getUserId()).getData();
        if (storeDto == null || storeDto.getPkStoreLevel() == null) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        orderQueryParam.setPkCountry(SecurityUtils.getPkCountry());
        orderQueryParam.setPkCuStore(storeDto.getPkId());
        orderQueryParam.setOrderStatus(EStoreStatus.STORE_RECEIVE.getValue());
        return getDataTable(storeOutService.queryStoreOutByCondition(orderQueryParam));
    }

    /**
     * 店铺发货
     *
     * @param orderQueryParam 入参
     */
    @PostMapping("/store-send")
    @Log(module = EOperationModule.STORE_SEND, business = EOperationBusiness.STORE_SEND, method = EOperationMethod.RECEIVE)
    public AjaxResult storeSendDeliver(@RequestBody OrderQueryParam orderQueryParam) {
        if (orderQueryParam.getItemIdList() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        // 根据登录账号查询店铺
        StoreDto storeDto = cuStoreServiceApi.getMemberStore(SecurityUtils.getUserId()).getData();
        if (storeDto == null || storeDto.getPkStoreLevel() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.STORE_NOT_EMPTY));
        }
        orderQueryParam.setPkCountry(SecurityUtils.getPkCountry());
        orderQueryParam.setPkCuStore(storeDto.getPkId());
        orderQueryParam.setOrderStatus(EStoreStatus.STORE_RECEIVE.getValue());
        storeOutService.sendDeliverByStore(orderQueryParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 查询列表,根据店铺查询，根据登录账号查询,查询已发货订单
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list-store-order")
    public TableDataInfo listStoreOrder(@RequestBody OrderQueryParam orderQueryParam) {
        if (null != orderQueryParam.getStartTime()) {
            orderQueryParam.setStartTime(DateUtils.getStartTime(orderQueryParam.getStartTime()));
        }
        if (null != orderQueryParam.getEndTime()) {
            orderQueryParam.setEndTime(DateUtils.getEndTime(orderQueryParam.getEndTime()));
        }

        // 根据登录账号查询店铺
        StoreDto storeDto = cuStoreServiceApi.getMemberStore(SecurityUtils.getUserId()).getData();
        if (storeDto == null || storeDto.getPkStoreLevel() == null) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        orderQueryParam.setPkCountry(SecurityUtils.getPkCountry());
        orderQueryParam.setPkCuStore(storeDto.getPkId());
        orderQueryParam.setOrderStatus(EStoreStatus.STORE_SEND.getValue());
        return getDataTable(storeOutService.queryStoreOutByCondition(orderQueryParam));
    }

    /**
     * 查询列表,查询店铺现存量
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list-store-hand")
    public TableDataInfo listStoreHandNum(@RequestBody StoreHandNumParam storeHandNumParam) {
        // 根据登录账号查询店铺
        StoreDto storeDto = cuStoreServiceApi.getMemberStore(SecurityUtils.getUserId()).getData();
        if (storeDto == null || storeDto.getPkStoreLevel() == null) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        storeHandNumParam.setPkCountry(SecurityUtils.getPkCountry());
        storeHandNumParam.setPkMemberStore(storeDto.getPkId());
        return getDataTable(storeHandNumService.queryIcStoreHandNumByCondition(storeHandNumParam));
    }
}
