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

import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.officehonor.entity.Officehonor;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.WorkattachmentService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.officeintroduce.entity.Officeintroduce;
import com.jeeplus.modules.officeintroduce.dao.OfficeintroduceDao;

/**
 * 企业介绍Service
 * @author liuw
 * @version 2017-12-05
 */
@Service
@Transactional(readOnly = true)
public class OfficeintroduceService extends CrudService<OfficeintroduceDao, Officeintroduce> {
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private SerialNumTplService serialNumTplService;
	@Autowired
	private OfficeService officeService;

	public Officeintroduce get(String id) {
		Officeintroduce officeintroduce = super.get(id);
		//附件
		WorkClientAttachment workClientAttachment = new WorkClientAttachment();
		workClientAttachment.setAttachmentId(id);
		workClientAttachment.setAttachmentFlag("111");
		officeintroduce.setWorkAttachments(workClientAttachmentDao.findList(workClientAttachment));
		return officeintroduce;
	}
	
	public List<Officeintroduce> findList(Officeintroduce officeintroduce) {
		return super.findList(officeintroduce);
	}
	
	public Page<Officeintroduce> findPage(Page<Officeintroduce> page, Officeintroduce officeintroduce) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			if(officeintroduce.getOffice() == null || StringUtils.isBlank(officeintroduce.getOffice().getId())){
				officeintroduce.setOffice(UserUtils.getSelectOffice().getParent());
			}else{
				Office office = officeService.get(officeintroduce.getOffice().getId());
				if("1".equals(office.getType())){
					//总公司
					officeintroduce.setCompanyId(officeintroduce.getOffice().getId());
				}else{
					officeintroduce.setOffice(officeintroduce.getOffice());
				}
			}
		}
		officeintroduce.setPage(page);
		page.setList(findList(officeintroduce));

		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(Officeintroduce officeintroduce) {
		super.save(officeintroduce);
		if(StringUtils.isBlank(officeintroduce.getNum())){
			officeintroduce.setNum(serialNumTplService.genSerialNum(UserUtils.get(officeintroduce.getCreateBy().getId()).getCompany(), Officeintroduce.SERIAL_BIZCODE));
			super.save(officeintroduce);
		}
		//附件信息
		for (WorkClientAttachment workClientAttachment : officeintroduce.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(officeintroduce.getId());
				workClientAttachment.setAttachmentFlag("111");
				workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
				if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
					workClientAttachment.preInsert();
					workClientAttachmentDao.insert(workClientAttachment);
				} else {
					workClientAttachment.preUpdate();
					workClientAttachmentDao.update(workClientAttachment);
				}
			} else {
				workClientAttachmentDao.delete(workClientAttachment);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Officeintroduce officeintroduce) {
		super.delete(officeintroduce);
	}
	
	public Page<Office> findPageByoffice(Page<Office> page, Office office) {
		office.setPage(page);
		page.setList(dao.findListByoffice(office));
		return page;
	}
	
	
	
}