package com.hfkj.service.discount.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsDiscountStockBatchMapper;
import com.hfkj.entity.BsDiscount;
import com.hfkj.entity.BsDiscountStockBatch;
import com.hfkj.entity.BsDiscountStockBatchExample;
import com.hfkj.entity.BsDiscountStockCode;
import com.hfkj.service.discount.BsDiscountService;
import com.hfkj.service.discount.BsDiscountStockBatchService;
import com.hfkj.service.discount.BsDiscountStockCodeService;
import com.hfkj.sysenum.discount.DiscountStockCodeStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @className: BsDiscountStockBatchServiceImpl
 * @author: HuRui
 * @date: 2024/3/19
 **/
@Service("discountStockBatchService")
public class BsDiscountStockBatchServiceImpl implements BsDiscountStockBatchService {

    @Resource
    private BsDiscountStockBatchMapper discountStockBatchMapper;
    @Resource
    private BsDiscountStockCodeService discountStockCodeService;
    @Resource
    private BsDiscountService discountService;

    @Override
    public void editData(BsDiscountStockBatch data) {
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setUpdateTime(new Date());
            discountStockBatchMapper.insert(data);

            data.setBatchNo(""+System.currentTimeMillis());
            discountStockBatchMapper.updateByPrimaryKey(data);
        } else {
            data.setUpdateTime(new Date());
            discountStockBatchMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void addStock(String discountNo, Integer stockCount) {
        // 查询优惠券
        BsDiscount discount = discountService.getDetail(discountNo);
        if (discount == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券");
        }
        BsDiscountStockBatch batch = new BsDiscountStockBatch();
        batch.setDiscountId(discount.getId());
        batch.setDiscountNo(discount.getDiscountNo());
        batch.setDiscountName(discount.getDiscountName());
        batch.setBatchStockNum(stockCount);
        batch.setStatus(1);
        editData(batch);

        List<BsDiscountStockCode> stockCodeList = new ArrayList<>();
        BsDiscountStockCode discountCode;
        for (int i = 0;i < stockCount;i++) {
            discountCode = new BsDiscountStockCode();
            discountCode.setDiscountStockBatchId(batch.getId());
            discountCode.setDiscountStockBatchNo(batch.getBatchNo());
            discountCode.setDiscountId(discount.getId());
            discountCode.setDiscountNo(discount.getDiscountNo());
            discountCode.setDiscountName(discount.getDiscountName());
            discountCode.setStatus(DiscountStockCodeStatusEnum.status1.getCode());
            discountCode.setCreateTime(new Date());
            discountCode.setUpdateTime(new Date());
            stockCodeList.add(discountCode);
        }
        // 批量新增
        discountStockCodeService.insertList(stockCodeList);

        batch.setStartId(""+stockCodeList.get(0).getId());
        batch.setEndId(""+stockCodeList.get(stockCount-1).getId());
        editData(batch);
    }

    @Override
    public BsDiscountStockBatch getStockBatchDetail(String batchNo) {
        BsDiscountStockBatchExample example = new BsDiscountStockBatchExample();
        example.createCriteria().andBatchNoEqualTo(batchNo).andStatusNotEqualTo(0);
        List<BsDiscountStockBatch> list = discountStockBatchMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsDiscountStockBatch> getStockBatchList(Map<String, Object> param) {
        BsDiscountStockBatchExample example = new BsDiscountStockBatchExample();
        BsDiscountStockBatchExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (StringUtils.isNotBlank(MapUtils.getString(param, "discountNo"))) {
            criteria.andDiscountNoLike("%"+MapUtils.getString(param, "discountNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "discountName"))) {
            criteria.andDiscountNameLike("%"+MapUtils.getString(param, "discountName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "batchNo"))) {
            criteria.andBatchNoLike("%"+MapUtils.getString(param, "batchNo")+"%");
        }

        example.setOrderByClause("create_time desc");
        return discountStockBatchMapper.selectByExample(example);
    }

}
