package com.hs.admin.service.impl;

import com.hs.admin.common.enums.StateType;
import com.hs.admin.mapper.DicOrgMapper;
import com.hs.admin.mapper.SysUserOrgRsMapper;
import com.hs.admin.model.DicOrg;
import com.hs.admin.model.SysUserOrgRs;
import com.hs.admin.model.reqmodel.OrgReq;
import com.hs.admin.model.respmodel.DicOrgList;
import com.hs.admin.model.respmodel.SysUserOrgRsList;
import com.hs.admin.service.DicOrgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class DicOrgServiceImpl implements DicOrgService {
    @Resource
    private DicOrgMapper dicOrgMapper;
    @Resource
    private SysUserOrgRsMapper sysUserOrgRsMapper;

    private List<DicOrgList> getLevelData(List<DicOrgList> list, Integer parentcode) {
        List<DicOrgList> resultList = new ArrayList<>();
        for (DicOrgList data : list) {
            if (data.getParentId() == parentcode) {
                List<DicOrgList> childList = getLevelData(list, data.getId());
                data.setDicOrgList(childList);
                resultList.add(data);
            }
        }
        return resultList;
    }


    public List<DicOrgList> getParentAll(OrgReq.GetOrgReq org) {
        List<DicOrgList> list = dicOrgMapper.selectParentAll();
        if (org.getOrgName() != null) {
            list = list.stream().filter(
                    o -> (o.getOrgName() != null && o.getOrgName().toLowerCase().contains(org.getOrgName().toLowerCase())))
                    .collect(Collectors.toList());
        }
        List<DicOrgList> resultList = getLevelData(list, ORG_TOP_PARENT_ID);
        return resultList;
    }

    public List<DicOrg> getAll() {
        List<DicOrg> list = dicOrgMapper.selectAll();
        return list;
    }

    public DicOrg info(Integer orgId) {
        DicOrg dicOrg = dicOrgMapper.selectByPrimaryKey(orgId);
        return dicOrg;
    }

    public boolean addOrg(Integer userId, OrgReq.AddOrgReq org) {
        if (org != null) {
            DicOrg dicOrg = new DicOrg();
            dicOrg.setCreateDate(new Date());
            dicOrg.setCreateUserid(Long.valueOf(userId));
            dicOrg.setState((short) StateType.ON.getValue());
            dicOrg.setOrgCode(org.getOrgCode());
            dicOrg.setOrgName(org.getOrgName());
            dicOrg.setOrgShortName(org.getOrgShortName());
            dicOrg.setOrgGroup(org.getOrgGroup());
            dicOrg.setOrgType(org.getOrgType());
            dicOrg.setOrgAddress(org.getOrgAddress());
            dicOrg.setParentId(org.getParentId());
            dicOrg.setIsNew(org.getIsNew());
            dicOrgMapper.insert(dicOrg);
            return true;
        }
        return false;
    }

    public boolean deleteOrg(OrgReq.DeleteOrgReq org) {
        if (org != null) {
            DicOrg dicOrg = dicOrgMapper.selectByPrimaryKey(org.getOrgId());
            if (dicOrg != null) {
                dicOrg.setState((short) StateType.OFF.getValue());
                dicOrgMapper.updateByPrimaryKey(dicOrg);
                return true;
            }
        }
        return false;
    }

    public boolean updateOrg(OrgReq.UpdateOrgReq org) {
        if (org != null) {
            DicOrg dicOrg = dicOrgMapper.selectByPrimaryKey(org.getOrgId());
            if (dicOrg != null) {
                dicOrg.setOrgCode(org.getOrgCode());
                dicOrg.setOrgName(org.getOrgName());
                dicOrg.setOrgShortName(org.getOrgShortName());
                dicOrg.setOrgGroup(org.getOrgGroup());
                dicOrg.setOrgType(org.getOrgType());
                dicOrg.setOrgAddress(org.getOrgAddress());
                dicOrg.setParentId(org.getParentId());
                dicOrg.setIsNew(org.getIsNew());
                dicOrgMapper.updateByPrimaryKey(dicOrg);
                return true;
            }
        }
        return false;
    }

    public DicOrg getByName(String orgName) {
        return dicOrgMapper.selectByName(orgName);
    }

    /**
     * 查询用户机构
     *
     * @param userId 操作人ID
     * @return
     */
    @Override
    public List<SysUserOrgRsList> getUserOrg(Integer userId) {
        List<SysUserOrgRsList> sysUserOrgRs = sysUserOrgRsMapper.selectByUser(userId);
        return sysUserOrgRs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserOrg(Long userId, Long saveUserId, List<Long> org) {
        int result = 0;
        if (org != null && org.size() > 0 && saveUserId != null) {
            sysUserOrgRsMapper.deleteByUser(saveUserId);
            for (Long orgId : org) {
                //判断是否存在关系数据
                SysUserOrgRs sysUserOrgRs = sysUserOrgRsMapper.selectByUserWithOrg(saveUserId, orgId);
                if (sysUserOrgRs != null) {
                    //开启状态
                    sysUserOrgRs.setState((short) StateType.ON.getValue());
                    result += sysUserOrgRsMapper.updateByPrimaryKey(sysUserOrgRs);

                } else {
                    //添加
                    sysUserOrgRs = new SysUserOrgRs();
                    sysUserOrgRs.setOrgId(orgId);
                    sysUserOrgRs.setUserId(saveUserId);
                    sysUserOrgRs.setCreateDate(new Date());
                    sysUserOrgRs.setCreateUserid(userId);
                    sysUserOrgRs.setState((short) StateType.ON.getValue());
                    result += sysUserOrgRsMapper.insert(sysUserOrgRs);
                }
            }
        }
        return result > 0;
    }


}
