package com.hai.service.impl;

import com.hai.common.utils.DateUtil;
import com.hai.dao.HighOrderPreMapper;
import com.hai.entity.HighOrder;
import com.hai.entity.HighOrderPre;
import com.hai.entity.HighOrderPreExample;
import com.hai.service.HighOrderPreService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service("highOrderPreService")
public class HighOrderPreServiceImpl implements HighOrderPreService {

    @Resource
    private HighOrderPreMapper highOrderPreMapper;

    @Override
    public List<HighOrderPre> getListOrderPre(Map<String, String> map) {
        HighOrderPreExample example  = new HighOrderPreExample();
        HighOrderPreExample.Criteria criteria = example.createCriteria();

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

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

        if (MapUtils.getString(map, "orderNo") != null) {
            criteria.andOrderNoLike("%" + MapUtils.getString(map, "orderNo") + "%");
        }

        if (MapUtils.getString(map, "goodsName") != null) {
            criteria.andGoodsNameLike("%" + MapUtils.getString(map, "goodsName") + "%");
        }

        if (MapUtils.getString(map, "userPhone") != null) {
            criteria.andPreUserPhoneEqualTo(MapUtils.getString(map, "userPhone"));
        }

        if (StringUtils.isNotBlank(map.get("createTimeS")) && StringUtils.isNotBlank(map.get("createTimeE"))) {
            criteria.andCreateTimeBetween(
                    DateUtil.format(map.get("createTimeS") , "yyyy-MM-dd HH:mm:ss") ,
                    DateUtil.format(map.get("createTimeE") , "yyyy-MM-dd HH:mm:ss"));
        }
        return highOrderPreMapper.selectByExample(example);
    }

    @Override
    public HighOrderPre findByOrderId(Long orderId) {
        return highOrderPreMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public HighOrderPre findByOrderNo(String orderNo) {
        return null;
    }

    @Override
    public void insertOrderPre(HighOrderPre highOrderPre) {
         highOrderPreMapper.insert(highOrderPre);
    }

    @Override
    public void updateOrderPre(HighOrderPre highOrderPre) {
         highOrderPreMapper.updateByPrimaryKey(highOrderPre);
    }
}
