package com.ruoyi.interfaces.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.uuid.IdUtils;
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.core.service.AbstractCrudService;
import com.ruoyi.interfaces.domain.PtServiceComment;
import com.ruoyi.common.exception.CheckException;
import com.ruoyi.interfaces.mapper.PtServiceCommentDao;
import com.ruoyi.interfaces.service.PtServiceCommentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author
 * @date 2022-1-17
 */
@Service
@Transactional
public class PtServiceCommentServiceImpl extends AbstractCrudService<PtServiceComment, PageParam<PtServiceComment>> implements PtServiceCommentService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PtServiceCommentDao ptServiceCommentDao;

    public PtServiceCommentServiceImpl(PtServiceCommentDao ptServiceCommentDao) {
        super(ptServiceCommentDao);
        this.ptServiceCommentDao = ptServiceCommentDao;
    }

    @Override
    public int insert(PtServiceComment ptServiceComment) {
        Optional.ofNullable(ptServiceComment).map(PtServiceComment::getSrvId).orElseThrow(() -> new CheckException("srvId.no"));
        ptServiceComment.setId(IdUtils.simpleUUID());
        ptServiceComment.setIntm(new Date());
        if (StringUtils.isBlank(ptServiceComment.getPid())) {
            ptServiceComment.setPid("0");
        }
        return this.ptServiceCommentDao.insert(ptServiceComment);
    }

    @Override
    public int update(PtServiceComment ptServiceComment) {
        return this.ptServiceCommentDao.update(ptServiceComment);
    }

    @Override
    public int delete(String id) {
        return this.ptServiceCommentDao.delete(id);
    }

    @Override
    public PtServiceComment save(PtServiceComment ptServiceComment) {
        int ret = 0;
        if (StringUtils.isBlank(ptServiceComment.getId())) {
            ret = insert(ptServiceComment);
        } else {
            ret = update(ptServiceComment);
        }
        return ptServiceComment;
    }

    @Override
    public PtServiceComment get(String id) {
        return ptServiceCommentDao.get(id);
    }

    @Override
    public PageInfo<PtServiceComment> findPageInfo(PageParam<PtServiceComment> param) {
        Optional.ofNullable(param.getData()).map(PtServiceComment::getSrvId).orElseThrow(() -> new CheckException("srvId.no"));
        PtServiceComment ptServiceComment = param.getData();
        ptServiceComment.setPid("0");
        PageHelper.startPage(param);
        param.setOrderBy("INTM");
        List<PtServiceComment> list = ptServiceCommentDao.list(ptServiceComment);

        list.forEach(c -> c.setPtServiceCommentList(getChildren(c.getId())));
        return new PageInfo<>(list);
    }

    private Page getChildren(String pid) {
        PageParam<PtServiceComment> param = new PageParam<>();
        param.setPageNum(1);
        param.setPageSize(10);
        param.setOrderBy("INTM");
        param.setData(PtServiceComment.builder().pid(pid).build());
        List<PtServiceComment> childList = ptServiceCommentDao.list(param.getData());
        return PageUtils.convert(childList);
    }
}
