package com.neusoft.smart.pos.card.persistent.repository;

import com.neusoft.smart.pos.card.persistent.domain.CardShopRelation;
import com.neusoft.smart.pos.card.persistent.domain.CardShopRelationExample;
import com.neusoft.smart.pos.card.persistent.mapper.CardShopRelationExtMapper;
import com.neusoft.smart.pos.card.persistent.mapper.CardShopRelationMapper;
import com.neusoft.smart.pos.card.service.RemoteOrgService;
import com.neusoft.smart.pos.constants.EntityTableConstant;
import com.neusoft.smart.pos.dto.org.response.PostOrgGroupResponse;
import com.neusoft.smart.pos.exchangeStrategy.persistent.domain.ExchangeStrategy;
import com.neusoft.smart.pos.exchangeStrategy.persistent.domain.ExchangeStrategyExample;
import com.neusoft.smart.pos.exchangeStrategy.persistent.mapper.ExchangeStrategyMapper;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.member.constants.MemberErrorCode;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("defaultCardRelationRepository")
public class DefaultCardRelationRepository implements ICardRelationRepository{

    @Autowired
    private RemoteOrgService remoteOrgService;

    @Resource
    private CardShopRelationExtMapper cardShopRelationExtMapper;

    @Resource
    private CardShopRelationMapper cardShopRelationMapper;

    @Resource
    private ExchangeStrategyMapper exchangeStrategyMapper;

    @Override
    public void createCardShopRelation(Integer cardId, Integer orgId, Integer shopId) {
        val cardRelation = new CardShopRelation();
        cardRelation.setCardId(cardId);
        ResponseData<List<PostOrgGroupResponse>> parents = null;
        if(shopId >= 0) {
            parents = remoteOrgService.getOrgGroupParents(EntityTableConstant.ENTITY_TABLE_SHOP.getCode(), shopId);
        }
        if(null == parents || null == parents.getResult() || parents.getResult().size() == 0) {
            if( orgId >= 0 ) {
                parents = remoteOrgService.getOrgGroupParents(EntityTableConstant.ENTITY_TABLE_ORG.getCode(), orgId);
            }
        }

        if(null != parents && null != parents.getResult() && parents.getResult().size() > 0) {
            if(parents.getResult().size() > 1) {
                throw new BusinessCommonException(MemberErrorCode.BUSINESS_CODE_NOT_EXISTS);
            }
            cardRelation.setStoreId(parents.getResult().get(0).getId());
        }

        if(cardRelation.getStoreId() == null || cardRelation.getStoreId() < 0) {
            throw new BusinessCommonException(MemberErrorCode.NO_ORG_PROVIDED);
        }

        cardShopRelationMapper.insert(cardRelation);
    }

    @Override
    public List<CardShopRelation> getCardShopRelation(Integer cardId) {
        CardShopRelationExample example = new CardShopRelationExample();
        example.createCriteria().andCardIdEqualTo(cardId);
        return cardShopRelationMapper.selectByExample(example);
    }

    @Override
    public void createExchangeStrategy(Integer cardId, Integer strategyTypeId, String strategyCategory, String params) {
        ExchangeStrategy exchangeStrategy = new ExchangeStrategy();
        exchangeStrategy.setCardId(cardId);
        exchangeStrategy.setStrategyTypeId(strategyTypeId);
        exchangeStrategy.setStrategyCategory(strategyCategory);
        exchangeStrategy.setParams(params);
        exchangeStrategyMapper.insert(exchangeStrategy);
    }

    public void insertExchangeStrategy(Integer cardId, Integer strategyTypeId, String strategyCategory, List<Double> params) {
        ExchangeStrategy exchangeStrategy = new ExchangeStrategy();
        exchangeStrategy.setCardId(cardId);
        exchangeStrategy.setStrategyTypeId(strategyTypeId);
        exchangeStrategy.setStrategyCategory(strategyCategory);
        String p = "";
        for (Double param:params) {
            p = p.concat(param+"");
            p = p.concat(";");
        }
        p = p.substring(0,p.length()-2);
        exchangeStrategy.setParams(p);
        exchangeStrategyMapper.insert(exchangeStrategy);
    }

    @Override
    public ExchangeStrategy getExchangeStrategy(Integer cardId, String strategyCategory) {
        ExchangeStrategyExample example = new ExchangeStrategyExample();
        example.createCriteria().andCardIdEqualTo(cardId).andStrategyCategoryEqualTo(strategyCategory);
        return exchangeStrategyMapper.selectByExample(example).stream().findAny().orElse(null);
    }

    @Override
    public int updateCardShopRelation(Integer oldCardId, Integer newCardId) {
        CardShopRelationExample cardShopRelationExample = new CardShopRelationExample();
        cardShopRelationExample.createCriteria().andCardIdEqualTo(oldCardId);
        CardShopRelation cardShopRelation = new CardShopRelation();
        cardShopRelation.setCardId(newCardId);
        return cardShopRelationMapper.updateByExample(cardShopRelation,cardShopRelationExample);
    }

    @Override
    public int updateExchangeStrategy(Integer oldCardId, Integer newCardId) {
        ExchangeStrategyExample example = new ExchangeStrategyExample();
        example.createCriteria().andCardIdEqualTo(oldCardId);
        ExchangeStrategy exchangeStrategy = new ExchangeStrategy();
        exchangeStrategy.setCardId(newCardId);
        return exchangeStrategyMapper.updateByExample(exchangeStrategy,example);
    }
}
