package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.AESUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmStoreMemberPhoneDao;
import com.ssm.manage.entity.StoreMemberPhone;
import com.ssm.manage.query.StoreMemberPhoneRequest;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmStoreMemberPhoneService;
import com.ssm.manage.utils.Constant;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class SsmStoreMemberPhoneServiceImpl extends BaseServiceImpl<StoreMemberPhone> implements SsmStoreMemberPhoneService {
//    @Autowired
//    private RedisClient<StaffResponse> redisClient;

    @Autowired
    private SsmStoreMemberPhoneDao dao;

    @Override
    public SsmStoreMemberPhoneDao getDao() {
        return dao;
    }

    @Override
    public PageInfo<StoreMemberPhone> selectPage(StoreMemberPhoneRequest memberPhoneRequest) {
        if (StringUtils.isNotBlank(memberPhoneRequest.getOldPhone())) {
            memberPhoneRequest.setOldPhone(AESUtils.encrypt(memberPhoneRequest.getOldPhone()));
        }
        PageInfo pageInfo = dao.selectPage(memberPhoneRequest);
        List<StoreMemberPhone> lists = pageInfo.getList();
        lists.forEach(storeMemberFile -> {
            try {
                storeMemberFile.setOldPhone(AESUtils.decrypt(storeMemberFile.getOldPhone()).replaceAll("\u0000",""));
            } catch (Exception e) {
                log.error("SsmStoreMemberPhoneServiceImpl.selectPage:",e);
            }
        });
        return pageInfo;
    }

    @Override
    public int add(StoreMemberPhoneRequest memberPhoneRequest) {
        int i = 0;
        StoreMemberPhone storeMemberPhone = new StoreMemberPhone();
        BeanUtils.copyProperties(memberPhoneRequest, storeMemberPhone);
        storeMemberPhone.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        i = dao.saveNotNull(storeMemberPhone);
        return i;
    }



}
