package com.hfkj.service.impl;

import com.hfkj.dao.BsMerPlatformNoMapper;
import com.hfkj.entity.BsMerPlatformNo;
import com.hfkj.entity.BsMerPlatformNoExample;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.sysenum.PlatformTypeEnum;
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("merPlatformNoService")
public class BsMerPlatformNoServiceImpl implements BsMerPlatformNoService {

    @Resource
    private BsMerPlatformNoMapper merPlatformNoMapper;

    @Override
    public void editMerPlatformNo(BsMerPlatformNo merPlatformNo) {
        if (merPlatformNo.getId() == null) {
            merPlatformNo.setStatus(1);
            merPlatformNo.setCreateTime(new Date());
            merPlatformNo.setUpdateTime(new Date());
            merPlatformNoMapper.insert(merPlatformNo);
        } else {
            merPlatformNo.setUpdateTime(new Date());
            merPlatformNoMapper.updateByPrimaryKey(merPlatformNo);
        }
    }

    @Override
    public BsMerPlatformNo getPlatformNo(Long merId, PlatformTypeEnum type) {
        BsMerPlatformNoExample example = new BsMerPlatformNoExample();
        example.createCriteria().andMerIdEqualTo(merId).andPlatformTypeEqualTo(type.getNumber()).andStatusEqualTo(1);
        List<BsMerPlatformNo> list = merPlatformNoMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerPlatformNo getPlatformNoByCupNo(String merCupNo) {
        BsMerPlatformNoExample example = new BsMerPlatformNoExample();
        example.createCriteria().andCupNoEqualTo(merCupNo).andStatusEqualTo(1);
        List<BsMerPlatformNo> list = merPlatformNoMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsMerPlatformNo> getMerPlatformNoList(Map<String, Object> param) {
        BsMerPlatformNoExample example = new BsMerPlatformNoExample();
        BsMerPlatformNoExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

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

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

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


        return merPlatformNoMapper.selectByExample(example);
    }
}
