package co.yixiang.yshop.module.distributor.service.distributororder;

import co.yixiang.yshop.framework.mybatis.core.query.QueryWrapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributororder.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributororder.DistributorOrderDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.distributor.convert.distributororder.DistributorOrderConvert;
import co.yixiang.yshop.module.distributor.dal.mysql.distributororder.DistributorOrderMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.distributor.enums.ErrorCodeConstants.*;

/**
 * 分销商订单 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class DistributorOrderServiceImpl implements DistributorOrderService {

    @Resource
    private DistributorOrderMapper orderMapper;

    @Override
    public Long createOrder(DistributorOrderCreateReqVO createReqVO) {
        // 插入
        DistributorOrderDO order = DistributorOrderConvert.INSTANCE.convert(createReqVO);
        orderMapper.insert(order);
        // 返回
        return order.getId();
    }

    @Override
    public void updateOrder(DistributorOrderUpdateReqVO updateReqVO) {
        // 校验存在
        validateOrderExists(updateReqVO.getId());
        // 更新
        DistributorOrderDO updateObj = DistributorOrderConvert.INSTANCE.convert(updateReqVO);
        orderMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrder(Long id) {
        // 校验存在
        validateOrderExists(id);
        // 删除
        orderMapper.deleteById(id);
    }

    private void validateOrderExists(Long id) {
        if (orderMapper.selectById(id) == null) {
            throw exception(ORDER_NOT_EXISTS);
        }
    }

    @Override
    public DistributorOrderDO getOrder(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public DistributorOrderDO getOrderByDetailId(Long detailId) {
        return orderMapper.selectOne(DistributorOrderDO::getDetailId, detailId);
    }

    @Override
    public List<DistributorOrderDO> getOrderList(Collection<Long> ids) {
        return orderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributorOrderDO> getOrderPage(DistributorOrderPageReqVO pageReqVO) {
        return orderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributorOrderDO> getOrderList(DistributorOrderExportReqVO exportReqVO) {
        return orderMapper.selectList(exportReqVO);
    }

    @Override
    public DistributorOrderDO sumFirstAmount(Long id, LocalDateTime[] createTime, Integer status) {
        QueryWrapperX<DistributorOrderDO> queryWrapperX = new QueryWrapperX<>();
        queryWrapperX.select("sum(first_amount) as first_amount");
        queryWrapperX.betweenIfPresent("create_time", createTime);
        queryWrapperX.eqIfPresent("superior_id_p1", id);
        queryWrapperX.eqIfPresent("status", status);
        return orderMapper.selectOne(queryWrapperX);
    }

    @Override
    public DistributorOrderDO sumSecondAmount(Long id, LocalDateTime[] createTime, Integer status) {
        QueryWrapperX<DistributorOrderDO> queryWrapperX = new QueryWrapperX<>();
        queryWrapperX.select("sum(second_amount) as second_amount");
        queryWrapperX.betweenIfPresent("create_time", createTime);
        queryWrapperX.eqIfPresent("superior_id_p2", id);
        queryWrapperX.eqIfPresent("status", status);
        return orderMapper.selectOne(queryWrapperX);
    }

    @Override
    public void insertBatch(List<DistributorOrderDO> list) {
        orderMapper.insertBatch(list);
    }


}
