package com.backendsys.service.B2c;

import com.backendsys.entity.B2c.B2cOrderDTO;
import com.backendsys.mapper.B2c.B2cOrderMapper;
import com.backendsys.utils.response.PageInfoResult;
import com.github.pagehelper.PageHelper;

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class B2cOrderServiceImpl implements B2cOrderService {

    /**
     * Redisson 加锁
     * @return
     */
    @Lazy
    @Autowired
    RedissonClient redissonClient;



    @Autowired
    private B2cOrderMapper b2cOrderMapper;

    /**
     * 查询 列表
     */
    public Map<String, Object> queryOrderList(Integer pageNum, Integer pageSize, B2cOrderDTO b2cOrderDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        List<Map<String, Object>> list = b2cOrderMapper.queryOrderList(b2cOrderDTO);
        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }

    /**
     * 查询 详情
     */
    @Override
    public Map<String, Object> queryOrderDetail(Long good_brand_id) {
        return b2cOrderMapper.queryOrderDetail(good_brand_id);
    }




    /**
     * 创建订单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertOrder(B2cOrderDTO b2cOrderDTO) {
        b2cOrderMapper.insertOrder(b2cOrderDTO);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("order_id", b2cOrderDTO.getOrder_id());
        result.put("prepay_id", b2cOrderDTO.getPrepay_id());
        return result;
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateOrder(B2cOrderDTO b2cOrderDTO) {
        b2cOrderMapper.updateOrder(b2cOrderDTO);
        return Map.of("order_id", b2cOrderDTO.getOrder_id());
    }

}
