package com.hfkj.service.impl;

import com.hfkj.dao.BsMerContractRecordMapper;
import com.hfkj.entity.BsMerContractRecord;
import com.hfkj.entity.BsMerContractRecordExample;
import com.hfkj.service.BsMerContractRecordService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service("merContractRecordService")
public class BsMerContractRecordServiceImpl implements BsMerContractRecordService {

    @Resource
    private BsMerContractRecordMapper merContractRecordMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void insertRecord(BsMerContractRecord record) {
        record.setCreateTime(new Date());
        merContractRecordMapper.insert(record);
    }

    @Override
    public void updateRecord(BsMerContractRecord record) {
        merContractRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public BsMerContractRecord getRecordByOrderNo(String orderNo) {
        BsMerContractRecordExample example = new BsMerContractRecordExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);
        List<BsMerContractRecord> list = merContractRecordMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerContractRecord getRecordByContractId(String contractId) {
        BsMerContractRecordExample example = new BsMerContractRecordExample();
        example.createCriteria().andContractIdEqualTo(contractId);
        List<BsMerContractRecord> list = merContractRecordMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsMerContractRecord> getContractRecordList(Map<String, Object> param) {
        BsMerContractRecordExample example = new BsMerContractRecordExample();
        BsMerContractRecordExample.Criteria criteria = example.createCriteria();

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

        return merContractRecordMapper.selectByExample(example);
    }
}
