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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EOrderStatus;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

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

    @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<WaresOrderVo> waresListByOrderId(List<Long> orderIdList) {
        return baseMapper.waresListByOrderId(orderIdList);
    }

    @Override
    public List<WaresOrderVo> queryWaresListByOrderId(Long orderId) {
        return baseMapper.queryWaresListByOrderId(orderId);
    }

    @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);
    }

    /**
     * 根据订单主键查询信息
     *
     * @param pkOrder
     * @return
     */
    @Override
    public List<SaOrderItemsExt> selectByList(Long pkOrder) {
        return baseMapper.selectByList(pkOrder);
    }

    /**
     * @description: 根据订单ID查询商品
     * @author: zhang jing
     * @date: 2023/4/21 18:02
     * @param: [canApplyOrderVO]
     * @return: java.util.List<com.hzs.sale.order.vo.CanApplyOrderVO>
     **/
    @Override
    public List<OrderItemsParam> getOrderItemsParams(List<Long> orderIdList) {
        return baseMapper.getOrderItemsParams(orderIdList);
    }

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

    @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(rollbackFor = Exception.class)
    @Override
    public boolean orderItemsReturn(List<Long> orderItemsIdList, Long userId, Integer pkCountry) {
        // 验证订单是否存在，存在回写，不存在说明已经撤单，不需要管；
        LambdaQueryWrapper<SaOrderItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SaOrderItems::getPkId, orderItemsIdList);
        List<SaOrderItems> saOrderItemsList = baseMapper.selectList(queryWrapper);
        if (saOrderItemsList == null || saOrderItemsList.size() == 0) {
            return Boolean.TRUE;
        }
        LambdaUpdateWrapper<SaOrderItems> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SaOrderItems::getPkId, orderItemsIdList);
        // 订单状态不包含 待付款、已关闭 才能进行更新
        updateWrapper.notIn(SaOrderItems::getOrderStatus, EOrderStatus.WAIT_PAY.getValue(), EOrderStatus.CANCEL.getValue());
        if (null != pkCountry) {
            updateWrapper.eq(SaOrderItems::getPkCountry, pkCountry);
        }
        updateWrapper.set(SaOrderItems::getOrderStatus, EOrderStatus.PAY.getValue());
        updateWrapper.set(SaOrderItems::getLogisticsCode, "");
        updateWrapper.set(SaOrderItems::getLogisticsCompany, "");
        updateWrapper.set(SaOrderItems::getPkModified, userId);
        updateWrapper.set(SaOrderItems::getModifiedTime, new Date());
        return this.update(updateWrapper);
    }

    @Override
    public int copyDeductionItems(Long pkOrderOld, Long pkOrderNew, Long userId, Date nowDate) {
        return baseMapper.copyDeductionItems(pkOrderOld, pkOrderNew, userId, nowDate);
    }

    /**
     * 根据订单编号查询订单项
     *
     * @param orderCode
     * @return
     */
    @Override
    public List<OrderItemsParam> selectByWaresCodeList(String orderCode) {
        return baseMapper.selectByWaresCodeList(orderCode);
    }

    @Override
    public BigDecimal getRetailWaresQuantityNew(Long pkMember, List<String> waresCodeList, Date payTime, List<Integer> orderTypeList, Integer systemType) {
        return baseMapper.getRetailWaresQuantityNew(pkMember, waresCodeList, payTime, orderTypeList, systemType);
    }

    @Override
    public int countOrderDeliver(List<String> orderCodeList, Integer pkCountry) {
        return baseMapper.countOrderDeliver(orderCodeList, pkCountry);
    }

}
