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.BsDiscountStockCodeMapper;
import com.hfkj.entity.BsDiscountStockCode;
import com.hfkj.entity.BsDiscountStockCodeExample;
import com.hfkj.entity.BsDiscountUser;
import com.hfkj.service.discount.BsDiscountStockCodeService;
import com.hfkj.service.discount.BsDiscountUserService;
import com.hfkj.sysenum.discount.DiscountStockCodeStatusEnum;
import com.hfkj.sysenum.discount.DiscountUserStatusEnum;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsDiscountCodeServiceImpl
 * @author: HuRui
 * @date: 2024/3/18
 **/
@Service("discountCodeService")
public class BsDiscountStockCodeServiceImpl implements BsDiscountStockCodeService {

    @Resource
    private BsDiscountStockCodeMapper discountStockCodeMapper;
    @Resource
    private BsDiscountUserService discountUserService;
    @Override
    public void insertList(List<BsDiscountStockCode> codeList) {
        discountStockCodeMapper.insertList(codeList);
    }

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

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor= {RuntimeException.class})
    public void recycle(Long stockCodeId) {
        // 查询库存记录
        BsDiscountStockCode stockCode = getDetail(stockCodeId);
        if (stockCode == null) {
            throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "位置优惠券库存id");
        }
        if (stockCode.getStatus().equals(DiscountStockCodeStatusEnum.status2.getCode())) {
            // 退回优惠券库存
            stockCode.setObtainType(null);
            stockCode.setObtainTime(null);
            stockCode.setReceiveMerUserId(null);
            stockCode.setReceiveMerUserPhone(null);
            stockCode.setDiscountPkStockCode(null);
            stockCode.setStatus(DiscountStockCodeStatusEnum.status1.getCode());
            stockCode.setAgentAppid(null);
            update(stockCode);

            // 用户优惠券
            BsDiscountUser discountUser = discountUserService.getDetailByCodeId(stockCodeId);
            if (discountUser != null) {
                discountUser.setStatus(DiscountUserStatusEnum.type4.getCode());
                discountUserService.editData(discountUser);
            }
        }
    }

    @Override
    public BsDiscountStockCode getDetail(Long id) {
        return discountStockCodeMapper.selectByPrimaryKey(id);
    }

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

        BsDiscountStockCodeExample example = new BsDiscountStockCodeExample();
        example.createCriteria().andDiscountNoEqualTo(discountNo).andStatusEqualTo(DiscountStockCodeStatusEnum.status1.getCode());
        example.setOrderByClause("create_time");

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

    @Override
    public List<BsDiscountStockCode> getCodeListByPkStockId(Long discountPkStockId) {
        BsDiscountStockCodeExample example = new BsDiscountStockCodeExample();
        example.createCriteria()
                .andStatusNotEqualTo(DiscountStockCodeStatusEnum.status0.getCode())
                .andDiscountPkStockCodeEqualTo(discountPkStockId);
        example.setOrderByClause("id desc");
        return discountStockCodeMapper.selectByExample(example);
    }

    @Override
    public List<BsDiscountStockCode> getCodeList(Map<String, Object> param) {
        BsDiscountStockCodeExample example = new BsDiscountStockCodeExample();
        BsDiscountStockCodeExample.Criteria criteria = example.createCriteria()
                .andStatusNotEqualTo(DiscountStockCodeStatusEnum.status0.getCode());

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "batchNo"))) {
            criteria.andDiscountStockBatchNoLike("%"+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("id desc");
        return discountStockCodeMapper.selectByExample(example);
    }
}
