package com.ssm.manage.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.AESUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmBaseGrouponCardDao;
import com.ssm.manage.dao.SsmBaseGrouponListDao;
import com.ssm.manage.entity.BaseGrouponCard;
import com.ssm.manage.entity.BaseGrouponList;
import com.ssm.manage.entity.RechargeType;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.BaseGrouponCardRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseGrouponCardService;
import com.ssm.manage.service.SsmBaseGrouponListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;


@Service
@Slf4j
public class SsmBaseGrouponListServiceImpl  extends BaseServiceImpl<BaseGrouponList> implements SsmBaseGrouponListService {

    @Autowired
    private SsmBaseGrouponListDao dao;

    @Autowired
    private SsmBaseGrouponCardService ssmBaseGrouponCardDao;

    @Override
    public SsmBaseGrouponListDao getDao() {
        return dao;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(BaseGrouponCardRequest baseGrouponCardRequest,Long groupId) throws SmsException {
        List<BaseGrouponList> baseGrouponLists = JSONArray.parseArray(baseGrouponCardRequest.getBaseGrouponLists(),BaseGrouponList.class);
        for (BaseGrouponList baseGrouponList : baseGrouponLists) {
            baseGrouponList.setGrouponCardId(baseGrouponCardRequest.getId());
            baseGrouponList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        }
        BaseGrouponCard baseGrouponCard = new BaseGrouponCard();
        baseGrouponCard.setId(groupId);
        BaseGrouponCard baseGrouponCard1 = ssmBaseGrouponCardDao.selectByPrimaryKey(baseGrouponCard.getId());
        Integer quantity = baseGrouponCard1.getQuantity();
        int i=1;
        if(quantity == baseGrouponLists.size()){
            int insert = dao.insertList(baseGrouponLists);
            return insert;
        }else{
            throw new SmsException(ResponseCode.MSG,"团购份数和团购名单不相等");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int update(BaseGrouponCardRequest baseGrouponCardRequest, String key) throws SmsException {
        List<BaseGrouponList> baseGrouponLists = JSONArray.parseArray(baseGrouponCardRequest.getBaseGrouponLists(),BaseGrouponList.class);
        for (BaseGrouponList baseGrouponList : baseGrouponLists) {
            baseGrouponList.setGrouponCardId(baseGrouponCardRequest.getId());
            baseGrouponList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        }
        int i =1;
        for (BaseGrouponList baseGrouponList : baseGrouponLists) {
            int update = dao.updateByPrimaryKeySelective(baseGrouponList);
            if(update==0){
                i=0;
            }
        }
        if(i>0){
            return i;
        }else{
            throw new SmsException(ResponseCode.MSG,"更新团购人名单异常");
        }
    }

    @Override
    public List<BaseGrouponList> insertBatch(List<BaseGrouponList> list, Long grouponCardId) throws SmsException {
        List<BaseGrouponList> unusualList = this.check(list);
        if (!unusualList.isEmpty()){
            return unusualList;
        }
        BaseGrouponCard baseGrouponCard = new BaseGrouponCard();
        baseGrouponCard.setId(grouponCardId);
        BaseGrouponCard baseGrouponCard1 = ssmBaseGrouponCardDao.selectByPrimaryKey(baseGrouponCard.getId());
        Integer quantity = baseGrouponCard1.getQuantity();
        int i=1;
        if(quantity == list.size()){
            for (BaseGrouponList baseGrouponList : list) {
                BaseGrouponList baseGrouponList1 = new BaseGrouponList();
                baseGrouponList1.setGrouponCardId(grouponCardId);
                baseGrouponList1.setMobilePhone(AESUtils.encrypt(baseGrouponList.getMobilePhone()));
                List<BaseGrouponList> select = dao.select(baseGrouponList1);
                if(select.size()>0){//更新
                    BaseGrouponList baseGrouponList2 = select.get(0);
                    baseGrouponList2.setMemberName(baseGrouponList.getMemberName());
                    int update = dao.updateByPrimaryKeySelective(baseGrouponList2);
                }else{
                    baseGrouponList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                    baseGrouponList.setMobilePhone(AESUtils.encrypt(baseGrouponList.getMobilePhone()));
                    baseGrouponList.setGrouponCardId(grouponCardId);
                    int insert = dao.insert(baseGrouponList);
                }
            }
        }else{
            throw new SmsException(ResponseCode.MSG,"团购份数和团购名单不相等");
        }
        return unusualList;
    }

    @Override
    public int insertBatch(List<BaseGrouponList> list) throws SmsException {
        int i = dao.insertList(list);
        return i;
    }


    public List<BaseGrouponList> check(List<BaseGrouponList> list){
        List<BaseGrouponList> unusualList = new ArrayList<>();
        Map<String, List<BaseGrouponList>> map = list.stream().collect(Collectors.groupingBy(baseGrouponList -> baseGrouponList.getMobilePhone()));
        for (String key : map.keySet()) {
            if (map.get(key).size() > 1){
                unusualList.addAll(map.get(key));
                list.removeAll(map.get(key));
            }
        }
        return unusualList;
    }
}
