package com.persagy.proxy.object.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.service.impl.AbstractAdmBaseServiceImpl;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.object.model.AdmProject;
import com.persagy.proxy.object.service.IAdmProjectService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ADM 项目 实现类
 * @author Charlie Yu
 * @date 2021-08-16
 */
@Service
public class AdmProjectServiceImpl extends AbstractAdmBaseServiceImpl<AdmProject> implements IAdmProjectService {

    /**
     * 查询
     *
     * @param context
     * @param request
     * @return
     */
    @Override
    public AdmResponse doQuery(InstanceUrlParam context, AdmQueryCriteria request) {
        AdmResponse response = super.doQuery(context,request,AdmProject.class);
        List<AdmProject> vos = (List<AdmProject>) response.getContent();
        response.setContent(processDataQuery(vos));
        return response;
    }

    /**
     * 修改
     *
     * @param context
     * @param voList
     * @return
     */
    @Override
    public List<AdmProject> doUpdate(InstanceUrlParam context, List<AdmProject> voList) {
        List<AdmProject> admProjects = super.doUpdate(context, AdmProject.class, processDataEdit(voList));
        return processDataQuery(admProjects);
    }

    @Override
    public List<AdmProject> processDataEdit(List<AdmProject> vos) {
        for(AdmProject admProject : vos){
            if(CollUtil.isEmpty(admProject.getInfos())){
                continue;
            }
            if(admProject.getInfos().containsKey("contractor")){
                admProject.getInfos().put("constructor",admProject.getInfos().get("contractor"));
                admProject.getInfos().remove("contractor");
            }
        }
        return vos;
    }

    @Override
    public List<AdmProject> processDataQuery(List<AdmProject> vos) {
        for(AdmProject admProject : vos){
            if(CollUtil.isEmpty(admProject.getInfos())){
                continue;
            }
            if(admProject.getInfos().containsKey("constructor")){
                admProject.getInfos().put("contractor",admProject.getInfos().get("constructor"));
                admProject.getInfos().remove("constructor");
            }
        }
        return vos;
    }
}
