package org.dromara.zhishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.*;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.zhishu.enums.PddOrderDetailEnum;
import org.dromara.zhishu.enums.PddOrderEnum;
import org.dromara.zhishu.util.JsonObjUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.TShopOrderDetailBo;
import org.dromara.zhishu.domain.vo.TShopOrderDetailVo;
import org.dromara.zhishu.domain.TShopOrderDetail;
import org.dromara.zhishu.mapper.TShopOrderDetailMapper;
import org.dromara.zhishu.service.ITShopOrderDetailService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单详情Service业务层处理
 *
 * @author Lion Li
 * @date 2025-03-13
 */
@RequiredArgsConstructor
@Service
public class TShopOrderDetailServiceImpl implements ITShopOrderDetailService {

    private final TShopOrderDetailMapper baseMapper;

    /**
     * 查询订单详情
     *
     * @param id 主键
     * @return 订单详情
     */
    @Override
    public TShopOrderDetailVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单详情列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单详情分页列表
     */
    @Override
    public TableDataInfo<TShopOrderDetailVo> queryPageList(TShopOrderDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TShopOrderDetail> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(TShopOrderDetail::getCreateTime);
        lqw.orderByDesc(TShopOrderDetail::getId);
//        Page<TShopOrderDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<TShopOrderDetailVo> result = baseMapper.customPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单详情列表
     *
     * @param bo 查询条件
     * @return 订单详情列表
     */
    @Override
    public List<TShopOrderDetailVo> queryList(TShopOrderDetailBo bo) {
        LambdaQueryWrapper<TShopOrderDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TShopOrderDetail> buildQueryWrapper(TShopOrderDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TShopOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TShopOrderDetail::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getShopId()), TShopOrderDetail::getShopId, bo.getShopId());
        lqw.like(StringUtils.isNotBlank(bo.getShopName()), TShopOrderDetail::getShopName, bo.getShopName());
        lqw.eq(bo.getGoodsCount() != null, TShopOrderDetail::getOrderId, bo.getOrderId());
        lqw.eq(bo.getGoodsCount() != null, TShopOrderDetail::getGoodsCount, bo.getGoodsCount());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsId()), TShopOrderDetail::getGoodsId, bo.getGoodsId());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsImg()), TShopOrderDetail::getGoodsImg, bo.getGoodsImg());
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), TShopOrderDetail::getGoodsName, bo.getGoodsName());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsPrice()), TShopOrderDetail::getGoodsPrice, bo.getGoodsPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsSpec()), TShopOrderDetail::getGoodsSpec, bo.getGoodsSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getOuterGoodsId()), TShopOrderDetail::getOuterGoodsId, bo.getOuterGoodsId());
        lqw.eq(StringUtils.isNotBlank(bo.getOuterId()), TShopOrderDetail::getOuterId, bo.getOuterId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuId()), TShopOrderDetail::getSkuId, bo.getSkuId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TShopOrderDetail::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增订单详情
     *
     * @param bo 订单详情
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TShopOrderDetailBo bo) {
        TShopOrderDetail add = MapstructUtils.convert(bo, TShopOrderDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单详情
     *
     * @param bo 订单详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TShopOrderDetailBo bo) {
        TShopOrderDetail update = MapstructUtils.convert(bo, TShopOrderDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TShopOrderDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单详情信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    /**
     * 校验并批量删除订单详情信息
     *

     * @return 是否删除成功
     */
    @Override
    public void insertBatchOrderDetail(String itemList, String orderSn, Long orderId) {

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        JsonArray jsonArray = gson.fromJson(itemList, JsonArray.class);

        QueryWrapper<TShopOrderDetail> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(TShopOrderDetail::getOrderSn,orderSn);
        baseMapper.delete(queryWrapper);


        for (Object o : jsonArray) {

            JsonObject orderObject = gson.fromJson(o.toString(), JsonObject.class);

            TShopOrderDetail detail = new TShopOrderDetail();
            detail.setOrderId(orderId);
            detail.setOrderSn(orderSn);

            Long goodsCount = JsonObjUtil.getLong(orderObject, PddOrderDetailEnum.GOODS_COUNT.getCode());
            Integer count = goodsCount.intValue();
            detail.setGoodsCount(count);

            String goodsImg = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.GOODS_IMG.getCode());
            detail.setGoodsImg(goodsImg);
            String goodsName = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.GOODS_NAME.getCode());
            detail.setGoodsName(goodsName);
            BigDecimal goodsPrice = JsonObjUtil.getDecimal(orderObject, PddOrderDetailEnum.GOODS_PRICE.getCode());
            detail.setGoodsPrice(goodsPrice);
            String goodsSpec = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.GOODS_SPEC.getCode());
            detail.setGoodsSpec(goodsSpec);
            String outerGoodsId = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.OUTER_GOODS_ID.getCode());
            detail.setOuterGoodsId(outerGoodsId);
            String outerId = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.OUTER_ID.getCode());
            detail.setOuterId(outerId);
            String skuId = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.SKU_ID.getCode());
            detail.setSkuId(skuId);

            String goodsId = JsonObjUtil.getString(orderObject, PddOrderDetailEnum.GOODS_ID.getCode());
            detail.setGoodsId(goodsId);


            baseMapper.insert(detail);


        }


    }


}
