package com.hwrj.cloud.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.service.PmsProductService;
import com.hwrj.cloud.admin.dao.SmsFlashPromotionProductRelationDao;
import com.hwrj.cloud.admin.dto.SmsFlashPromotionProduct;
import com.hwrj.cloud.admin.mapper.SmsFlashPromotionProductRelationMapper;
import com.hwrj.cloud.admin.model.SmsFlashPromotionProductRelation;
import com.hwrj.cloud.admin.model.SmsFlashPromotionProductRelationExample;
import com.hwrj.cloud.admin.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 限时购商品关联管理Service实现类
 * Created by macro on 2018/11/16.
 */
@Service
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;

    @Autowired
    private PmsProductService productService;

    @Override
    public int create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation relation : relationList) {
            relationMapper.insert(relation);
        }
        return relationList.size();
    }

    @Override
    public int update(Long id, SmsFlashPromotionProductRelation relation) {
        relation.setId(id);
        return relationMapper.updateByPrimaryKey(relation);
    }

    @Override
    public int delete(Long id) {
        return relationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SmsFlashPromotionProductRelation getItem(Long id) {
        return relationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SmsFlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<SmsFlashPromotionProduct> promotionProducts = relationDao.getList(flashPromotionId, flashPromotionSessionId);
        for (SmsFlashPromotionProduct smsFlashPromotionProduct : promotionProducts) {
//            CommonResult<PmsProductOutput> result = pmsProductClient.getProductByProductId(smsFlashPromotionProduct.getProductId());
//            if (result.getCode() == ResultCode.SUCCESS.getCode()){
//                Gson gson = new Gson();
//                PmsProductOutput pmsProductOutput = gson.fromJson(JSON.toJSONString(result.getData()), PmsProductOutput.class);
//                smsFlashPromotionProduct.setProduct(pmsProductOutput);
//            }
            smsFlashPromotionProduct.setProduct(productService.getProductByProductId(smsFlashPromotionProduct.getProductId()));

        }
        return promotionProducts;
    }

    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        SmsFlashPromotionProductRelationExample example = new SmsFlashPromotionProductRelationExample();
        example.createCriteria()
                .andFlashPromotionIdEqualTo(flashPromotionId)
                .andFlashPromotionSessionIdEqualTo(flashPromotionSessionId);
        return relationMapper.countByExample(example);
    }
}
