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

import java.util.List;

import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.utils.StringUtils;
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.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractInfo;
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.officequalify.entity.Officequalify;
import com.jeeplus.modules.officequalify.dao.OfficequalifyDao;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业资质Service
 * @author liuw
 * @version 2017-12-05
 */
@Service
@Transactional(readOnly = true)
public class OfficequalifyService extends CrudService<OfficequalifyDao, Officequalify> {
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private SerialNumTplService serialNumTplService;
	@Autowired
	private OfficeService officeService;

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

		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(Officequalify officequalify) {
	/*	if (!file.isEmpty() && file.getSize() > 0) {
			OSSClientUtil ossClientUtil = new OSSClientUtil();
			String fileName = file.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
			String url = ossClientUtil.uploadFile2OSS(file, "Officehonor");
			officequalify.setImage(url);
			*//*Workattachment workattachment = new Workattachment();
			workattachment.setUrl(url);
			workattachment.setType(fileType);
			workattachment.setAttachmentName(fileName);
			workattachment.setAttachmentUser(UserUtils.getUser().getId());
			workattachment.setAttachmentId(officehonor.getId());
			workattachment.setAttachmentFlag("60");
			workattachmentService.save(workattachment);*//*
		}*/
		super.save(officequalify);
		if(StringUtils.isBlank(officequalify.getNum())){
			officequalify.setNum(serialNumTplService.genSerialNum(UserUtils.get(officequalify.getCreateBy().getId()).getCompany(), Officequalify.SERIAL_BIZCODE));
			super.save(officequalify);
		}
		//附件信息
		for (WorkClientAttachment workClientAttachment : officequalify.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(officequalify.getId());
				workClientAttachment.setAttachmentFlag("109");
				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(Officequalify officequalify) {
		super.delete(officequalify);
	}
	
	public Page<Office> findPageByoffice(Page<Office> page, Office office) {
		office.setPage(page);
		page.setList(dao.findListByoffice(office));
		return page;
	}
}