package org.jeecg.modules.system.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.app.dto.AppCategoryDTO;
import org.jeecg.modules.system.app.entity.AppCategory;
import org.jeecg.modules.system.app.mapper.AppCategoryMapper;
import org.jeecg.modules.system.app.service.IAppCategoryService;
import org.jeecg.modules.system.app.vo.AppCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import static org.jeecg.common.constant.CommonConstant.SC_INTERNAL_SERVER_ERROR_500;

/**
 * @Description: 运动类型表
 * @Author: jeecg-boot
 * @Date:   2025-07-03
 * @Version: V1.0
 */
@Service
public class AppCategoryServiceImpl extends ServiceImpl<AppCategoryMapper, AppCategory> implements IAppCategoryService {


    @Override
    public Boolean saveCategory(AppCategoryVO categoryVO) {
        AppCategory category = new AppCategory();
        BeanUtils.copyProperties(categoryVO, category);
        int insert = baseMapper.insert(category);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean editCategory(AppCategoryDTO categoryDTO) {
        AppCategory selectedById = baseMapper.selectById(categoryDTO.getId());
        if (null==selectedById) throw new JeecgBootException("无此类目信息");
        AppCategory category = new AppCategory();
        BeanUtils.copyProperties(categoryDTO, category);
        int update = baseMapper.updateById(category);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteCategory(String id) {
        AppCategory selectedById = baseMapper.selectById(id);
        if (null==selectedById) throw new JeecgBootException("无此类目信息");
        int delete = baseMapper.deleteById(id);
        if (delete > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public IPage<AppCategoryVO> queryCategoryList(AppCategoryDTO dto) {
        Page<AppCategory> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<AppCategory> wrapper = Wrappers.<AppCategory>lambdaQuery()
                .like(StringUtils.isNotBlank(dto.getName()), AppCategory::getName, dto.getName()).last("order by sort");
        IPage<AppCategory> resultPage = baseMapper.selectPage(page, wrapper);
        return resultPage.convert(record -> {
            AppCategoryVO vo = new AppCategoryVO();
            BeanUtils.copyProperties(record, vo);
            return vo;
        });
    }
    /**
     * 权限校验
     * @param category
     * @param sysUser
     */
    private void checkPermission(AppCategory category, LoginUser sysUser ){
        if (!sysUser.getOrgCode().equals(category.getOrgCode())) throw new JeecgBootException("无权限操作", SC_INTERNAL_SERVER_ERROR_500);
    }

}
