package com.hfkj.service.discount.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsDiscountPkStockCodeMapper;
import com.hfkj.entity.BsDiscountPkStockCode;
import com.hfkj.entity.BsDiscountPkStockCodeExample;
import com.hfkj.entity.BsDiscountStockCode;
import com.hfkj.entity.BsDiscountStockCodeExample;
import com.hfkj.service.discount.BsDiscountPkStockCodeService;
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 javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsDiscountPkStockCodeServiceImpl
 * @author: HuRui
 * @date: 2024/9/2
 **/
@Service("discountPkStockCodeService")
public class BsDiscountPkStockCodeServiceImpl implements BsDiscountPkStockCodeService {
    @Resource
    private BsDiscountPkStockCodeMapper discountPkStockCodeMapper;

    @Override
    public void insertList(List<BsDiscountPkStockCode> codeList) {
        discountPkStockCodeMapper.insertList(codeList);
    }

    @Override
    public void update(BsDiscountPkStockCode data) {
        data.setUpdateTime(new Date());
        discountPkStockCodeMapper.updateByPrimaryKey(data);
    }

    @Override
    public List<BsDiscountPkStockCode> getAssignCodeList(String discountPkNo, Integer number) {
        // 分页
        PageHelper.startPage(1,number);

        BsDiscountPkStockCodeExample example = new BsDiscountPkStockCodeExample();
        example.createCriteria().andDiscountPkNoEqualTo(discountPkNo).andStatusEqualTo(DiscountStockCodeStatusEnum.status1.getCode());
        example.setOrderByClause("create_time");

        PageInfo<BsDiscountPkStockCode> pageInfo = new PageInfo<>(discountPkStockCodeMapper.selectByExample(example));
        if (pageInfo.getList().size() != number) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券包库存不足");
        }
        return pageInfo.getList();
    }

    @Override
    public BsDiscountPkStockCode getDetail(Long stockId) {
        return discountPkStockCodeMapper.selectByPrimaryKey(stockId);
    }

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

        if (MapUtils.getLong(param, "codeId") != null) {
            criteria.andIdEqualTo(MapUtils.getLong(param, "codeId"));
        }

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

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

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

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

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