package com.hzs.sale.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EOrderStatus;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.ext.SaOrderItemsExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.sale.order.SaOrderItems;
import com.hzs.sale.deliver.param.DeliverExcludeProductParam;
import com.hzs.sale.deliver.param.DeliverMergeParam;
import com.hzs.sale.deliver.param.DeliverUnhandledParam;
import com.hzs.sale.deliver.vo.DeliverUnhandledVO;
import com.hzs.sale.deliver.vo.DeliverExcludeProductVO;
import com.hzs.sale.order.mapper.SaOrderItemsMapper;
import com.hzs.sale.order.param.OrderItemsParam;
import com.hzs.sale.order.param.WaresOrderParam;
import com.hzs.sale.order.service.ISaOrderItemsService;
import com.hzs.sale.order.vo.WaresOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 销售订单-明细表 服务实现类
 */
@Service
public class SaOrderItemsServiceImpl extends ServiceImpl<SaOrderItemsMapper, SaOrderItems> implements ISaOrderItemsService {

    private SaOrderHandle saOrderHandle;

    @Autowired
    public void setSaOrderHandle(SaOrderHandle saOrderHandle) {
        this.saOrderHandle = saOrderHandle;
    }

    @Override
    public List<DeliverUnhandledVO> queryDeliverUnhandledList(DeliverUnhandledParam param, Integer pkCountry) {
        return baseMapper.queryDeliverUnhandledList(param, pkCountry);
    }

    @Override
    public List<DeliverExcludeProductVO> queryExcludeProductByIdList(List<Long> idList, Integer pkCountry) {
        return baseMapper.queryExcludeProductByIdList(idList, pkCountry);
    }

    @Override
    public List<DeliverExcludeProductVO> queryExcludeProductByQueryParam(DeliverExcludeProductParam param, Integer pkCountry) {
        return baseMapper.queryExcludeProductByQueryParam(param, pkCountry);
    }

    @Override
    public List<SaOrderItemsExt> queryMergeByQueryParam(DeliverMergeParam param, Integer pkCountry) {
        return baseMapper.queryMergeByQueryParam(param, pkCountry);
    }

    @Override
    public List<SaOrderItemsExt> queryMergeByIdList(List<Long> idList, DeliverMergeParam param, Integer pkCountry) {
        return baseMapper.queryMergeByIdList(idList, param, pkCountry);
    }

    @Override
    public Boolean updateOrderItems(SaOrder saOrder) {
        return baseMapper.updateOrderItems(saOrder) > 0;
    }

    @Override
    public void updateOrderItemsByMemberList(List<Long> memberList, Long pkApprove, Date currentDateTime) {
        baseMapper.updateOrderItemsByMemberList(memberList, pkApprove, currentDateTime);
    }

    @Override
    public Boolean updateOrderItemsById(List<Long> pkOrderItemsId, Long pkApprove, Date currentDate) {
        return baseMapper.updateOrderItemsById(pkOrderItemsId, pkApprove, currentDate) > 0;
    }

    @Override
    public List<SaOrderItemsExt> queryListByOrderId(List<Long> orderIdList) {
        return baseMapper.queryListByOrderId(orderIdList);
    }

    @Override
    public List<SaOrderItemsExt> queryListByItemId(List<Long> itemIdList, List<Integer> statusList, Integer pkCountry) {
        return baseMapper.queryListByItemId(itemIdList, statusList, pkCountry);
    }

    @Override
    public List<WaresOrderVo> selectByInvestmentList(WaresOrderParam waresOrderParam) {
        return baseMapper.selectByInvestmentList(waresOrderParam);
    }

    @Override
    public void closeOrder(Integer pkCountry, String data, Integer orderType) {
        baseMapper.closeOrder(pkCountry, data, orderType);
    }

    @Override
    public List<SaOrderItemsExt> selectByList(Long pkOrder) {
        return baseMapper.selectByList(pkOrder);
    }

    @Override
    public List<OrderItemsParam> getOrderItemsParams(List<Long> orderIdList) {
        return baseMapper.getOrderItemsParams(orderIdList);
    }

    @Override
    public int insertBach(List<SaOrderItems> orderItemsList) {
        return baseMapper.insertBach(orderItemsList);
    }

    @Override
    public int checkOrderItemsPutOn(String orderCode) {
        return baseMapper.checkOrderItemsPutOn(orderCode);
    }

    @Override
    public int updateToPayByPkOrder(List<Long> pkOrderList, Integer pkCountry, Long userId) {
        return baseMapper.updateToPayByPkOrder(pkOrderList, pkCountry, userId);
    }

    @Transactional
    @Override
    public boolean orderItemsReturn(List<Long> orderItemsIdList, Long userId, Integer pkCountry) {
        // 验证订单是否存在，存在回写，不存在说明已经撤单，不需要管；
        QueryWrapper<SaOrderItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(SystemFieldConstants.PK_ID, orderItemsIdList);
        List<SaOrderItems> saOrderItemsList = baseMapper.selectList(queryWrapper);
        if (saOrderItemsList == null || saOrderItemsList.size() == 0) {
            return Boolean.TRUE;
        }
        UpdateWrapper<SaOrderItems> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in(SystemFieldConstants.PK_ID, orderItemsIdList);
        // 订单状态不包含 待付款、已关闭 才能进行更新
        updateWrapper.notIn("ORDER_STATUS", EOrderStatus.WAIT_PAY.getValue(), EOrderStatus.CANCEL.getValue());
        if (null != pkCountry) {
            updateWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        }
        updateWrapper.set("ORDER_STATUS", EOrderStatus.PAY.getValue());
        updateWrapper.set("LOGISTICS_CODE", "");
        updateWrapper.set("LOGISTICS_COMPANY", "");
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        return this.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sendDeliverBySaOrder(List<SaOrderExt> saOrderExtList, Long pkMember) {
        List<SaOrderItemsExt> saOrderItemsExtList = new ArrayList<>();
        for (SaOrderExt saOrderExt : saOrderExtList) {
            saOrderItemsExtList.addAll(saOrderExt.getOrderItemsExtList());
        }
        if (saOrderItemsExtList.size() > 0) {
            List<List<?>> orderItemsList = saOrderHandle.handleCutList(saOrderItemsExtList);
            orderItemsList.forEach(itemList -> baseMapper.sendDeliverBySaOrder(itemList, pkMember));
        }
        return Boolean.TRUE;
    }

    @Override
    public List<OrderItemsParam> listPayOrderData(String orderCode) {
        return baseMapper.listPayOrderData(orderCode);
    }

}
