package com.backendsys.service.B2c;

import com.backendsys.entity.B2c.B2cGoodSpecDTO;
import com.backendsys.mapper.B2c.B2cGoodSpecMapper;
import com.backendsys.mapper.B2c.B2cGoodSpecSkuMapper;
import com.backendsys.utils.response.PageInfoResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class B2CGoodSpecServiceImpl implements B2cGoodSpecService {

    @Autowired
    private B2cGoodSpecMapper b2cGoodSpecMapper;

    @Autowired
    private B2cGoodSpecSkuMapper b2cGoodSpecSkuMapper;

    /**
     * 查询 列表
     */
    public Map<String, Object> queryGoodSpecList(Integer pageNum, Integer pageSize, B2cGoodSpecDTO b2cGoodSpecDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        List<Map<String, Object>> list = b2cGoodSpecMapper.queryGoodSpecList(b2cGoodSpecDTO);

        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }

    /**
     * 查询 详情
     */
    @Override
    public Map<String, Object> queryGoodSpecDetail(Long good_id) {
        return b2cGoodSpecMapper.queryGoodSpecDetail(good_id);
    }

    /**
     * 创建
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertGoodSpec(B2cGoodSpecDTO b2cGoodSpecDTO) {
        b2cGoodSpecMapper.insertGoodSpec(b2cGoodSpecDTO);
        return Map.of("good_spec_id", b2cGoodSpecDTO.getGood_spec_id());
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateGoodSpec(B2cGoodSpecDTO b2cGoodSpecDTO) {
        b2cGoodSpecMapper.updateGoodSpec(b2cGoodSpecDTO);
        return Map.of("good_spec_id", b2cGoodSpecDTO.getGood_spec_id());
    }

    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteGoodSpec(B2cGoodSpecDTO b2cGoodSpecDTO) {
        b2cGoodSpecMapper.deleteGoodSpec(b2cGoodSpecDTO);
        return Map.of("good_spec_id", b2cGoodSpecDTO.getGood_spec_id());
    }
}
