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

import cn.hutool.core.util.ObjectUtil;
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 lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
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.entity.AppSite;
import org.jeecg.modules.system.app.mapper.AppCategoryMapper;
import org.jeecg.modules.system.app.mapper.AppSiteMapper;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

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

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

    @Resource
    private AppSiteMapper appSiteMapper;

    @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.getPageNo(), 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;
        });
    }

    @Override
    public List<AppCategoryVO> queryCategories() {
        List<AppCategoryVO> list = new ArrayList<>();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String orgCode = loginUser.getOrgCode();
        AppSite site = appSiteMapper.selectOne(Wrappers.<AppSite>lambdaQuery().eq(AppSite::getOrgCode, orgCode));
        if (ObjectUtil.isEmpty(site)) {
            log.info("当前账号无门店信息,判定为平台用户：{}", orgCode);
            List<AppCategory> appCategories = baseMapper.selectList(Wrappers.<AppCategory>lambdaQuery());
            for (AppCategory appCategory : appCategories){
                AppCategoryVO vo = new AppCategoryVO();
                BeanUtils.copyProperties(appCategory, vo);
                list.add(vo);
            }
            return list;
        }
        String categoryIds = site.getCategoryId();
        if (StringUtils.isNotEmpty(categoryIds)){
            for (String id : categoryIds.split(",")) {
                AppCategory category = baseMapper.selectById(id);
                AppCategoryVO vo = new AppCategoryVO();
                BeanUtils.copyProperties(category, vo);
                list.add(vo);
            }
        }
        return list;
    }

    /**
     * 权限校验
     * @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);
    }

}
