package com.hai.service.impl;

import com.hai.dao.HighMerchantStoreAccountRecordMapper;
import com.hai.entity.HighMerchantStoreAccountRecord;
import com.hai.entity.HighMerchantStoreAccountRecordExample;
import com.hai.service.HighMerchantStoreAccountRecordService;
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("merStoreAccountRecordService")
public class HighMerchantStoreAccountRecordServiceImpl implements HighMerchantStoreAccountRecordService {

    @Resource
    private HighMerchantStoreAccountRecordMapper accountRecordMapper;

    @Override
    public void editRecord(HighMerchantStoreAccountRecord record) {
        if (record.getId() == null) {
            record.setCreateTime(new Date());
            record.setStatus(1);
            accountRecordMapper.insert(record);
        } else {
            accountRecordMapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public List<HighMerchantStoreAccountRecord> getRecordList(Map<String, Object> param) {
        HighMerchantStoreAccountRecordExample example = new HighMerchantStoreAccountRecordExample();
        HighMerchantStoreAccountRecordExample.Criteria criteria = example.createCriteria().andStatusEqualTo(1);

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

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

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

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

        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")));
        }

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