package org.springblade.manager.feign;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mixsmart.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.entity.WbsTreeContract;
import org.springblade.manager.service.IContractInfoService;
import org.springblade.manager.service.IWbsTreeContractService;
import org.springblade.manager.vo.WbsTreeContractTreeVO;
import org.springblade.manager.vo.WbsTreeContractTreeVOS;
import org.springblade.system.entity.DictBiz;
import org.springblade.system.feign.IDictBizClient;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@AllArgsConstructor
public class WbsTreeContractClientImpl implements WbsTreeContractClient {

    private final IContractInfoService contractInfoService;

    private final IWbsTreeContractService wbsTreeContractService;

    private final IDictBizClient dictBizClient;

    @Override
    public Boolean updateContractNodeParameter(WbsTreeContract node) {
        return this.wbsTreeContractService.update(Wrappers.<WbsTreeContract>lambdaUpdate().set(WbsTreeContract::getDeptName, node.getDeptName()).eq(WbsTreeContract::getPKeyId, node.getPKeyId()));
    }

    @Override
    public WbsTreeContract getContractNodeByPrimaryKeyId(String primaryKeyId) {
        return this.wbsTreeContractService.getOne(Wrappers.<WbsTreeContract>lambdaQuery().eq(WbsTreeContract::getPKeyId, primaryKeyId));
    }

    @Override
    public Boolean removeContractTreeNode(String ids) {
        return this.wbsTreeContractService.update(Wrappers.<WbsTreeContract>lambdaUpdate().set(WbsTreeContract::getIsDeleted, 1).in(WbsTreeContract::getPKeyId, Func.toLongList(ids)));
    }

    @Override
    public WbsTreeContract getContractWbsTreeByPrimaryKeyId(Long primaryKeyId) {
        return this.wbsTreeContractService.getOne(Wrappers.<WbsTreeContract>lambdaQuery().eq(WbsTreeContract::getPKeyId, primaryKeyId).eq(WbsTreeContract::getIsDeleted, 0));
    }

    @Override
    public List<WbsTreeContractTreeVOS> queryContractWbsTreeAll(String contractId, Integer wbsType, String parentId) {
        return JSONArray.parseArray(JSONObject.toJSONString(this.contractInfoService.queryContractWbsTreeAll(contractId, wbsType, parentId)), WbsTreeContractTreeVOS.class);
    }

    @Override
    public List<WbsTreeContractTreeVOS> queryContractWbsTreeByContractIdAndType(String contractId, Integer wbsType, String parentId) {
        List<WbsTreeContractTreeVO> vo = this.contractInfoService.queryContractWbsTreeByContractIdAndType(contractId, wbsType, parentId);

        //不知道为什么通过远程调用时实体字段不是想要的，所以中间用字段相同且不继承其它实体的类转换一次
        List<WbsTreeContractTreeVOS> vos = JSONArray.parseArray(JSONObject.toJSONString(vo), WbsTreeContractTreeVOS.class);
        vos.forEach(voData -> voData.setLeaf(new Integer("6").equals(voData.getDeptCategory())));
        return vos;
    }

    @Override
    public List<WbsTreeContractTreeVOS> queryCurrentContractLogList(String contractId) {
        List<WbsTreeContract> result = this.wbsTreeContractService.queryCurrentContractLogList(contractId);
        if(result != null && result.size() != 0){
            List<WbsTreeContractTreeVOS> vosResult = new ArrayList<>();
            result.forEach(node -> {
                //转换
                WbsTreeContractTreeVOS vos = new WbsTreeContractTreeVOS();
                vos.setPrimaryKeyId(String.valueOf(node.getPKeyId()));
                vos.setTitle(StringUtils.isNotEmpty(node.getFullName()) ? node.getFullName() : node.getDeptName());
                //最终返回集合
                vosResult.add(vos);
            });

            List<WbsTreeContractTreeVOS> finalResult = new ArrayList<>();
            //排序顺序
            List<DictBiz> dictBizList = this.dictBizClient.getList("contract_log_sort", "notRoot").getData();
            if(dictBizList == null || dictBizList.size() == 0){
                return vosResult;
            }
            //存在排序，则根据排序规则对数据进行排序
            dictBizList.sort(Comparator.comparingInt(DictBiz::getSort));
            dictBizList.forEach(dictBiz -> {
                Iterator<WbsTreeContractTreeVOS> iterator = vosResult.iterator();
                while (iterator.hasNext()){
                    WbsTreeContractTreeVOS next = iterator.next();
                    if(next.getTitle().contains(dictBiz.getDictValue())){
                        finalResult.add(next);
                        iterator.remove();
                        break;
                    }
                }
            });

            return finalResult;
        }
        return null;
    }
}
