/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workprojectnotify.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workprojectnotify.dao.WorkProjectNotifyDao;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通知Service
 * @author yangfan
 * @version 2017-11-10
 */
@Service
@Transactional(readOnly = true)
public class WorkProjectNotifyService extends CrudService<WorkProjectNotifyDao, WorkProjectNotify> {

	@Autowired
	private WorkProjectNotifyDao workProjectNotifyDao;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private UserDao userDao;

	public WorkProjectNotify get(String id) {
		return super.get(id);
	}
	public WorkProjectNotify getByNotifyId(String id) {
		return dao.getByNotifyId(id);
	}

	public List<WorkProjectNotify> findList(WorkProjectNotify workProjectNotify) {
		//workProjectNotify.getSqlMap().put("dsf", dataScopeFilterOR(workProjectNotify.getCurrentUser(), "o", "u","s", MenuStatusEnum.PROJECT_NOTIFY.getValue()));
		return super.findList(workProjectNotify);
	}
	public List<WorkProjectNotify> findListByAPP(WorkProjectNotify workProjectNotify) {
		//workProjectNotify.getSqlMap().put("dsf", dataScopeFilterOR(workProjectNotify.getCurrentUser(), "o", "u","s", MenuStatusEnum.PROJECT_NOTIFY.getValue()));
		return dao.findListByAPP(workProjectNotify);
	}
	public List<WorkProjectNotify> findListByHome(WorkProjectNotify workProjectNotify) {
		try {
			User user = workProjectNotify.getCurrentUser();
			Office branchOffice = new Office();
			String parentIds = user.getOffice().getParentIds();
			if (StringUtils.isNotBlank(parentIds)){
				String[] ids = user.getOffice().getParentIds().split(",");
				for (int i= ids.length-1;i>0;i--){
					Office office = officeService.get(ids[i]);
					if (office.getType().equals("3")){
						branchOffice = office;
						break;
					}
				}
			}
			user.setBranchOffice(branchOffice);
			return super.findList(workProjectNotify);
		}catch (Exception e){
			return super.findList(workProjectNotify);
		}

	}

	public Page<WorkProjectNotify> findPage(Page<WorkProjectNotify> page, WorkProjectNotify workProjectNotify) {
		int count = dao.queryCount(workProjectNotify);
		page.setCount(count);
		page.setCountFlag(false);
		workProjectNotify.setPage(page);
		page.setList(findList(workProjectNotify));
		return page;
	}
	public Page<WorkProjectNotify> findPages(Page<WorkProjectNotify> page, WorkProjectNotify workProjectNotify) {
		workProjectNotify.setPage(page);
		workProjectNotify.getSqlMap().put("dsf", dataScopeFilterOR(workProjectNotify.getCurrentUser(), "o", "u","s", MenuStatusEnum.PROJECT_NOTIFY.getValue()));
		page.setList(findList(workProjectNotify));
		return page;
	}
	public Page<WorkProjectNotify> findPageByAPP(Page<WorkProjectNotify> page, WorkProjectNotify workProjectNotify) {
		workProjectNotify.setPage(page);
		page.setList(findListByAPP(workProjectNotify));
		return page;
	}

	@Transactional(readOnly = false)
	public List<User> readByNotifyId(WorkProjectNotify workProjectNotify) {
		try {
			workProjectNotify.setStatus("1");
			List<User> users = dao.findReadStateByNotifyId(workProjectNotify);
			workProjectNotify.preUpdate();
			dao.updateReadStateByNotifyId(workProjectNotify);
			return users;
		}catch (Exception e){
			return null;
		}
	}
	@Transactional(readOnly = false)
	public void readByNotifyUser(WorkProjectNotify workProjectNotify) {
			workProjectNotify.setStatus("1");
			workProjectNotify.preUpdate();
			dao.updateReadStateByNotifyRoleAndUser(workProjectNotify);
	}

	@Transactional(readOnly = false)
	public int modifyDelflag(WorkProjectNotify workProjectNotify){
		workProjectNotify.setDelFlag("1");
		workProjectNotify.preUpdate();
		return dao.updateDelflagByNotifyId(workProjectNotify);
	}

	@Transactional(readOnly = false)
	public int readByNotifyIdAndNotifyRole(WorkProjectNotify notify) {
		notify.setStatus("1");
		notify.preUpdate();
		return dao.updateReadStateByNotifyIdAndNotifyRole(notify);
	}
	@Transactional(readOnly = false)
	public int readByNotifyIdAndNotifyUser(WorkProjectNotify notify) {
		notify.setStatus("1");
		notify.preUpdate();
		return dao.updateReadStateByNotifyIdAndNotifyUser(notify);
	}

	@Transactional(readOnly = false)
	public int readByNotifyIdAndNotifyUser(WorkProjectNotify notify,String status){
		notify.setStatus(status);
		notify.preUpdate();
		return dao.updateReadStateByNotifyIdAndNotifyUser(notify);
	}
	@Transactional(readOnly = false)
	public void save(WorkProjectNotify workProjectNotify) {
		User user = workProjectNotify.getUser();
		if (user!=null && StringUtils.isNotBlank(user.getId())){
			user = UserUtils.get(user.getId());
			workProjectNotify.setOfficeId(user.getOffice().getId());
		}
		super.save(workProjectNotify);
	}
	@Transactional(readOnly = false)
	public void saveRelation(WorkProjectNotify workProjectNotify) {
		User user = workProjectNotify.getUser();
		if (user!=null && StringUtils.isNotBlank(user.getId())){
			user = userDao.get(user.getId());
			if(user!=null && user.getOffice()!=null && StringUtils.isNotBlank(user.getOffice().getId())){
				workProjectNotify.setOfficeId(user.getOffice().getId());
			}else{
				workProjectNotify.setOfficeId("");
			}
		}
		super.save(workProjectNotify);
	}
	@Transactional(readOnly = false)
	public void insert(WorkProjectNotify workProjectNotify) {
		dao.insert(workProjectNotify);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkProjectNotify workProjectNotify) {
		super.delete(workProjectNotify);
	}


    public WorkProjectNotify getByWorkBidingDocument(String workBidingDocumentId) {
		return workProjectNotifyDao.getByWorkBidingDocument(workBidingDocumentId);
    }

    public WorkProjectNotify getByNotifyIdAndNotifyUser(String notifyId , String notifyUserId){
		return workProjectNotifyDao.getByNotifyIdAndNotifyUser(notifyId,notifyUserId);
	}



}