package cn.com.ctop.crawler.modules.pangolin.service.impl;

import cn.com.ctop.common.module.service.IBindAccountLoginService;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.common.module.vo.PangolinProjectVO;
import cn.com.ctop.crawler.modules.pangolin.entity.PangolinApp;
import cn.com.ctop.crawler.modules.pangolin.mapper.PangolinAppMapper;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinAppService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PangolinAppServiceImpl extends ServiceImpl<PangolinAppMapper, PangolinApp> implements PangolinAppService {
    @Autowired
    private PangolinAppMapper pangolinAppMapper;
    @Autowired
    private IBindAccountLoginService bindAccountLoginService;
    @Override
    public Map<String, Object> getListByUserId(String userId, JSONObject data) {
        Map<String,Object>result = new HashMap<>();
        List<Long>projectIds = null;
        List<String>accountNames = null;
        JSONArray accountNameArray = data.getJSONArray("accountName");
        JSONArray projectIdArray = data.getJSONArray("projectId");
        if(null!=accountNameArray&&!accountNameArray.isEmpty()){
            accountNames = new ArrayList<>();
            for(int i=0;i<accountNameArray.size();i++){
                accountNames.add(accountNameArray.getString(i));
            }
        }
        if(null!=projectIdArray&&!projectIdArray.isEmpty()){
            projectIds = new ArrayList<>();
            for(int i=0;i<projectIdArray.size();i++){
                projectIds.add(projectIdArray.getLong(i));
            }
        }
        List<PangolinApp>appList = pangolinAppMapper.getListByParams(userId,projectIds,accountNames);
        result.put("data",appList);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> getMemberList(String userId) {
        Map<String,Object>resultMap = new HashMap<>();
        //1:查询所有项目信息
        List<PangolinProjectVO> projectVOS = bindAccountLoginService.getProjectByParams(userId, CtopAdConstant.PLATFORM_TYPE_PANGOLIN_PY);
        resultMap.put("data",projectVOS);
        ResultMapUtils.setResultMap(resultMap,StatusCode.COMMON_SUCCESS);
        return resultMap;
    }

    @Override
    public void saveOrUpdateApp(PangolinApp app) {
        QueryWrapper<PangolinApp>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name",app.getAccountName());
        queryWrapper.eq("app_id",app.getAppId());
        queryWrapper.last(" limit 1");
        PangolinApp getApp = this.getOne(queryWrapper);
        if(null == getApp){
            this.save(app);
        }else{
            getApp.setName(app.getName());
            this.updateById(getApp);
        }
    }
}
