package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmPromotionMemberDao;
import com.ssm.manage.entity.PromotionList;
import com.ssm.manage.entity.PromotionMember;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.PromotionListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmPromotionMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class SsmPromotionMemberServiceImpl extends BaseServiceImpl<PromotionMember> implements SsmPromotionMemberService {
    @Autowired
    private SsmPromotionMemberDao dao;

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

    @Override
    public PageInfo<PromotionMember> selectPage(PromotionListRequest promotionListRequest) {
        PromotionMember promotionMember = new PromotionMember();
        promotionMember.setPromotionId(promotionListRequest.getId());
        promotionMember.setStoreId(promotionListRequest.getStoreId());
        List<PromotionMember> list = dao.select(promotionMember);
        PageInfo<PromotionMember> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addPromotionMember(List<PromotionMember> list, PromotionList promotionList) throws SmsException {
        int i = 0;
        for (PromotionMember promotionMember : list) {
            //System.out.println("-----------"+promotionMember.getMobilePhone().trim());
            promotionMember.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            promotionMember.setPromotionId(promotionList.getId());
            promotionMember.setPromotionNum(promotionList.getPromotionNum());
            promotionMember.setPromotionName(promotionList.getPromotionName());
            promotionMember.setMobilePhone(promotionMember.getMobilePhone().trim());
            promotionMember.setStoreId(promotionList.getStoreId());
            promotionMember.setStoreName(promotionList.getStoreName());
            try {
                i = dao.saveNotNull(promotionMember);
            }catch (Exception e){
                log.error("SsmPromotionMemberServiceImpl.addPromotionMember:", e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            }
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updatePromotionMember(List<PromotionMember> list, PromotionList promotionList) throws SmsException {
        int i = 0;
        //先删除
        PromotionMember promotionMember = new PromotionMember();
        promotionMember.setPromotionId(promotionList.getId());
        dao.delete(promotionMember);
        //在添加
        i = this.addPromotionMember(list,promotionList);
        return 0;
    }
}
