package com.hfkj.service.discount.impl;

import com.hfkj.dao.CouponDiscountGoodsRelMapper;
import com.hfkj.dao.CouponDiscountMapper;
import com.hfkj.entity.*;
import com.hfkj.service.discount.CouponDiscountService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service("couponDiscountService")
public class CouponDiscountServiceImpl implements CouponDiscountService {

    @Resource
    private CouponDiscountMapper couponDiscountMapper;

    @Resource
    private CouponDiscountGoodsRelMapper couponDiscountGoodsRelMapper;

    @Override
    public void create(CouponDiscount couponDiscount) {
        couponDiscountMapper.insert(couponDiscount);
    }

    @Override
    public void update(CouponDiscount couponDiscount) {
        couponDiscountMapper.updateByPrimaryKeySelective(couponDiscount);
    }

    @Override
    public void delete(Long id, Boolean fullDelete) {
        if (fullDelete) {
            couponDiscountMapper.deleteByPrimaryKey(id);
        } else {
            CouponDiscount couponDiscount = queryDetail(id);
            couponDiscount.setStatus(0);
            couponDiscount.setUpdateTime(new Date());
            update(couponDiscount);
        }
    }

    @Override
    public CouponDiscount queryDetail(Long id) {
        return couponDiscountMapper.selectByPrimaryKey(id);
    }

    @Override
    public CouponDiscount queryDetailByMap(Map<String, Object> map) {
        return null;
    }

    @Override
    public List<CouponDiscount> getList(Map<String, Object> map) {

        CouponDiscountExample example = new CouponDiscountExample();
        CouponDiscountExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map, "type"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "name"))) {
            criteria.andNameLike("%"+MapUtils.getString(map, "name")+"%");
        }
        if (MapUtils.getString(map, "platformCode") != null) {
            criteria.andPlatformCodeEqualTo(MapUtils.getString(map, "platformCode"));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else {
            criteria.andStatusNotEqualTo(0);
        }

        return couponDiscountMapper.selectByExample(example);
    }

    @Override
    public List<CouponDiscountGoodsRel> getListGoodsRel(Map<String, Object> map) {
        CouponDiscountGoodsRelExample example = new CouponDiscountGoodsRelExample();
        CouponDiscountGoodsRelExample.Criteria criteria = example.createCriteria();

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

        if (StringUtils.isNotBlank(MapUtils.getString(map, "specsIds"))) {
            String[] specsIds = MapUtils.getString(map, "specsIds").split(",");
            List<Long> list = new ArrayList<>();
            for (String specsId : specsIds) {
                list.add(Long.valueOf(specsId));
            }
            criteria.andSpecsIdIn(list);
        }

        return couponDiscountGoodsRelMapper.selectByExample(example);
    }

    @Override
    public void createGoodsRel(CouponDiscountGoodsRel couponDiscountGoodsRel) {
        couponDiscountGoodsRelMapper.insert(couponDiscountGoodsRel);
    }

    @Override
    public void deleteDiscountGoodsRel(Long id) {
        couponDiscountGoodsRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertGoodsRelBatch(List<CouponDiscountGoodsRel> list) {
        couponDiscountGoodsRelMapper.insertGoodsRelBatch(list);
    }

    @Override
    public void deleteDiscountGoodsRelDiscountId(Long discountId) {
        CouponDiscountGoodsRelExample example = new CouponDiscountGoodsRelExample();
        example.createCriteria().andDiscountIdEqualTo(discountId);
        couponDiscountGoodsRelMapper.deleteByExample(example);
    }
}
