package com.ruoyi.asset.service.impl;

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

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.TbLocationMapper;
import com.ruoyi.asset.domain.TbLocation;
import com.ruoyi.asset.service.ITbLocationService;

import javax.annotation.Resource;

/**
 * 所属位置Service业务层处理
 * 
 * @author 原动力
 * @date 2023-05-15
 */
@Service
public class TbLocationServiceImpl implements ITbLocationService 
{
    @Resource
    private TbLocationMapper tbLocationMapper;

    /**
     * 查询所属位置
     * 
     * @param id 所属位置主键
     * @return 所属位置
     */
    @Override
    public TbLocation selectTbLocationById(Long id)
    {
        return tbLocationMapper.selectTbLocationById(id);
    }

    /**
     * 查询所属位置列表
     * 
     * @param tbLocation 所属位置
     * @return 所属位置
     */
    @Override
    public List<TbLocation> selectTbLocationList(TbLocation tbLocation)
    {
        return tbLocationMapper.selectTbLocationList(tbLocation);
    }

    @Override
    public List<TbLocation> selectTree(Long id) {
        List<TbLocation> locations = new ArrayList<>();
        if (id != null) {
            TbLocation location = selectTbLocationById(id);
            locations.add(location);
        } else {
            TbLocation location = new TbLocation();
            location.setParentId(0L);
            locations = selectTbLocationList(location);
        }
        for (TbLocation location : locations) {
            List<TbLocation> tbLocations = buildChildren(location);
            location.setChildren(tbLocations);
        }
        return locations;
    }

    private List<TbLocation> buildChildren(TbLocation location) {
        TbLocation sunLocation = new TbLocation();
        sunLocation.setParentId(location.getId());
        List<TbLocation> locations = selectTbLocationList(sunLocation);
        if (locations != null && !locations.isEmpty()) {
            for (TbLocation tbLocation : locations) {
                List<TbLocation> tbLocations = buildChildren(tbLocation);
                tbLocation.setChildren(tbLocations);
            }
        }
        return locations;
    }

    /**
     * 新增所属位置
     * 
     * @param tbLocation 所属位置
     * @return 结果
     */
    @Override
    public int insertTbLocation(TbLocation tbLocation)
    {
        Long parentId = tbLocation.getParentId();
        if (parentId == null || parentId == 0) {
            tbLocation.setParentId(0L);
        } else {
            if (selectTbLocationById(parentId) == null) {
                return 0;
            }
        }

        String dateTimeId = DateUtils.dateTimeNow("yyyyMMddHHmmssSSS");
        String orderNumber = "WZ" + dateTimeId;

        tbLocation.setNumber(orderNumber);
        tbLocation.setCreateTime(DateUtils.getNowDate());
        tbLocation.setCreateBy(SecurityUtils.getUsername());
        int flag = tbLocationMapper.insertTbLocation(tbLocation);
        if (flag < 1) {
            return flag;
        }

        HashSet<Long> set = new HashSet<>();

        while (parentId != null && parentId > 0) {
            TbLocation location = selectTbLocationById(parentId);
            set.add(location.getId());
            parentId = location.getParentId();
        }

        StringBuilder stringBuilder = new StringBuilder("");
        if (!set.isEmpty()) {
            for (Long id : set) {
                stringBuilder.append(id);
                stringBuilder.append(",");
            }
        }
        stringBuilder.append(tbLocation.getId());
        tbLocation.setSequence(stringBuilder.toString());
        return tbLocationMapper.updateTbLocation(tbLocation);
    }

    /**
     * 修改所属位置
     * 
     * @param tbLocation 所属位置
     * @return 结果
     */
    @Override
    public int updateTbLocation(TbLocation tbLocation)
    {
        tbLocation.setUpdateTime(DateUtils.getNowDate());
        return tbLocationMapper.updateTbLocation(tbLocation);
    }

    /**
     * 批量删除所属位置
     * 
     * @param ids 需要删除的所属位置主键
     * @return 结果
     */
    @Override
    public int deleteTbLocationByIds(Long[] ids)
    {
        return tbLocationMapper.deleteTbLocationByIds(ids);
    }

    /**
     * 删除所属位置信息
     * 
     * @param id 所属位置主键
     * @return 结果
     */
    @Override
    public int deleteTbLocationById(Long id)
    {
        return tbLocationMapper.deleteTbLocationById(id);
    }
}
