package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmPromotionProjectDao;
import com.ssm.manage.entity.PromotionList;
import com.ssm.manage.entity.PromotionProject;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.mapper.PromotionProjectMapper;
import com.ssm.manage.query.PromotionListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmPromotionProjectService;
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 tk.mybatis.mapper.entity.Example;

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

@Service
@Slf4j
public class SsmPromotionProjectServiceImpl extends BaseServiceImpl<PromotionProject> implements SsmPromotionProjectService {
    @Autowired
    private SsmPromotionProjectDao dao;

    @Autowired
    private PromotionProjectMapper mapper;

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

    @Override
    public List<PromotionProject> selectList(PromotionListRequest promotionListRequest) {
        PromotionProject promotionProject = new PromotionProject();
        promotionProject.setPromotionId(promotionListRequest.getId());
        promotionProject.setStoreId(promotionListRequest.getStoreId());
        return dao.select(promotionProject);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addPromotionProject(List<PromotionProject> list, PromotionList promotionList) throws SmsException {
        int i = 0;
        for (PromotionProject promotionProject : list) {
            promotionProject.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            promotionProject.setPromotionId(promotionList.getId());
            promotionProject.setPromotionNum(promotionList.getPromotionNum());

            promotionProject.setStoreId(promotionList.getStoreId());
            promotionProject.setStoreName(promotionList.getStoreName());
            promotionProject.setStandardMoney(BigDecimalUtils.multiply(promotionProject.getPriceMember(),BigDecimalUtils.bigDecimal(promotionProject.getTimes().toString())));
            try {
                i = dao.saveNotNull(promotionProject);
            } catch (Exception e) {
                log.error("SsmPromotionProjectServiceImpl.addPromotionProject:", e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            }
        }
        return i;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updatePromotionProject(List<PromotionProject> list, PromotionList promotionList) throws SmsException {
        int i = 0;
        PromotionProject promotionProject = new PromotionProject();
        promotionProject.setPromotionId(promotionList.getId());
        //先删除原来的项目
        dao.delete(promotionProject);
        //添加
        i = this.addPromotionProject(list,promotionList);

        return i;
    }

    @Override
    public void deleteByPromotionId(Long id) {
        mapper.deleteByPromotionId(id);
    }

    @Override
    public List<PromotionProject> selectByPromotionIds(List<Long> promotionIds) {
        Example example = new Example(PromotionProject.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("promotionId",promotionIds);
        return dao.selectByExample(example);
    }
}
