package com.hai.service.impl;

import com.hai.dao.HighGasOrderMapper;
import com.hai.entity.HighGasOrder;
import com.hai.entity.HighGasOrderExample;
import com.hai.service.HighGasOrderService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: HighGasOrderServiceImpl
 * @author: HuRui
 * @date: 2022/9/6
 **/
@Service("gasOrderService")
public class HighGasOrderServiceImpl implements HighGasOrderService {

    @Resource
    private HighGasOrderMapper gasOrderMapper;

    @Override
    public void addGasOrder(HighGasOrder gasOrder) {
        gasOrder.setCreateTime(new Date());
        gasOrderMapper.insert(gasOrder);
    }

    @Override
    public void updateGasOrder(HighGasOrder gasOrder) {
        gasOrderMapper.updateByPrimaryKeySelective(gasOrder);
    }

    @Override
    public HighGasOrder getDetailByChildOrderNo(String childOrderNo) {
        HighGasOrderExample example = new HighGasOrderExample();
        example.createCriteria().andChildOrderNoEqualTo(childOrderNo);
        List<HighGasOrder> list = gasOrderMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public HighGasOrder getDetailByOrderNo(String orderNo) {
        HighGasOrderExample example = new HighGasOrderExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);
        List<HighGasOrder> list = gasOrderMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighGasOrder> getGasOrderList(Map<String, Object> param) {
        HighGasOrderExample example = new HighGasOrderExample();
        HighGasOrderExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(param, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(param, "companyId"));
        }

        if (MapUtils.getLong(param, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(param, "merId"));
        }

        if (MapUtils.getLong(param, "storeId") != null) {
            criteria.andStoreIdEqualTo(MapUtils.getLong(param, "storeId"));
        }

        if (MapUtils.getLong(param, "gasOrgId") != null) {
            criteria.andGasOrgIdEqualTo(MapUtils.getLong(param, "gasOrgId"));
        }

        if (MapUtils.getLong(param, "gasAgentId") != null) {
            criteria.andGasAgentIdEqualTo(MapUtils.getLong(param, "gasAgentId"));
        }

        if (MapUtils.getLong(param, "gasSalesmanId") != null) {
            criteria.andGasSalesmanIdEqualTo(MapUtils.getLong(param, "gasSalesmanId"));
        }

        if (MapUtils.getLong(param, "gasStaffId") != null) {
            criteria.andGasStaffIdEqualTo(MapUtils.getLong(param, "gasStaffId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "storeName"))) {
            criteria.andStoreNameLike("%"+MapUtils.getString(param, "storeName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "orderNo"))) {
            criteria.andOrderNoLike("%"+MapUtils.getString(param, "orderNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "childOrderNo"))) {
            criteria.andChildOrderNoLike("%"+MapUtils.getString(param, "childOrderNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "memCardNo"))) {
            criteria.andMemCardNoLike("%"+MapUtils.getString(param, "memCardNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "memPhone"))) {
            criteria.andMemPhoneLike("%"+MapUtils.getString(param, "memPhone")+"%");
        }

        if (MapUtils.getInteger(param, "gasOilType") != null) {
            criteria.andGasOilTypeEqualTo(MapUtils.getInteger(param, "storeId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "gasStaffName"))) {
            criteria.andGasStaffNameLike("%"+MapUtils.getString(param, "gasStaffName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "gasOilNo"))) {
            criteria.andGasOilNoEqualTo(MapUtils.getString(param, "gasOilNo"));
        }

        if (MapUtils.getInteger(param, "payType") != null) {
            criteria.andPayTypeEqualTo(MapUtils.getInteger(param, "payType"));
        }

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

        if (MapUtils.getLong(param, "createTimeS") != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeS")));
        }

        if (MapUtils.getLong(param, "createTimeE") != null) {
            criteria.andCreateTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeE")));
        }

        if (MapUtils.getLong(param, "payTimeS") != null) {
            criteria.andPayTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "payTimeS")));
        }

        if (MapUtils.getLong(param, "payTimeE") != null) {
            criteria.andPayTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "payTimeE")));
        }

        if (MapUtils.getLong(param, "refundTimeS") != null) {
            criteria.andRefundTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "refundTimeS")));
        }

        if (MapUtils.getLong(param, "refundTimeE") != null) {
            criteria.andRefundTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "refundTimeE")));
        }

        example.setOrderByClause("create_time desc");
        return gasOrderMapper.selectByExample(example);
    }
}
