package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.Project;
import cn.com.ctop.common.module.mapper.ProjectMapper;
import cn.com.ctop.common.module.service.IProjectService;
import cn.com.ctop.common.module.utils.Check;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-11-19
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public List<Project> listByMediaType(int projectType, int checkLink) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_id", projectType);
        queryWrapper.eq("check_link", checkLink);
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public String getNameByAccountId(Long accountId) {
        return projectMapper.getNameByAccountId(accountId);
    }

    @Override
    public String getSupplierCodeById(Long projectId) {
        return projectMapper.querySupplierCodeById(projectId);
    }

    @Override
    public List<JSONObject> getProjectListByIds(JSONArray projectIdArray) {
        return projectMapper.getProjectListByIds(projectIdArray);
    }

    @Override
    public Map<Long, String> getProjectMap(Map<Long, Long> projectIdMap) {
        Map<Long, String> projectMap = new HashMap<>();
        if (!Check.isNullMap(projectIdMap)) {
            List<JSONObject> projectList = projectMapper.getListByMap(projectIdMap);
            if (!Check.isNull(projectList)) {
                for (int i = 0; i < projectList.size(); i++) {
                    JSONObject projectJson = projectList.get(i);
                    projectMap.put(projectJson.getLong("id"), projectJson.getString("projectName"));
                }
            }
        }
        return projectMap;
    }

    @Override
    public List<Long> getProjectIdsByProductId(Long productId) {
        return projectMapper.getProjectIdsByProductId(productId);
    }

    @Override
    public List<Long> getProjectIdsByProductIdAndNotIn(Long productId, List<Long> partakeProjectIds) {
        return projectMapper.getProjectIdsByProductIdAndNotIn(productId, partakeProjectIds);
    }

    @Override
    public JSONObject getProjectByAccountId(Long accountId) {
        return projectMapper.getProjectByAccountId(accountId);
    }

    @Override
    public List<Long> getProjectIdsByMediaId(String mediaId) {
        return projectMapper.getProjectIdsByMediaId(mediaId);
    }
}
