package com.hzya.frame.sysnew.organ.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.organ.entity.SysOrganEntity;
import com.hzya.frame.sysnew.organ.dao.ISysOrganDao;
import com.hzya.frame.sysnew.organ.service.ISysOrganService;
import com.hzya.frame.sysnew.person.dao.ISysPersonDao;
import com.hzya.frame.sysnew.person.entity.SysPersonEntity;
import com.hzya.frame.sysnew.role.entity.SysRoleEntity;
import com.hzya.frame.sysnew.userCompany.dao.ISysUserCompanyDao;
import com.hzya.frame.sysnew.userCompany.entity.SysUserCompanyEntity;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

import com.hzya.frame.basedao.service.impl.BaseService;

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

/**
 * 组织机构表(Sys_Organ)(SysOrgan)表服务实现类
 *
 * @author makejava
 * @since 2024-03-26 15:24:28
 */
@Service(value = "sysOrganService")
public class SysOrganServiceImpl extends BaseService<SysOrganEntity, String> implements ISysOrganService {

    private ISysOrganDao sysOrganDao;
    @Resource
    private ISysPersonDao sysPersonDao;
    @Resource
    private ISysUserCompanyDao sysUserCompanyDao;
    @Autowired
    public void setSysOrganDao(ISysOrganDao dao) {
        this.sysOrganDao = dao;
        this.dao = dao;
    }
    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询组织树
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntityTree(JSONObject jsonObject) {
        SysOrganEntity entity = getData("jsonStr", jsonObject, SysOrganEntity.class);
        if(entity == null ){
            entity = new SysOrganEntity();
        }
        List<SysOrganEntity> returnOrg = new ArrayList<>();
        //查询符合条件的组织数据
        List<SysOrganEntity> sysOrganEntityList = sysOrganDao.queryByLike(entity);
        if(sysOrganEntityList != null && sysOrganEntityList.size() > 0){
            for (int i = 0; i < sysOrganEntityList.size(); i++) {
                if("0".equals(sysOrganEntityList.get(i).getParentOrganId())){
                    recursiveSetTree(sysOrganEntityList.get(i),sysOrganEntityList);
                    sysOrganEntityList.get(i).setLabel(sysOrganEntityList.get(i).getOrganName());
                    returnOrg.add(sysOrganEntityList.get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询组织树成功", returnOrg);
    }

    /**
     * @Author lvleigang
     * @Description  递归设置树
     * @Date 9:19 上午 2024/3/28
     * @param sysOrganEntity
     * @param sysOrganEntityList
     * @return void
     **/
    private void recursiveSetTree(SysOrganEntity sysOrganEntity, List<SysOrganEntity> sysOrganEntityList) {
        List<SysOrganEntity> children = new ArrayList<>();
        for (int i = 0; i < sysOrganEntityList.size(); i++) {
            if(sysOrganEntityList.get(i).getParentOrganId() != null && !"".equals(sysOrganEntityList.get(i).getParentOrganId())){
                if(sysOrganEntity.getId().equals(sysOrganEntityList.get(i).getParentOrganId())){
                    sysOrganEntityList.get(i).setLabel(sysOrganEntityList.get(i).getOrganName());
                    children.add(sysOrganEntityList.get(i));
                    recursiveSetTree(sysOrganEntityList.get(i),sysOrganEntityList);
                }
            }
        }
        sysOrganEntity.setChildren(children);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存组织
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject jsonObject) {
        SysOrganEntity entity = getData("jsonStr", jsonObject, SysOrganEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getOrganName() == null || "".equals(entity.getOrganName())) {
            return BaseResult.getFailureMessageEntity("请先输入组织名称");
        }
        if (entity.getOrganCode() == null || "".equals(entity.getOrganCode())) {
            return BaseResult.getFailureMessageEntity("请先输入组织代码");
        }
        if (entity.getParentOrganId() == null || "".equals(entity.getParentOrganId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setCreate();
        //保存
        sysOrganDao.save(entity);
        return BaseResult.getSuccessMessageEntity("保存组织成功", entity);
    }
    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取组织
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        SysOrganEntity entity = getData("jsonStr", jsonObject, SysOrganEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = sysOrganDao.get(entity.getId());
        if(entity == null ){
            return BaseResult.getFailureMessageEntity("获取组织失败");
        }
        if(entity.getParentOrganId() != null && !"0".equals(entity.getParentOrganId())){
            SysOrganEntity parentEntity = sysOrganDao.get(entity.getParentOrganId());
            if(entity != null ){
                entity.setParentOrganName(parentEntity.getOrganName());
            }
        }
        return BaseResult.getSuccessMessageEntity("获取组织成功", entity);
    }
    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改组织
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject jsonObject) {
        SysOrganEntity entity = getData("jsonStr", jsonObject, SysOrganEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getOrganName() == null || "".equals(entity.getOrganName())) {
            return BaseResult.getFailureMessageEntity("请先输入组织名称");
        }
        if (entity.getOrganCode() == null || "".equals(entity.getOrganCode())) {
            return BaseResult.getFailureMessageEntity("请先输入组织代码");
        }
        if (entity.getParentOrganId() == null || "".equals(entity.getParentOrganId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        //保存
        sysOrganDao.update(entity);
        return BaseResult.getSuccessMessageEntity("修改组织成功", entity);

    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除组织,只能删除最后一层
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject jsonObject) {
        SysOrganEntity entity = getData("jsonStr", jsonObject, SysOrganEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if(!checkOrgUse(entity)){
            return BaseResult.getFailureMessageEntity("当前组织已经使用，不允许删除");
        }
        entity.setUpdate();
        sysOrganDao.logicRemove(entity);
        return BaseResult.getSuccessMessageEntity("删除组织成功");
    }
    /**
     * @Author lvleigang
     * @Description  校验组织是否使用 目前只校验（用户\人员）
     * @Date 3:27 下午 2024/3/27
     * @param entity
     * @return boolean
     **/
    private boolean checkOrgUse(SysOrganEntity entity) {
        SysPersonEntity sysPersonEntity = new SysPersonEntity();
        sysPersonEntity.setOrganId(entity.getId());
        if(sysPersonDao.getCount(sysPersonEntity) > 0){
            return false;
        }
        SysUserCompanyEntity sysUserCompanyEntity = new SysUserCompanyEntity();
        sysUserCompanyEntity.setUserCompanyId(entity.getId());
        if(sysUserCompanyDao.getCount(sysUserCompanyEntity) > 0){
            return false;
        }
        return true;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 启用停用组织
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
        SysOrganEntity entity = getData("jsonStr", jsonObject, SysOrganEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getState() == null || "".equals(entity.getState())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //0启用 1 停用
        if("0".equals(entity.getState())){
            entity.setUpdate();
            sysOrganDao.update(entity);
            return BaseResult.getSuccessMessageEntity("启用组织成功");
        }else {
            //人员修改为停用状态
            SysPersonEntity sysPersonEntity = new SysPersonEntity();
            sysPersonEntity.setOrganId(entity.getId());
            sysPersonEntity.setState(entity.getState());
            sysPersonEntity.setUpdate();
            sysPersonDao.update(sysPersonEntity);

            //删除用户关联的公司
            SysUserCompanyEntity sysUserCompanyEntity = new SysUserCompanyEntity();
            sysUserCompanyEntity.setUserCompanyId(entity.getId());
            sysUserCompanyEntity.setUpdate();
            sysUserCompanyDao.logicRemoveMultiCondition(sysUserCompanyEntity);

            entity.setUpdate();
            sysOrganDao.update(entity);
            return BaseResult.getSuccessMessageEntity("停用组织成功");
        }
    }
}
