package com.hai.goods.service.impl;

import com.hai.config.HuiLianTongConfig;
import com.hai.dao.GoodsPresentMapper;
import com.hai.dao.GoodsPresentRecordMapper;
import com.hai.entity.*;
import com.hai.goods.service.GoodsPresentService;
import com.hai.service.HighCouponCodeService;
import com.hai.service.HighCouponService;
import com.hai.service.HighDiscountAgentCodeService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("goodsPresentService")
public class GoodsPresentServiceImpl implements GoodsPresentService {

    @Resource
    private GoodsPresentMapper goodsPresentMapper;

    @Resource
    private GoodsPresentRecordMapper goodsPresentRecordMapper;

    @Resource
    private GoodsPresentService goodsPresentService;

    @Resource
    private HighCouponService highCouponService;

    @Resource
    private HighCouponCodeService couponCodeService;

    @Resource
    private HighDiscountAgentCodeService highDiscountAgentCodeService;

    @Override
    public void insertPresent(GoodsPresent present) {
        goodsPresentMapper.insert(present);
    }

    @Override
    public void updatePresent(GoodsPresent present) {
        goodsPresentMapper.updateByPrimaryKey(present);
    }

    @Override
    public List<GoodsPresent> getPresentList(Map<String, Object> map) {
        GoodsPresentExample example = new GoodsPresentExample();
        GoodsPresentExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "goodsId") != null) {
            criteria.andGoodsIdEqualTo(MapUtils.getLong(map, "goodsId"));
        }
        criteria.andStatusNotEqualTo(String.valueOf(0));

        return goodsPresentMapper.selectByExample(example);
    }

    @Override
    public List<GoodsPresent> getPresentList(Long goodsId) {
        GoodsPresentExample example = new GoodsPresentExample();
        GoodsPresentExample.Criteria criteria = example.createCriteria();

        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andStatusNotEqualTo(String.valueOf(0));

        return goodsPresentMapper.selectByExample(example);
    }

    @Override
    public GoodsPresent findPresentById(Long id) {
        return goodsPresentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deletePresent(Long id) {
        goodsPresentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertPresentRecord(GoodsPresentRecord record) {
        goodsPresentRecordMapper.insert(record);
    }

    @Override
    public List<GoodsPresentRecord> getPresentRecord(Map<String, Object> map) {
        return null;
    }

    @Override
    public void presentUser(HighOrder order, HighChildOrder highChildOrder) {

        try {
            // 赠送卡券or优惠券
            List<GoodsPresent> goodsPresents = goodsPresentService.getPresentList(highChildOrder.getGoodsId());

            if (goodsPresents.size() > 0) {

                    for (GoodsPresent present : goodsPresents) {
                        GoodsPresentRecord presentRecord = new GoodsPresentRecord();
                        presentRecord.setStatus("0");
                        if (present.getType() == 1) {
                            HighCoupon coupon = highCouponService.getCouponById(present.getSourceId());
                            if  (coupon.getCouponSource() == 1 || coupon.getCouponSource() == 2 || coupon.getCouponSource() == 3)  {
                                for (int i = 0; i < present.getNum(); i++) {
                                    for (int j = 0; j < highChildOrder.getSaleCount(); j++) {
                                        // 支付购买卡券
                                        couponCodeService.payCoupon(order,highChildOrder,coupon);
                                    }
                                }
                            }
                            if  (coupon.getCouponSource() == 4 || coupon.getCouponSource() == 5)  {
                                for (int i = 0; i < present.getNum(); i++) {
                                        // 支付购买卡券
                                        couponCodeService.payCoupon(order,highChildOrder,coupon);
                                }
                            }
                        }
                        if (present.getType() == 2) {
                            highDiscountAgentCodeService.giveDiscount(present.getSourceId() , ((long) present.getNum() * highChildOrder.getSaleCount()), order.getMemId());
                        }

                        presentRecord.setOrderNo(order.getOrderNo());
                        presentRecord.setOrderId(order.getId());
                        presentRecord.setChildOrderId(highChildOrder.getId());
                        presentRecord.setCreateTime(new Date());
                        presentRecord.setNum(present.getNum()*highChildOrder.getSaleCount());
                        presentRecord.setUpdateTime(new Date());
                        presentRecord.setType(present.getType());
                        presentRecord.setStatus("1");
                        presentRecord.setSourceId(present.getSourceId());
                        presentRecord.setSourceName(present.getSourceName());
                        presentRecord.setOpName("系统赠送");
                        presentRecord.setPresentId(present.getId());

                        goodsPresentService.insertPresentRecord(presentRecord);
                    }
                }

        } catch (Exception e) {

        }
    }

    @Override
    public void preGiveUser(HighChildOrder childOrder) {

        try {

            List<GoodsPresent> goodsPresents = goodsPresentService.getPresentList(childOrder.getGoodsId());

            if (goodsPresents.size() > 0) {
                    for (GoodsPresent present : goodsPresents) {
                        if (present.getType() == 1) {
                            HighCoupon coupon = highCouponService.getCouponById(present.getSourceId());
                            if  (coupon.getCouponSource() == 1 || coupon.getCouponSource() == 2 || coupon.getCouponSource() == 3)  {
                                for (int i = 0; i < present.getNum(); i++) {
                                    for (int j = 0; j < childOrder.getSaleCount(); j++) {
                                        highCouponService.assignOrderStock(present.getSourceId(), childOrder.getChildOrderNo());
                                    }
                                }
                            }
                            if (coupon.getCouponSource() == 4) {
                                HuiLianTongConfig.recharge(childOrder.getChildOrderNo(),
                                        childOrder.getSaleCount()*present.getNum(),
                                        coupon.getCouponKey(),
                                        childOrder.getMemPhone());
                            }

                        }
                    }
                }

        } catch (Exception e) {

        }

    }
}
