package com.hai.service.impl;

import com.hai.common.security.UserCenter;
import com.hai.dao.HighOilCardContactRecordMapper;
import com.hai.entity.HighOilCardContactRecord;
import com.hai.entity.HighOilCardContactRecordExample;
import com.hai.model.UserInfoModel;
import com.hai.service.HighOilCardContactRecordService;
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;

@Service("oilCardContactRecordService")
public class HighOilCardContactRecordServiceImpl implements HighOilCardContactRecordService {

    @Resource
    private HighOilCardContactRecordMapper oilCardContactRecordMapper;

    @Resource
    private UserCenter userCenter;

    @Override
    public void insert(Long oilCardId, String oilCardNo , String content) {
        UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);

        HighOilCardContactRecord record = new HighOilCardContactRecord();
        record.setOilCardId(oilCardId);
        record.setCardNo(oilCardNo);
        record.setContent(content);
        record.setCreateTime(new Date());
        record.setStatus(1);
        record.setOpUserId(userInfoModel.getSecUser().getId());
        record.setOpUserName(userInfoModel.getSecUser().getUserName());
        oilCardContactRecordMapper.insert(record);
    }

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "cardNo"))) {
            criteria.andCardNoEqualTo(MapUtils.getString(param, "cardNo"));
        }

        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 oilCardContactRecordMapper.selectByExample(example);
    }
}
