package com.backendsys.service.B2c;

import com.backendsys.aspect.HttpRequestAspect;
import com.backendsys.entity.B2c.B2cShoppingCartDTO;
import com.backendsys.mapper.B2c.B2cShoppingCartMapper;
import com.backendsys.utils.response.PageInfoResult;
import org.springframework.beans.factory.annotation.Autowired;
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 B2cShoppingCartServiceImpl implements B2cShoppingCartService {

    @Autowired
    private HttpRequestAspect httpRequestAspect;

    @Autowired
    private B2cShoppingCartMapper b2cShoppingCartMapper;

    /**
     * 查询 购物车列表 (去除分页) (需要分组 待做)
     */
    //Integer pageNum, Integer pageSize, B2cShoppingCartDTO b2cShoppingCartDTO
    public Map<String, Object> queryShoppingCartList() {
        //// 分页查询
        //if (pageNum != null && pageSize != null) {
        //    PageHelper.startPage(pageNum, pageSize);
        //    PageHelper.getLocalPage().setPageSizeZero(true);
        //}
        B2cShoppingCartDTO b2cShoppingCartDTO = new B2cShoppingCartDTO();
        b2cShoppingCartDTO.setMember_id(httpRequestAspect.getMemberId());
        // 分页输出 (自定义)
        List<Map<String, Object>> list = b2cShoppingCartMapper.queryShoppingCartList(b2cShoppingCartDTO);
        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }

    /**
     * 查询 详情 (根据 member_id, good_id, sku_id)
     */
    @Override
    public Map<String, Object> queryShoppingCartDetail(B2cShoppingCartDTO b2cShoppingCartDTO) {
        return b2cShoppingCartMapper.queryShoppingCartDetail(b2cShoppingCartDTO);
    }

    /**
     * 加入购物车
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertShoppingCart(B2cShoppingCartDTO b2cShoppingCartDTO) {
        b2cShoppingCartMapper.insertShoppingCart(b2cShoppingCartDTO);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("shopping_cart_id", b2cShoppingCartDTO.getShopping_cart_id());
        return result;
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateShoppingCart(B2cShoppingCartDTO b2cShoppingCartDTO) {
        b2cShoppingCartMapper.updateShoppingCart(b2cShoppingCartDTO);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("shopping_cart_id", b2cShoppingCartDTO.getShopping_cart_id());
        return result;
        //return Map.of("shopping_cart_id", b2cShoppingCartDTO.getShopping_cart_id());
    }

    /**
     * 清空购物车
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> cleanShoppingCart(B2cShoppingCartDTO b2cShoppingCartDTO) {
        b2cShoppingCartMapper.cleanShoppingCart(b2cShoppingCartDTO);
        return Map.of("member_id", b2cShoppingCartDTO.getMember_id());
    }
}
