package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.HighGasClassGroupMapper;
import com.hai.entity.HighGasClassGroup;
import com.hai.entity.HighGasClassGroupExample;
import com.hai.service.HighGasClassGroupService;
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("gasClassGroupService")
public class HighGasClassGroupServiceImpl implements HighGasClassGroupService {

    @Resource
    private HighGasClassGroupMapper gasClassGroupMapper;

    @Override
    public void editGroup(HighGasClassGroup gasClassGroup) {
        if (gasClassGroup.getId() == null) {
            gasClassGroup.setStatus(1);
            gasClassGroup.setCreateTime(new Date());
            gasClassGroup.setUpdateTime(new Date());
            gasClassGroupMapper.insert(gasClassGroup);
        } else {
            gasClassGroup.setUpdateTime(new Date());
            gasClassGroupMapper.updateByPrimaryKey(gasClassGroup);
        }
    }

    @Override
    public void delGroup(Long groupId) {
        HighGasClassGroup classGroup = getDetailById(groupId);
        if (classGroup == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到班组");
        }
        classGroup.setStatus(0);
        editGroup(classGroup);
    }

    @Override
    public HighGasClassGroup getDetailById(Long groupId) {
        return gasClassGroupMapper.selectByPrimaryKey(groupId);
    }

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merchantStoreName"))) {
            criteria.andMerchantStoreNameLike("%" + MapUtils.getString(param, "merchantStoreId") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "name"))) {
            criteria.andNameLike("%" + MapUtils.getString(param, "name") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "principalName"))) {
            criteria.andPrincipalNameLike("%" + MapUtils.getString(param, "principalName") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "principalPhone"))) {
            criteria.andPrincipalPhoneLike("%" + MapUtils.getString(param, "principalPhone") + "%");
        }

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