package com.hai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.DateUtil;
import com.hai.dao.HighCouponAgentCodeMapper;
import com.hai.dao.HighCouponCodeOtherMapper;
import com.hai.dao.HighUserCouponMapper;
import com.hai.entity.*;
import com.hai.service.HighCouponCodeOtherService;
import com.hai.service.HighDiscountAgentCodeService;
import com.hai.service.HighDiscountUserRelService;
import com.hai.service.HighOrderService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

@Service("highCouponCodeOtherService")
public class HighCouponCodeOtherServiceImpl implements HighCouponCodeOtherService {

    @Resource
    private HighCouponCodeOtherMapper highCouponCodeOtherMapper;

    @Resource
    private HighUserCouponMapper highUserCouponMapper;

    @Resource
    private HighCouponAgentCodeMapper highCouponAgentCodeMapper;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighDiscountAgentCodeService discountAgentCodeService;

    @Resource
    private HighDiscountUserRelService discountUserRelService;

    @Override
    public void update(HighCouponCodeOther couponCodeOther) {
        highCouponCodeOtherMapper.updateByPrimaryKey(couponCodeOther);
    }

    @Override
    public void insertCouponCodeOther(HighCouponCodeOther couponCodeOther) {
        highCouponCodeOtherMapper.insert(couponCodeOther);
    }

    @Override
    public List<HighCouponCodeOther> getList(Map<String, Object> map) {
        HighCouponCodeOtherExample example = new HighCouponCodeOtherExample();
        HighCouponCodeOtherExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map,"type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map,"type"));
        }

        if (MapUtils.getLong(map,"orderId") != null) {
            criteria.andOrderIdEqualTo(MapUtils.getLong(map,"orderId"));
        }

        if (MapUtils.getInteger(map,"childOrderId") != null) {
            criteria.andChildOrderIdEqualTo(MapUtils.getLong(map,"childOrderId"));
        }
        return highCouponCodeOtherMapper.selectByExample(example);
    }

    @Override
    public HighCouponCodeOther getDetailByCouponAgentCodeId(Long couponAgentCodeId) {
        HighCouponCodeOtherExample example = new HighCouponCodeOtherExample();
        example.createCriteria().andCouponAgentCodeIdEqualTo(couponAgentCodeId);
        List<HighCouponCodeOther> list = highCouponCodeOtherMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public HighCouponCodeOther getDetailByCouNo(String couNo) {
        HighCouponCodeOtherExample example = new HighCouponCodeOtherExample();
        example.createCriteria().andCouNoEqualTo(couNo);
        List<HighCouponCodeOther> list = highCouponCodeOtherMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void cqCnpcUseNotify(JSONObject param) {
        HighCouponCodeOther couponCodeOther = getDetailByCouNo(param.getString("couponNo"));
        if (couponCodeOther != null) {
            couponCodeOther.setUseTime(DateUtil.format(param.getString("useTime"), "yyyyMMddHHmmss"));
            couponCodeOther.setStationCode(param.getString("stationCode"));
            couponCodeOther.setStationName(param.getString("stationName"));
            couponCodeOther.setStatus(40);
            update(couponCodeOther);

            if (couponCodeOther.getChildOrderId() != null) {
                HighUserCouponExample userCouponExample = new HighUserCouponExample();
                userCouponExample.createCriteria().andQrCodeImgEqualTo(couponCodeOther.getCouNo()).andStatusEqualTo(1);
                List<HighUserCoupon> list = highUserCouponMapper.selectByExample(userCouponExample);
                if (list.size() > 0) {
                    HighUserCoupon userCoupon = list.get(0);
                    userCoupon.setStatus(2);
                    userCoupon.setConsumeTime(couponCodeOther.getUseTime());
                    highUserCouponMapper.updateByPrimaryKey(userCoupon);
                }
                highOrderService.childOrderComplete(couponCodeOther.getChildOrderId());
            }

            if (couponCodeOther.getCouponAgentCodeId() != null) {
                HighCouponAgentCode agentCode = highCouponAgentCodeMapper.selectByPrimaryKey(couponCodeOther.getCouponAgentCodeId());
                if (agentCode != null) {
                    agentCode.setStatus(3);
                    highCouponAgentCodeMapper.updateByPrimaryKey(agentCode);
                }
            }

            if (couponCodeOther.getDiscountAgentCodeId() != null) {
                HighDiscountAgentCode discountAgentCode = discountAgentCodeService.getCodeById(couponCodeOther.getDiscountAgentCodeId());
                if (discountAgentCode != null) {
                    discountAgentCode.setStatus(3);
                    discountAgentCodeService.updateCode(discountAgentCode);

                    HighDiscountUserRel discountUserRel = discountUserRelService.getRelByAgentCodeId(couponCodeOther.getDiscountAgentCodeId());
                    if (discountUserRel != null) {
                        discountUserRel.setStatus(2);
                        discountUserRel.setUseTime(couponCodeOther.getUseTime());
                        discountUserRelService.updateDiscountUserRel(discountUserRel);
                    }
                }
            }
        }
    }
}
