package com.app.admin.framework.system.organization.service.impl;

import cn.hutool.core.date.DateUtil;
import com.app.admin.framework.system.organization.convert.SysOrganizationConvert;
import com.app.admin.framework.system.organization.dao.SysOrganizationDao;
import com.app.admin.framework.system.organization.model.SysOrganization;
import com.app.admin.framework.system.organization.service.SysOrganizationService;
import com.app.admin.framework.system.organization.view.SysOrganizationView;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.support.service.impl.CrudBaseServiceSupport;
import com.app.framework.core.support.view.grid.Paging;
import com.app.framework.core.support.view.tree.ExTreeNode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 组织机构管理
 */
@Service
public class SysOrganizationServiceImpl extends CrudBaseServiceSupport<SysOrganizationDao, SysOrganization> implements SysOrganizationService {

    public List<SysOrganization> getPageList(SysOrganization view, Paging paging) {
        LambdaQueryWrapper<SysOrganization> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysOrganization::getParentId, view.getParentId());
        if (StringUtils.isNotBlank(view.getName())) {
            wrapper.like(SysOrganization::getName, view.getName());
        }
        wrapper.orderByDesc(SysOrganization::getCreateTime);
        return this.pageList(wrapper, paging);
    }

    /**
     * 保存组织机构
     *
     * @param view 组织机构对象
     */
    public SysOrganization saveOrganization(SysOrganizationView view, LoginUser loginUser) {
        SysOrganization entity = new SysOrganization();
        BeanUtils.copyProperties(view, entity);
        if (entity.getId() == null) {
            entity.setCreateTime(DateUtil.date());
            this.save(entity);
            //更新path字段
            SysOrganization update = new SysOrganization();
            update.setId(entity.getId());
            update.setPath(new StringBuilder().append(entity.getId()).append(",").toString());
            this.updateById(update);
        } else {
            if (StringUtils.isBlank(entity.getPath())) {
                entity.setPath(new StringBuilder().append(entity.getId()).append(",").toString());
            }
            this.updateById(entity);
        }
        return entity;
    }

    /**
     * 查询机构树
     *
     * @param parentId
     * @return
     */
    public List<ExTreeNode> listTree(Long parentId) {
        LambdaQueryWrapper<SysOrganization> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(SysOrganization::getPath, parentId + ",");
        wrapper.eq(SysOrganization::getState, StateEnum.NORMAL.getValue());
        wrapper.orderByDesc(SysOrganization::getCreateTime);
        List<SysOrganization> list = this.list(wrapper);
        return new SysOrganizationConvert().transStaticTree(list);
    }
}