package com.czg.service.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.resp.CzgRespCode;
import com.czg.resp.CzgResult;
import com.czg.system.dto.MiniAppPagesDTO;
import com.czg.system.entity.MiniAppPages;
import com.czg.system.service.MiniAppPageService;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.service.system.mapper.MiniAppPagesMapper;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 小程序页面路径 服务层实现。
 *
 * @author mac
 * @since 2025-02-12
 */
@DubboService
public class MiniAppPagesServiceImpl extends ServiceImpl<MiniAppPagesMapper, MiniAppPages> implements MiniAppPageService {

    @Override
    public CzgResult<Long> insertMiniAppPage(MiniAppPagesDTO pagesDTO) {
        if (exists(new QueryWrapper().eq(MiniAppPages::getName, pagesDTO.getName()))) {
            return CzgResult.failure("页面名称已存在");
        }
        MiniAppPages pages = BeanUtil.toBean(pagesDTO, MiniAppPages.class);
        save(pages);
        return CzgResult.success(pages.getId());
    }

    @Override
    public CzgResult<Boolean> updateMiniAppPage(MiniAppPagesDTO pagesDTO) {
        if (exists(new QueryWrapper().eq(MiniAppPages::getName, pagesDTO.getName()).ne(MiniAppPages::getId, pagesDTO.getId()))) {
            return CzgResult.failure("页面名称已存在");
        }

        MiniAppPages pages = getById(pagesDTO.getId());
        if (pages == null) {
            return CzgResult.failure(CzgRespCode.RECORD_NOT_EXIST);
        }

        pages.setIcon(pagesDTO.getIcon());
        pages.setName(pagesDTO.getName());
        pages.setPath(pagesDTO.getPath());
        pages.setStatus(pagesDTO.getStatus());
        pages.setDescription(pagesDTO.getDescription());
        pages.setStatus(pagesDTO.getStatus());
        pages.setSort(pagesDTO.getSort());
        updateById(pages);

        return CzgResult.success(true);
    }

    @Override
    public CzgResult<Boolean> deleteMiniAppPageById(Long id) {
        MiniAppPages pages = getById(id);
        if (pages == null) {
            return CzgResult.failure(CzgRespCode.RECORD_NOT_EXIST);
        }

        removeById(id);
        return CzgResult.success(true);
    }

    @Override
    public CzgResult<Page<MiniAppPagesDTO>> getMiniAppPage(String name, String path, Integer status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(name)) {
            queryWrapper.like(MiniAppPages::getName, name);
        }
        if (StrUtil.isNotEmpty(path)) {
            queryWrapper.like(MiniAppPages::getPath, path);
        }
        if (status != null && status != -1) {
            queryWrapper.eq(MiniAppPages::getStatus, status);
        }
        queryWrapper.orderBy(MiniAppPages::getSort, true);
        Page<MiniAppPagesDTO> dtoPage = pageAs(PageUtil.buildPage(), queryWrapper, MiniAppPagesDTO.class);
        return CzgResult.success(dtoPage);
    }
}
