package top.imwork.window.silos.service.upms.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import jakarta.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.imwork.commons.core.pojo.BaseTreeNode;
import top.imwork.commons.core.utils.StringUtils;
import top.imwork.window.silos.convert.upms.PermissionConvert;
import top.imwork.window.silos.dao.upms.PermissionDao;
import top.imwork.window.silos.entity.upms.Permission;
import top.imwork.window.silos.pojo.upms.bo.PermissionBO;
import top.imwork.window.silos.pojo.upms.bo.PermissionResultBO;
import top.imwork.window.silos.pojo.upms.dto.PermissionDTO;
import top.imwork.window.silos.pojo.upms.dto.PermissionListDTO;
import top.imwork.window.silos.service.upms.IPermissionService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, Permission> implements IPermissionService {
    @Resource
    private PermissionDao permissionDao;

    /**
     * 根据id获取信息
     * @param id 用户id
     * @return PermissionBO 响应信息
     */
    @Override
    public PermissionBO info(Long id) {
        Permission permission = permissionDao.info(id);
        return PermissionConvert.permissionDoToBo(permission);
    }

    /**
     * 新增/更新信息
     * @param permissionDTO 信息
     * @return permissionBO 更新后信息
     */
    @Override
    public PermissionBO save(PermissionDTO permissionDTO) {
        Permission permission = PermissionConvert.permissionDtoToDo(permissionDTO);
        if (ObjectUtils.isEmpty(permission.getId())) {
            permission.setCreator("admin");
            permission.setCreateTime(new Date());
            permission.setUpdateTime(new Date());
            permission.setModifier("admin");
            permissionDao.insert(permission);
        } else {
            permissionDao.updateById(permission);
        }
        return PermissionConvert.permissionDoToBo(permission);
    }

    /**
     * 根据id删除信息
     * @param id id
     * @return true/false 是否删除成功
     */
    @Override
    public Boolean delete(Long id) {
        return permissionDao.deleteById(id) == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean deleteByIds(String[] ids) {
        return permissionDao.deleteByIds(ids) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public PermissionResultBO queryPage(PermissionListDTO permissionListDTO) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(permissionListDTO.getPermissionName())) {
            queryWrapper.like("privilege_name", permissionListDTO.getPermissionName());
        }

        if (!StringUtils.isEmpty(permissionListDTO.getCreateBeginTime())) {
            queryWrapper.apply("create_time >= '" + permissionListDTO.getCreateBeginTime() + "'");
        }

        if (!StringUtils.isEmpty(permissionListDTO.getCreateEndTime())) {
            queryWrapper.apply("create_time <= '" + permissionListDTO.getCreateEndTime() + "'");
        }

        if (!StringUtils.isEmpty(permissionListDTO.getUpdateBeginTime())) {
            queryWrapper.apply("update_time >= '" + permissionListDTO.getUpdateBeginTime() + "'");
        }

        if (!StringUtils.isEmpty(permissionListDTO.getUpdateEndTime())) {
            queryWrapper.apply("update_time <= '" + permissionListDTO.getUpdateEndTime() + "'");
        }

        queryWrapper.like("del_flag", "N");

        Page<Permission> page = new Page<>(permissionListDTO.getPageNo(), permissionListDTO.getPageRows());
        IPage<Permission> iPage = permissionDao.queryPage(page, queryWrapper);

        PermissionResultBO resultBO = new PermissionResultBO();
        resultBO.setPageNo(permissionListDTO.getPageNo());
        resultBO.setPageRows(permissionListDTO.getPageRows());
        resultBO.setPageCount((int) iPage.getPages());
        resultBO.setTotalRows((int) iPage.getTotal());
        resultBO.setDataList(PermissionConvert.permissionListToPermissionBOList(iPage.getRecords()));
        resultBO.setParams(permissionListDTO);

        return resultBO;
    }

    @Override
    public List<BaseTreeNode> queryTreeList() {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("del_flag", "N");
        List<Permission> list = permissionDao.selectList(queryWrapper);

        List<BaseTreeNode> treeNodeList = new ArrayList<>();
        for(Permission  permission : list){
            BaseTreeNode  treeNode = new BaseTreeNode();
            treeNode.setId(permission.getId().toString());
            treeNode.setName(permission.getPrivilegeName());
            treeNode.setPId(permission.getParentId().toString());
            treeNodeList.add(treeNode);
        }
        List<BaseTreeNode> treeNodes = BaseTreeNode.buildTree(treeNodeList);
        return treeNodes;
    }
}