package co.yixiang.yshop.module.order.service.storeorderdetail;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.enums.OrderInfoEnum;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.order.controller.admin.storeorderdetail.vo.StoreOrderDetailCreateReqVO;
import co.yixiang.yshop.module.order.controller.admin.storeorderdetail.vo.StoreOrderDetailExportReqVO;
import co.yixiang.yshop.module.order.controller.admin.storeorderdetail.vo.StoreOrderDetailPageReqVO;
import co.yixiang.yshop.module.order.controller.admin.storeorderdetail.vo.StoreOrderDetailUpdateReqVO;
import co.yixiang.yshop.module.order.convert.storeorderdetail.StoreOrderDetailConvert;
import co.yixiang.yshop.module.order.dal.dataobject.storeorder.StoreOrderDO;
import co.yixiang.yshop.module.order.dal.dataobject.storeorderdetail.StoreOrderDetailDO;
import co.yixiang.yshop.module.order.dal.mysql.storeorderdetail.StoreOrderDetailMapper;
import co.yixiang.yshop.module.order.enums.OrderDetailStateEnum;
import co.yixiang.yshop.module.product.controller.app.cart.vo.AppStoreCartQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 订单明细 Service 实现类
 *
 * @author moxiangrong
 */
@Service
@Validated
@Slf4j
public class StoreOrderDetailServiceImpl implements StoreOrderDetailService {

    @Resource
    private StoreOrderDetailMapper storeOrderDetailMapper;

    @Override
    public Long createStoreOrderDetail(StoreOrderDetailCreateReqVO createReqVO) {
        // 插入
        StoreOrderDetailDO storeOrderDetail = StoreOrderDetailConvert.INSTANCE.convert(createReqVO);
        storeOrderDetailMapper.insert(storeOrderDetail);
        // 返回
        return storeOrderDetail.getId();
    }

    @Override
    public void updateStoreOrderDetail(StoreOrderDetailUpdateReqVO updateReqVO) {
        // 更新
        StoreOrderDetailDO updateObj = StoreOrderDetailConvert.INSTANCE.convert(updateReqVO);
        storeOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreOrderDetail(Long id) {
        // 删除
        storeOrderDetailMapper.deleteById(id);
    }

    @Override
    public StoreOrderDetailDO getStoreOrderDetail(Long id) {
        return storeOrderDetailMapper.selectById(id);
    }

    @Override
    public List<StoreOrderDetailDO> getStoreOrderDetailList(Collection<Long> ids) {
        return storeOrderDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StoreOrderDetailDO> getStoreOrderDetailPage(StoreOrderDetailPageReqVO pageReqVO) {
        return storeOrderDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StoreOrderDetailDO> getStoreOrderDetailList(StoreOrderDetailExportReqVO exportReqVO) {
        return storeOrderDetailMapper.selectList(exportReqVO);
    }

    //    @Async
    @Override
    public void batchInsert(Long oid, String orderId, List<AppStoreCartQueryVo> cartInfo, StoreOrderDO storeOrder) {
        log.info("==========添加订单详情信息start===========");
        List<StoreOrderDetailDO> list = new ArrayList<>();
        for (AppStoreCartQueryVo cart : cartInfo) {
            for (int i = 0; i < cart.getCartNum(); i++) {
                StoreOrderDetailDO detailDO = new StoreOrderDetailDO();
                detailDO.setOid(oid);
                detailDO.setOrderId(orderId);
                detailDO.setProductId(cart.getProductId());
                detailDO.setSkuId(cart.getProductInfo().getSkuId());
                detailDO.setProductAttrUnique(cart.getProductAttrUnique());
                detailDO.setOriginalPrice(cart.getTruePrice());
                detailDO.setPrice(cart.getPayPrice());
                detailDO.setUseIntegral(cart.getUseIntegral());
                detailDO.setIntegralDeductionAmount(cart.getIntegralDeductionAmount());
                list.add(detailDO);
            }
        }
        storeOrderDetailMapper.batchInsert(list);
    }

    @Override
    public Integer updateState(Long afterSalesId, Long oid) {
        Integer state = OrderInfoEnum.REFUND_STATUS_HAVE_REFUNDED.getValue();
        if (ObjectUtil.isNotNull(afterSalesId)) {
            // 判断是否全部退款,全部退款修改订单状态为已退款
            Long total = storeOrderDetailMapper.selectCount(new LambdaQueryWrapper<StoreOrderDetailDO>()
                    .ne(StoreOrderDetailDO::getState, OrderDetailStateEnum.AFTER_SALES_COMPLETE.getValue())
                    .eq(StoreOrderDetailDO::getOid, oid));
            Long updateNumber = storeOrderDetailMapper.selectCount(new LambdaQueryWrapper<StoreOrderDetailDO>()
                    .eq(StoreOrderDetailDO::getAfterSalesId, afterSalesId));
            state = total.equals(updateNumber) ? state : null;
            // 修改明细状态为售后完成
            storeOrderDetailMapper.update(null, new LambdaUpdateWrapper<StoreOrderDetailDO>()
                    .set(StoreOrderDetailDO::getState, OrderDetailStateEnum.AFTER_SALES_COMPLETE.getValue())
                    .eq(StoreOrderDetailDO::getAfterSalesId, afterSalesId));
        }
        return state;
    }

}
