/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.officelicense.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.Office;
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.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.officelicense.entity.OfficeLicense;
import com.jeeplus.modules.officelicense.dao.OfficeLicenseDao;

/**
 * 企业证照Service
 * @author lw
 * @version 2018-08-14
 */
@Service
@Transactional(readOnly = true)
public class OfficeLicenseService extends CrudService<OfficeLicenseDao, OfficeLicense> {
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private SerialNumTplService serialNumTplService;

	@Autowired
	private OfficeService officeService;

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

		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(OfficeLicense officeLicense) {
		super.save(officeLicense);
		if(StringUtils.isBlank(officeLicense.getNum())){
			officeLicense.setNum(serialNumTplService.genSerialNum(UserUtils.get(officeLicense.getCreateBy().getId()).getCompany(), OfficeLicense.SERIAL_BIZCODE));
			super.save(officeLicense);
		}
		//附件信息
		for (WorkClientAttachment workClientAttachment : officeLicense.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(officeLicense.getId());
				workClientAttachment.setAttachmentFlag("112");
				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(OfficeLicense officeLicense) {
		super.delete(officeLicense);
	}
	
	
	
	
}