package com.hfkj.service.goods.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsOrderStarbucksMapper;

import com.hfkj.entity.BsOrder;
import com.hfkj.entity.BsOrderStarbucks;
import com.hfkj.entity.BsOrderStarbucksExample;
import com.hfkj.service.goods.BsOrderStarbucksService;
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("bsOrderStarbucksService")
public class BsOrderStarbucksServiceImpl implements BsOrderStarbucksService {

    @Resource
    private BsOrderStarbucksMapper bsOrderStarbucksMapper;

    @Override
    public void create(BsOrderStarbucks orderStarbucks) {
        bsOrderStarbucksMapper.insert(orderStarbucks);
    }

    @Override
    public void update(BsOrderStarbucks orderStarbucks) {
        bsOrderStarbucksMapper.updateByPrimaryKeySelective(orderStarbucks);
    }

    @Override
    public void delete(Long id, Boolean fullDelete) {
        bsOrderStarbucksMapper.deleteByPrimaryKey(id);
    }

    @Override
    public BsOrderStarbucks queryDetail(Long id) {
        return bsOrderStarbucksMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsOrderStarbucks findGoodsOrder(String orderNo) {
        BsOrderStarbucksExample example = new BsOrderStarbucksExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);

        List<BsOrderStarbucks> list = bsOrderStarbucksMapper.selectByExample(example);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(map, "orderNo"))) {
            criteria.andOrderNoEqualTo(MapUtils.getString(map, "orderNo"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "childOrderNo"))) {
            criteria.andChildOrderNoEqualTo(MapUtils.getString(map, "childOrderNo"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "userPhone"))) {
            criteria.andUserPhoneEqualTo(MapUtils.getString(map, "userPhone"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "platformCode"))) {
            criteria.andPlatformCodeEqualTo(MapUtils.getString(map, "platformCode"));
        }

        if (MapUtils.getLong(map, "createTimeS") != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(map, "createTimeS")));
        }
        if (MapUtils.getLong(map, "createTimeE") != null) {
            criteria.andCreateTimeLessThan(new Date(MapUtils.getLong(map, "createTimeE")));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        }
        example.setOrderByClause("create_time desc");

        return bsOrderStarbucksMapper.selectByExample(example);
    }

    @Override
    public void updateOrderStatus(String orderNo, Integer status) {
        // 查询星巴克订单
        Map<String , Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        List<BsOrderStarbucks> list = getList(map);

        for (BsOrderStarbucks orderStarbucks : list) {
            orderStarbucks.setUpdateTime(new Date());
            orderStarbucks.setStatus(status);

            update(orderStarbucks);
        }
    }
}
