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.MerPlatformNoStatusEnum;
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.HashMap;
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.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 getCurrentPlatform(Long merId) {
        BsMerPlatformNoExample example = new BsMerPlatformNoExample();
        example.createCriteria()
                .andMerIdEqualTo(merId)
                .andStatusNotEqualTo(MerPlatformNoStatusEnum.status0.getNumber())
                .andCurrentPlatformEqualTo(true);
        List<BsMerPlatformNo> list = merPlatformNoMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerPlatformNo getPlatformNo(Long merId, PlatformTypeEnum type, MerPlatformNoStatusEnum status) {
        BsMerPlatformNoExample example = new BsMerPlatformNoExample();
        BsMerPlatformNoExample.Criteria criteria = example.createCriteria()
                .andMerIdEqualTo(merId)
                .andPlatformTypeEqualTo(type.getNumber())
                .andStatusNotEqualTo(MerPlatformNoStatusEnum.status0.getNumber());
        if (status != null) {
            criteria.andStatusEqualTo(status.getNumber());
        }
        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.getLong(param, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(param, "merId"));
        }

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

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

    @Override
    public void switchPlatform(Long merId, PlatformTypeEnum type) {
        Map<String,Object> param = new HashMap<>();
        param.put("merId", merId);
        List<BsMerPlatformNo> platformNoList = getMerPlatformNoList(param);
        for (BsMerPlatformNo platform : platformNoList) {
            if (platform.getPlatformType().equals(type.getNumber())) {
                platform.setCurrentPlatform(true);
            } else {
                platform.setCurrentPlatform(false);
            }
            editMerPlatformNo(platform);
        }
    }
}
