package com.hfkj.service.merchant.impl;

import com.hfkj.dao.BsMerchantUserMapper;
import com.hfkj.entity.BsMerchantUser;
import com.hfkj.entity.BsMerchantUserExample;
import com.hfkj.service.merchant.BsMerchantUserService;
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;

/**
 * @className: BsMerchantUserServiceImpl
 * @author: HuRui
 * @date: 2024/5/31
 **/
@Service("merchantUserService")
public class BsMerchantUserServiceImpl implements BsMerchantUserService {

    @Resource
    private BsMerchantUserMapper merchantUserMapper;

    @Override
    public void editData(BsMerchantUser merchantUser) {
        merchantUser.setUpdateTime(new Date());
        if (merchantUser.getId() == null){
            merchantUser.setCreateTime(new Date());
            merchantUserMapper.insert(merchantUser);
        } else {
            merchantUserMapper.updateByPrimaryKey(merchantUser);
        }
    }

    @Override
    public BsMerchantUser getUser(String merNo,String phone) {
        BsMerchantUserExample example = new BsMerchantUserExample();
        example.createCriteria().andMerNoEqualTo(merNo).andUserPhoneEqualTo(phone).andStatusNotEqualTo(0);
        List<BsMerchantUser> list = merchantUserMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) {
            criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo"));
        }

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

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