package com.platform.service.impl;

import com.platform.dao.SysDeptDao;
import com.platform.entity.SysDeptEntity;
import com.platform.entity.UserWindowDto;
import com.platform.page.Page;
import com.platform.page.PageHelper;
import com.platform.service.SysDeptService;
import com.platform.utils.Constant;
import com.platform.utils.DateUtils;
import com.platform.vos.SysDeptEntityVo;
import com.qiniu.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("sysDeptService")
public class SysDeptServiceImpl implements SysDeptService {
    @Autowired
    private SysDeptDao sysDeptDao;


    @Override
    public int countQueryListByName(String name, List<Long> deptIdList) {
        return sysDeptDao.queryListByNameCount(name , deptIdList);
    }

    @Override
    public List<SysDeptEntity> queryListByName(String name, List<Long> deptIdList) {
        return sysDeptDao.queryListByName(name , deptIdList);
    }

    @Override
    public SysDeptEntity queryObject(Long deptId) {
        return sysDeptDao.queryObject(deptId);
    }


    @Override
    public SysDeptEntity getDeptByParentDeptId(Long parentDeptId) {
        return sysDeptDao.getDeptByParentDetpId(parentDeptId);
    }

    @Override
    public int queryTotal() {
        return sysDeptDao.queryTotal();
    }

    @Override
    public List<SysDeptEntity> queryList(Map<String, Object> map) {
        return sysDeptDao.queryList(map);
    }

    @Override
    public void save(SysDeptEntity deptEntity) {
        sysDeptDao.save(deptEntity);
    }

    @Override
    public void save(SysDeptEntityVo sysDept) {
        SysDeptEntity deptEntity = new SysDeptEntity();
        BeanUtils.copyProperties(sysDept,deptEntity);
        if(!ObjectUtils.isEmpty(sysDept.getExpirationDate())){
            Date date = null;
            try {
                date = DateUtils.convertStringToDate(sysDept.getExpirationDate());
            } catch (Exception e) {
                throw new RuntimeException("时间格式有误,正确格式：yyyy-MM-dd");
            }
            deptEntity.setExpirationDate(date);
        }
        if(ObjectUtils.isEmpty(sysDept.getDeptId())){
            save(deptEntity);
            sysDept.setDeptId(deptEntity.getDeptId());
        }else{
            update(sysDept);
        }

    }

    @Override
    public void update(SysDeptEntity sysDept) {
        sysDeptDao.update(sysDept);
    }

    @Override
    public void update(SysDeptEntityVo sysDept) {
        SysDeptEntity deptEntity = new SysDeptEntity();
        BeanUtils.copyProperties(sysDept,deptEntity);
        if(!ObjectUtils.isEmpty(sysDept.getExpirationDate())){
            deptEntity.setExpirationDate(DateUtils.convertStringToDate(sysDept.getExpirationDate()));
        }
        sysDeptDao.update(deptEntity);
    }

    @Override
    public void delete(Long deptId) {
        sysDeptDao.delete(deptId);
    }

    @Override
    public List<Long> queryDetpIdList(Long parentId) {
        return sysDeptDao.queryDetpIdList(parentId);
    }

    @Override
    public String getSubDeptIdList(Long deptId) {
        List<Long> deptIdList = getSubDeptIdLongList(deptId);
        String deptFilter = StringUtils.join(deptIdList, ",");
        return deptFilter;
    }

    @Override
    public List<Long> getSubDeptIdLongList(Long deptId) {
        //部门及子部门ID列表
        List<Long> deptIdList = new ArrayList<>();

        //获取子部门ID
        List<Long> subIdList = queryDetpIdList(deptId);
        getDeptTreeList(subIdList, deptIdList);

        //添加本部门
        deptIdList.add(deptId);

        return deptIdList;
    }

    /**
     * 递归
     */
    public void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList) {
        for (Long deptId : subIdList) {
            List<Long> list = queryDetpIdList(deptId);
            if (list.size() > 0) {
                getDeptTreeList(list, deptIdList);
            }

            deptIdList.add(deptId);
        }
    }
}
