package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.entity.PromotionUnit;
import com.ssm.manage.dao.SsmPromotionUnitDao;
import com.ssm.manage.query.PromotionUnitRequest;
import com.ssm.manage.service.SsmPromotionUnitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.rmi.ServerException;
import java.util.List;

/**
 * (PromotionUnit)表服务实现类
 *
 * @author makejava
 * @since 2021-12-24 10:00:52
 */
@Service
public class SsmPromotionUnitServiceImpl extends BaseServiceImpl<PromotionUnit> implements SsmPromotionUnitService {

    @Autowired
    private SsmPromotionUnitDao dao;

    @Override
    public BaseDao<PromotionUnit> getDao() {
        return dao;
    }

    @Override
    @Transactional
    public int add(PromotionUnitRequest promotionUnitRequest) throws ServerException {
        int status = 1;
        List<PromotionUnit> promotionUnits = promotionUnitRequest.getPromotionUnits();
        for (PromotionUnit promotionUnit : promotionUnits) {
            promotionUnit.setPromotionNum(promotionUnitRequest.getPromotionNum());
            promotionUnit.setPromotionName(promotionUnitRequest.getPromotionName());
            if(null == promotionUnit.getStoreId()){
                throw new RuntimeException("数据存在门店编码为空");
            }
            if(null == promotionUnit.getPromotionNum()){
                throw new RuntimeException("数据存在促销编码为空");
            }
            int insert = dao.insert(promotionUnit);
            if(insert>0){

            }else{
                status = 0;
            }
        }
        return status;
    }

    @Override
    @Transactional
    public int update(PromotionUnitRequest promotionUnitRequest) throws ServerException {
        if(null == promotionUnitRequest.getStoreId()){
            throw new RuntimeException("数据存在门店编码为空");
        }
        if(null == promotionUnitRequest.getPromotionNum()){
            throw new RuntimeException("数据存在促销编码为空");
        }
        PromotionUnit promotionUnit = new PromotionUnit();
        BeanUtils.copyProperties(promotionUnitRequest, promotionUnit);
        return dao.update(promotionUnit);

    }

    @Override
    @Transactional
    public int empty(PromotionUnitRequest promotionUnitRequest){
        if(null == promotionUnitRequest.getStoreId()){
            throw new RuntimeException("数据存在门店编码为空");
        }
        if(null == promotionUnitRequest.getPromotionNum()){
            throw new RuntimeException("数据存在促销编码为空");
        }
        PromotionUnit promotionUnit = new PromotionUnit();
        BeanUtils.copyProperties(promotionUnitRequest, promotionUnit);
        return dao.empty(promotionUnit);

    }

    @Override
    @Transactional
    public int insertupdat(PromotionUnitRequest promotionUnitRequest){
        int status = 1;
        List<PromotionUnit> promotionUnits = promotionUnitRequest.getPromotionUnits();
        for (PromotionUnit promotionUnit : promotionUnits) {
            promotionUnit.setPromotionNum(promotionUnitRequest.getPromotionNum());
            promotionUnit.setPromotionName(promotionUnitRequest.getPromotionName());
            if(null == promotionUnit.getStoreId()){
                throw new RuntimeException("数据存在门店编码为空");
            }
            if(null == promotionUnit.getPromotionNum()){
                throw new RuntimeException("数据存在促销编码为空");
            }
            int insertupdat = 0;
            if(null == promotionUnit.getPromotionId()){
                insertupdat = dao.insert(promotionUnit);
            }else{
                insertupdat = dao.update(promotionUnit);
            }

            if(insertupdat>0){

            }else{
                status = 0;
            }
        }
        return status;
    }

    @Override
    public List<Long> selectallId(PromotionUnit promotionUnit) {
        return dao.selectallId(promotionUnit);
    }


}
