package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.StoreVerificationCode;
import shop.alien.mapper.StoreVerificationCodeMapper;
import shop.alien.store.service.StoreVerificationCodeService;

import java.util.Date;
import java.util.Objects;

/**
 * 短信验证码 service 实现
 */
@Service
@RequiredArgsConstructor
public class StoreVerificationCodeServiceImpl extends ServiceImpl<StoreVerificationCodeMapper, StoreVerificationCode>
        implements StoreVerificationCodeService {

    @Override
    public IPage<StoreVerificationCode> pageList(String phone, Integer appType, Integer businessType, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<StoreVerificationCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(phone), StoreVerificationCode::getPhone, phone);
        wrapper.eq(null != appType, StoreVerificationCode::getAppType, appType);
        wrapper.eq(null != businessType, StoreVerificationCode::getBusinessType, businessType);
        wrapper.eq(StoreVerificationCode::getDeleteFlag, 0);
        wrapper.orderByDesc(StoreVerificationCode::getCreatedTime);
        Page<StoreVerificationCode> page = new Page<>(null == pageNum ? 1 : pageNum, null == pageSize ? 10 : pageSize);
        return this.page(page, wrapper);
    }

    @Override
    public StoreVerificationCode create(StoreVerificationCode request) {
        request.setId(null);
        Date now = new Date();
        request.setCreatedTime(now);
        request.setUpdatedTime(now);
        request.setDeleteFlag(0);
        this.save(request);
        return request;
    }

    @Override
    public StoreVerificationCode updateRecord(StoreVerificationCode request) {
        if (Objects.isNull(request.getId())) {
            throw new IllegalArgumentException("id不能为空");
        }
        request.setUpdatedTime(new Date());
        this.updateById(request);
        return this.getById(request.getId());
    }

    @Override
    public boolean removeRecord(Integer id) {
        if (Objects.isNull(id)) {
            return false;
        }
        StoreVerificationCode entity = new StoreVerificationCode();
        entity.setId(id);
        entity.setDeleteFlag(1);
        entity.setUpdatedTime(new Date());
        return this.updateById(entity);
    }

}

