package org.springblade.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.manager.dto.WbsTreePrivateDTO2;
import org.springblade.manager.entity.ContractInfo;
import org.springblade.manager.entity.WbsInfo;
import org.springblade.manager.entity.WbsTreePrivate;

import org.springblade.manager.mapper.ContractInfoMapper;
import org.springblade.manager.mapper.WbsInfoMapper;
import org.springblade.manager.mapper.WbsTreeContractMapper;
import org.springblade.manager.mapper.WbsTreePrivateMapper;
import org.springblade.manager.service.IWbsTreePrivateService;
import org.springblade.manager.vo.WbsNodeTableVO;
import org.springblade.manager.vo.WbsTreePrivateVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@AllArgsConstructor
public class WbsTreePrivateServiceImpl extends BaseServiceImpl<WbsTreePrivateMapper, WbsTreePrivate> implements IWbsTreePrivateService {

    private final WbsTreeContractMapper wbsTreeContractMapper;
    private final ContractInfoMapper contractInfoMapper;
    private final WbsInfoMapper wbsInfoMapper;

    @Override
    public List<WbsTreePrivateVO> tree(String wbsId, String projectId) {
        return ForestNodeMerger.merge(baseMapper.tree(wbsId, projectId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submit(WbsTreePrivate wbsTreePrivate) {
        if (wbsTreePrivate.getPKeyId() != null && !("").equals(wbsTreePrivate.getPKeyId())) {
            Long pKeyId = wbsTreePrivate.getPKeyId();
            //修改
            int row = baseMapper.updateByPKeyId(pKeyId, wbsTreePrivate);
            if (row > 0) {
                WbsTreePrivate wbsTreePrivate1 = baseMapper.getByPKeyId(pKeyId);
                //同步合同段wbs库
                wbsTreeContractMapper.updateByCondition1(wbsTreePrivate1);
                return true;
            }
        } else {
            wbsTreePrivate.setIsDeleted(0);
            wbsTreePrivate.setStatus(1);
            Long pKeyId = SnowFlakeUtil.getId();
            wbsTreePrivate.setPKeyId(pKeyId);
            wbsTreePrivate.setWbsType(String.valueOf(1)); //默认质检
            WbsTreePrivate parent = baseMapper.getByCondition(wbsTreePrivate.getParentId(), wbsTreePrivate.getProjectId(), wbsTreePrivate.getWbsId());
            if (parent != null) {
                String ancestors = parent.getAncestors() + "," + wbsTreePrivate.getParentId();
                wbsTreePrivate.setAncestors(ancestors);
            }
            //新增
            int row = baseMapper.insert(wbsTreePrivate);
            if (row > 0) {
                //获取当前项目下所有合同段id
                QueryWrapper<ContractInfo> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("p_id", wbsTreePrivate.getProjectId());
                List<ContractInfo> contractInfos = contractInfoMapper.selectList(queryWrapper);
                WbsInfo wbsInfo = wbsInfoMapper.selectById(wbsTreePrivate.getWbsId());
                Integer wbsType = wbsInfo.getWbsType();
                for (ContractInfo contractInfo : contractInfos) {
                    //同步新增合同段wbs库
                    Long pKeyId1 = SnowFlakeUtil.getId();
                    wbsTreeContractMapper.insertByCondition(wbsTreePrivate, contractInfo.getId(), pKeyId1, wbsType);
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public List<WbsNodeTableVO> selectByNodeTable(String id, String wbsId, String projectId) {
        //查询节点下的所有表单
        List<WbsNodeTableVO> wbsNodeTableVOS = baseMapper.selectByNodeTable(id, wbsId, projectId);
        return wbsNodeTableVOS;
    }

    @Override
    public boolean removeTableByCondition(String id, String wbsId, String projectId) {
        int integer = baseMapper.updateByCondition1(id, wbsId, projectId);
        if (integer > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<WbsTreePrivateVO> lazyTree(String wbsId, String tenantId, Long parentId, String projectId) {
        if (AuthUtil.isAdministrator()) {
            tenantId = StringPool.EMPTY;
        }
        return ForestNodeMerger.merge(baseMapper.lazyTree(wbsId, tenantId, parentId, projectId));
    }

    @Override
    public List<WbsTreePrivateDTO2> findWbsTreePrivateSameLevel(String projectId, String parentId, String wbsId) {
        return baseMapper.selectNodeListByCondition(projectId, parentId, wbsId);
    }

    @Override
    public boolean wbsTreePrivateSort(List<WbsTreePrivateDTO2> wbsTreeDTO) {
        int number = 1;
        for (WbsTreePrivateDTO2 wbsTree : wbsTreeDTO) {
            wbsTree.setSort(number);
            //修改sort
            baseMapper.updateSortById(wbsTree.getPKeyId(), wbsTree.getSort());
            number++;
        }
        return true;
    }

    @Override
    public boolean wbsTreePrivateTableSort(List<WbsTreePrivateDTO2> wbsTreeDTO) {
        int number = 1;
        for (WbsTreePrivateDTO2 wbsTree : wbsTreeDTO) {
            wbsTree.setSort(number);
            //修改sort
            baseMapper.updateSortById2(wbsTree.getPKeyId(), wbsTree.getSort());
            number++;
        }
        return true;
    }

    @Override
    public int deleteLogicBypKeyId(String pKeyId) {
        return baseMapper.updateIsDeletedByPKeyId(pKeyId);
    }

    @Override
    public List<WbsTreePrivate> findWbsTreePrivate(String ids) {
        return baseMapper.selectList(Wrappers.<WbsTreePrivate>query().lambda().eq(WbsTreePrivate::getProjectId, ids));
    }


    public List<WbsTreePrivate> findAllNodeList(String wbsTreeIds, String projectId, String wbsId) {
        String[] ids = wbsTreeIds.split(",");
        List<Long> idList = new ArrayList<>();
        List<WbsTreePrivate> list = new ArrayList<>();
        for (String id : ids) {
            idList.add(Long.valueOf(id));
            WbsTreePrivate wbsTreePrivate = baseMapper.selectByCondition(id, projectId, wbsId);
            list.add(wbsTreePrivate);
        }
        return list;
    }

}
