package org.springblade.manager.feign;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@AllArgsConstructor
public class WbsTreeContractClientImpl implements WbsTreeContractClient {

    private final IContractInfoService contractInfoService;

    private final IWbsTreeContractService wbsTreeContractService;

    @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;
    }
}
