package com.bonus.system.att.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.bonus.common.core.constant.Constants;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.MapVo;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.att.dao.OrgChangeDao;
import com.bonus.system.att.entity.AttGroupBean;
import com.bonus.system.att.entity.AttGroupCheckOrgBean;
import com.bonus.system.att.entity.OrgChangeBean;
import com.bonus.system.att.utils.TreeUtils;
import com.bonus.system.basic.domain.SysTree;
import com.bonus.system.dept.dao.ProDeptRoleDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织架构-业务层
 *
 * @author zys
 */
@Service("OrgChangeService")
public class OrgChangeServiceImpl implements OrgChangeService {

    @Resource(name = "OrgChangeDao")
    private OrgChangeDao orgChangeDao;

    @Resource(name = "ProDeptRoleDao")
    private ProDeptRoleDao proDeptRoleDao;

    @Override
    public List<OrgChangeBean> selectOrgChangeList(OrgChangeBean bean) {
        return orgChangeDao.selectOrgChangeList(bean);
    }

    @Override
    public int deleteAttGroup(Long groupId) {
        return orgChangeDao.deleteAttGroup(groupId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertOrgChange(OrgChangeBean bean) {
        //新增组织架构表
        return orgChangeDao.insertOrgChange(bean);
    }

    @Override
    public OrgChangeBean selectOrgChangeById(Long id) {
        return orgChangeDao.selectOrgChangeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateOrgChange(OrgChangeBean bean) {
        return orgChangeDao.updateOrgChange(bean);
    }

    @Override
    public int orgChangeCheck(OrgChangeBean o) {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        int i = 0;
        if (StringUtils.isNotEmpty(o.getIsCheck())) {
            // 1,2 是审核人审核 0,3 是自己修改
            String examineStatus = "1,2";
            String ownStatus = "0,3";
            if (examineStatus.contains(o.getIsCheck())) {
                o.setCheckUserId(userId);
                SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
                String userName = sysUser.getUserName();
                List<MapVo> orgList = sysUser.getOrgList();
                String orgName = "";
                if (!orgList.isEmpty()){
                   orgName = orgList.get(0).getName();
                }
                if ("1".equals(o.getIsCheck())) {
                    o.setCheckOpinion("同意 " + orgName + " " + userName + " " + DateUtil.now());
                } else {
                    o.setCheckOpinion("不同意 " + orgName + " " + userName + " " + DateUtil.now());
                }
                o.setCheckTime(DateUtil.date());
            } else if (ownStatus.contains(o.getIsCheck())) {

            }
            i = orgChangeDao.orgChangeCheck(o);
            if ("1".equals(o.getIsCheck())) {
                orgChangeDao.updateOrgByUserIdAndOldOrgId(o);
//                orgChangeDao.updateOrgByUserId(o);
            }
        }
        return i;
    }

    @Override
    public List<MapVo> getAttGroupList() {
        return orgChangeDao.getAttGroupList();
    }

    @Override
    public void updateOrgByUserId(OrgChangeBean item) {
        orgChangeDao.updateOrgByUserId(item);
    }

    @Override
    public void updateAttGroupByUserId(OrgChangeBean item) {
        orgChangeDao.updateAttGroupByUserId(item);
    }

    @Override
    public void updateOrgChangeIsApply(OrgChangeBean item) {
        orgChangeDao.updateOrgChangeIsApply(item);
    }


}
