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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.result.R;
import shop.alien.entity.store.CommonPushTaskNum;
import shop.alien.mapper.CommonPushTaskNumMapper;
import shop.alien.store.service.CommonPushTaskNumService;

@Slf4j
@Service
@RequiredArgsConstructor
public class CommonPushTaskNumServiceImpl extends ServiceImpl<CommonPushTaskNumMapper, CommonPushTaskNum> implements CommonPushTaskNumService {

    @Override
    public R<String> add(CommonPushTaskNum taskNum) {
        log.info("CommonPushTaskNumServiceImpl.add, param={}", taskNum);
        boolean result = this.save(taskNum);
        return result ? R.success("新增成功") : R.fail("新增失败");
    }

    @Override
    public R<String> deleteById(Long id) {
        log.info("CommonPushTaskNumServiceImpl.deleteById, id={}", id);
        boolean result = this.removeById(id);
        return result ? R.success("删除成功") : R.fail("删除失败");
    }

    @Override
    public R<String> update(CommonPushTaskNum taskNum) {
        log.info("CommonPushTaskNumServiceImpl.update, param={}", taskNum);
        if (taskNum.getId() == null) {
            return R.fail("id不能为空");
        }
        boolean result = this.updateById(taskNum);
        return result ? R.success("更新成功") : R.fail("更新失败");
    }

    @Override
    public R<CommonPushTaskNum> getInfoById(Long id) {
        log.info("CommonPushTaskNumServiceImpl.getInfoById, id={}", id);
        return R.data(this.getById(id));
    }

    @Override
    public R<IPage<CommonPushTaskNum>> list(Integer pageNum, Integer pageSize, Long pushTaskId, String phoneType) {
        log.info("CommonPushTaskNumServiceImpl.list, pageNum={}, pageSize={}, pushTaskId={}, phoneType={}",
                pageNum, pageSize, pushTaskId, phoneType);
        Page<CommonPushTaskNum> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CommonPushTaskNum> wrapper = new LambdaQueryWrapper<>();
        if (pushTaskId != null) {
            wrapper.eq(CommonPushTaskNum::getPushTaskId, pushTaskId);
        }
        if (StringUtils.isNotBlank(phoneType)) {
            wrapper.eq(CommonPushTaskNum::getPhoneType, phoneType);
        }
        wrapper.orderByDesc(CommonPushTaskNum::getUpdatedTime);
        return R.data(this.page(page, wrapper));
    }
}
