package com.codvision.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.auth.entity.Org;
import com.codvision.auth.entity.OrgProperty;
import com.codvision.auth.entity.vo.OrgPropertyVO;
import com.codvision.auth.mapper.OrgMapper;
import com.codvision.auth.mapper.OrgPropertyMapper;
import com.codvision.auth.service.IOrgPropertyService;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 组织属性表 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-10
 */
@Service
public class OrgPropertyServiceImpl extends BaseServiceImpl<OrgPropertyMapper, OrgProperty, OrgPropertyVO> implements IOrgPropertyService {

    @Resource
    private OrgMapper orgMapper;

    @Override
    public OrgPropertyVO getDetail(Long id) {
        OrgPropertyVO vo = new OrgPropertyVO();
        OrgProperty po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<OrgPropertyVO> getPageResult(QueryPage<OrgProperty> queryPage) {
        QueryWrapper<OrgProperty> queryWrapper = queryPage.getWrapper();
        IPage<OrgPropertyVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<OrgPropertyVO> listAll(OrgProperty queryBean) {
        QueryWrapper<OrgProperty> wrapper = PageUtil.initWrapper(queryBean);
        List<OrgProperty> list = this.list(wrapper);
        return BeanUtil.copyToList(list, OrgPropertyVO.class);
    }

    @Override
    public boolean save(OrgProperty orgProperty) {
        Assert.notNull(orgProperty, "组织属性表不能为空");
        if (orgProperty.getPid() != 0) {
            OrgProperty parentOrgProperty = getBaseMapper()
                    .selectOne(Wrappers.<OrgProperty>lambdaQuery().eq(OrgProperty::getId, orgProperty.getPid()));
            Assert.notNull(parentOrgProperty, "父级组织属性表不存在");
            orgProperty.setLevel(parentOrgProperty.getLevel() + 1);
        } else {
            orgProperty.setLevel(1);
        }
        getBaseMapper().insert(orgProperty);
        return true;
    }

    @Override
    public boolean updateById(OrgProperty orgProperty) {
        Assert.notNull(orgProperty, "组织属性表不能为空");
        Assert.notNull(orgProperty.getId(), "组织属性表id不能为空");
        if (orgProperty.getPid() != 0) {
            OrgProperty parentOrgProperty = getBaseMapper()
                    .selectOne(Wrappers.<OrgProperty>lambdaQuery().eq(OrgProperty::getId, orgProperty.getPid()));
            Assert.notNull(parentOrgProperty, "父级组织属性表不存在");
            orgProperty.setLevel(parentOrgProperty.getLevel() + 1);
        } else {
            orgProperty.setLevel(1);
        }
        getBaseMapper().updateById(orgProperty);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        OrgProperty orgProperty = getById(id);
        Assert.notNull(orgProperty, "组织属性表不存在");

        // 判断是否存在下级，如果有下级不可进行删除
        if (this.count(Wrappers.<OrgProperty>lambdaQuery().eq(OrgProperty::getPid, id)) > 0) {
            throw new BusinessException("存在下级属性，不可删除");
        }
        // 判断是否绑定组织，如果有绑定组织不可删除
        if (orgMapper.selectCount(Wrappers.<Org>lambdaQuery().eq(Org::getOrgPropertyId, id)) > 0) {
            throw new BusinessException("属性绑定组织，不可删除");
        }

        getBaseMapper().deleteById(id);
        return true;
    }
}
