package com.palmnest.service.notice.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.notice.ArticleCensusDao;
import com.palmnest.model.notice.ArticleCensusModel;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.notice.ArticleCensusManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scala.annotation.meta.param;

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

/**
 * 组织架构L1 - ArticleCensusManagerImpl
 *
 * @author: jiangyong
 * @email: jiangyong@avic-s.com
 * @date: 2019/12/14 14:24
 */
@Service("articleCensusManager")
public class ArticleCensusManagerImpl extends GenericManagerImpl<ArticleCensusModel, Integer> implements ArticleCensusManager {
	@Autowired
	private ArticleCensusDao articleCensusDao;

	@Override
	public Page getDataList(Map<String, Object> map, int pageNo, int pageSize) throws Exception {
		return null;
	}

	@Override
	public Integer getTotal(Map<String, Object> map) {
		return articleCensusDao.getTotal(map);
	}

	@Override
	public void saveOrUpdate(Integer artId, String orgIds) {

		//param.put("dayInfo", dayInfo);
		String [] orgArr =orgIds.split(",");
		for (String orgId:
			 orgArr) {
			Map<String, Object> param = new HashMap<>();
			param.put("orgId", orgId);
			param.put("artId", artId);
			List<ArticleCensusModel> list = articleCensusDao.getListByParam(param);
			if (list != null && list.size() > 0) {
				ArticleCensusModel articleCensusModel = list.get(0);
				Integer            count              = articleCensusModel.getCount();
				count = Math.addExact(count == null ? 0 : count, 1);
				articleCensusModel.setCount(count);
				articleCensusModel.setCreatedBy(1);
				articleCensusModel.setUpdatedAt(new Date());
				articleCensusDao.updateObj(articleCensusModel);
			} else {
				ArticleCensusModel articleCensusModel = new ArticleCensusModel();
				articleCensusModel.setArtId(artId);
				articleCensusModel.setOrgId(Integer.parseInt(orgId));
				articleCensusModel.setCount(1);
				articleCensusModel.setCreatedAt(new Date());
				articleCensusModel.setEnabled(1);
				articleCensusModel.setPlatformType(2);
				articleCensusDao.saveObj(articleCensusModel);
			}
		}


	}

}
