package com.ruoyi.interfaces.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.CheckUtils;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.interfaces.domain.PtApp;
import com.ruoyi.interfaces.mapper.PtAppMapper;
import com.ruoyi.interfaces.service.PtAppService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author LinQiLong
 * @date 2022/1/7 10:20
 */
@Service
public class PtAppServiceImpl implements PtAppService {

    private final PtAppMapper ptAppMapper;

    public PtAppServiceImpl(PtAppMapper ptAppMapper) {
        this.ptAppMapper = ptAppMapper;
    }

    @Override
    public int insert(PtApp ptApp) {
//        ptApp.setAppId(UuidUtil.uuid());
//        ptApp.setAppSecret(UUID.randomUUID().toString().replace("-", ""));
//        Date date = new Date();
//        ptApp.setAppPublish(date);
//        ptApp.setAuditTime(date);
//        ptApp.setRqTime(date);
        return ptAppMapper.insert(ptApp);
    }

    @Override
    public int update(PtApp ptApp) {
        CheckUtils.notEmpty(ptApp.getAppId(), "appId.no");
        return ptAppMapper.updateByPrimaryKeySelective(ptApp);
    }

    @Override
    public boolean del(String keys) {
        if (StringUtils.isBlank(keys)) {
            return false;
        }
        String[] strs = keys.split(",");
        int deletes = 0;
        for (String str : strs) {
            deletes += ptAppMapper.deleteByPrimaryKey(str);
        }
        return deletes == strs.length;
    }

    @Override
    public PtApp get(String key) {
        return ptAppMapper.selectByPrimaryKey(key);
    }

    @Override
    public Page listOfPage(PageParam<PtApp> ptAppPageParam) {
        PageHelper.startPage(ptAppPageParam);
        List<PtApp> list = list(ptAppPageParam.getData());
        return PageUtils.convert(list);
    }

    @Override
    public List<PtApp> list(PtApp ptApp) {
        return ptAppMapper.selectAll(ptApp);
    }

    @Override
    public List<PtApp> listByIds(String[] ids) {
        if (ids == null || ids.length == 0) {
            return null;
        }
        return ptAppMapper.listByIds(ids);
    }
}
