package com.hai.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.dao.HighMerchantAccountMarketingMapper;
import com.hai.entity.HighMerchantAccountMarketing;
import com.hai.entity.HighMerchantAccountMarketingExample;
import com.hai.enum_type.MerAccountMarketingType;
import com.hai.service.HighMerchantAccountMarketingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("merchantAccountMarketingService")
public class HighMerchantAccountMarketingServiceImpl implements HighMerchantAccountMarketingService {

    @Resource
    private HighMerchantAccountMarketingMapper marketingMapper;

    @Override
    public void insert(HighMerchantAccountMarketing merchantAccountMarketing) {
        merchantAccountMarketing.setStatus(1);
        merchantAccountMarketing.setCreateTime(new Date());
        merchantAccountMarketing.setUpdateTime(new Date());
        marketingMapper.insert(merchantAccountMarketing);
    }

    @Override
    public void update(HighMerchantAccountMarketing merchantAccountMarketing) {
        merchantAccountMarketing.setUpdateTime(new Date());
        marketingMapper.updateByPrimaryKeySelective(merchantAccountMarketing);
    }

    @Override
    public HighMerchantAccountMarketing getDetailByMerAndType(Long merId, Integer type) {
        HighMerchantAccountMarketingExample example = new HighMerchantAccountMarketingExample();
        example.createCriteria().andMerIdEqualTo(merId).andTypeEqualTo(type).andStatusNotEqualTo(0);
        List<HighMerchantAccountMarketing> list = marketingMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BigDecimal getPrice(Long merId, Integer type, Integer oilNo) {
        HighMerchantAccountMarketing marketing = getDetailByMerAndType(merId, type);
        if (marketing == null) {
            return new BigDecimal("0");
        }
        if (type.equals(MerAccountMarketingType.type3.getType())) {
            JSONArray oilNoArray = JSONArray.parseArray(marketing.getOilNoReliefPrice());
            for (Object obj : oilNoArray) {
                JSONObject oilNoData = (JSONObject) obj;
                if (oilNo.equals(oilNoData.getInteger("oilNo"))) {
                    return oilNoData.getBigDecimal("price");
                }
            }
        }
        return new BigDecimal("0");
    }
}
