package com.hai.service.impl;

import com.hai.dao.BsProductDiscountMapper;
import com.hai.dao.BsProductPayTypeMapper;
import com.hai.dao.BsProductPlatformMapper;
import com.hai.entity.*;
import com.hai.service.BsConfigService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service(value = "bsConfigService")
public class BsConfigServiceImpl implements BsConfigService {

    @Resource
    private BsProductDiscountMapper bsProductDiscountMapper;

    @Resource
    private BsProductPayTypeMapper bsProductPayTypeMapper;

    @Resource
    private BsProductPlatformMapper bsProductPlatformMapper;

    @Override
    public BsProductDiscount getProductDiscountByMap(Map<String, Object> map) {
        BsProductDiscountExample example = new BsProductDiscountExample();
        BsProductDiscountExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map , "productType") != null) {
            criteria.andProductTypeEqualTo(MapUtils.getInteger(map , "productType"));
        }

        if (MapUtils.getLong(map , "sourceId") != null) {
            criteria.andSourceIdEqualTo(MapUtils.getLong(map , "sourceId"));
        }

        if (bsProductDiscountMapper.selectByExample(example) == null) {
            return null;
        }

        return bsProductDiscountMapper.selectByExample(example).get(0);
    }

    @Override
    public String getProductPlatformByMap(Map<String, Object> map) {

        BsProductPlatformExample example = new BsProductPlatformExample();
        BsProductPlatformExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map , "productType") != null) {
            criteria.andProductTypeEqualTo(MapUtils.getInteger(map , "productType"));
        }

        if (MapUtils.getLong(map , "sourceId") != null) {
            criteria.andSourceIdEqualTo(MapUtils.getLong(map , "sourceId"));
        }

        List<BsProductPlatform> list = bsProductPlatformMapper.selectByExample(example);

        StringBuilder string = new StringBuilder();
        StringBuilder stringName = new StringBuilder();

        for (BsProductPlatform productPlatform : list) {
            stringName.append(productPlatform.getPlatformName()).append(",");
            string.append(productPlatform.getPlatformId()).append(",");
        }
        if (MapUtils.getInteger(map , "returnType") == 1) {
            return string.toString();
        } else {
            return stringName.toString();
        }
    }

    @Override
    public String getProductPayTypeByMap(Map<String, Object> map) {
        BsProductPayTypeExample example = new BsProductPayTypeExample();
        BsProductPayTypeExample.Criteria criteria = example.createCriteria();
        Integer platformId = MapUtils.getInteger(map , "platformId");
        if (MapUtils.getInteger(map , "productType") != null) {
            criteria.andProductTypeEqualTo(MapUtils.getInteger(map , "productType"));
        }

        if (MapUtils.getLong(map , "sourceId") != null) {
            criteria.andSourceIdEqualTo(MapUtils.getLong(map , "sourceId"));
        }

        List<BsProductPayType> list =  bsProductPayTypeMapper.selectByExample(example);

        StringBuilder stringName = new StringBuilder();
        StringBuilder string = new StringBuilder();

        //  1：支付宝  2：微信  3：汇联通工会卡  4：银联   5：银联分期
        for (BsProductPayType productPayType : list) {
            if (platformId != null) {
                if (platformId == 1 || platformId == 2) {
                    if (productPayType.getPayTypeId() == 2 || productPayType.getPayTypeId() == 3) {
                        stringName.append(productPayType.getPayTypeName()).append(",");
                        string.append(productPayType.getPayTypeId()).append(",");
                    }
                } else if (platformId == 3) {
                    if (productPayType.getPayTypeId() == 4 || productPayType.getPayTypeId() == 5) {
                        stringName.append(productPayType.getPayTypeName()).append(",");
                        string.append(productPayType.getPayTypeId()).append(",");
                    }
                }
            } else {
                stringName.append(productPayType.getPayTypeName()).append(",");
                string.append(productPayType.getPayTypeId()).append(",");
            }

        }

        if (MapUtils.getInteger(map , "returnType") == 1) {
            return string.toString();
        } else {
            return stringName.toString();
        }

    }
}
