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

import java.util.List;

import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.officequalify.entity.Officequalify;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.entity.Workattachment;
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.officehonor.entity.Officehonor;
import com.jeeplus.modules.officehonor.dao.OfficehonorDao;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业荣誉Service
 * @author liuw
 * @version 2017-12-05
 */
@Service
@Transactional(readOnly = true)
public class OfficehonorService extends CrudService<OfficehonorDao, Officehonor> {
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private SerialNumTplService serialNumTplService;
	@Autowired
	private OfficeService officeService;

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

		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(Officehonor officehonor) {
		/*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");
			officehonor.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(officehonor);
		if(StringUtils.isBlank(officehonor.getNum())){
			officehonor.setNum(serialNumTplService.genSerialNum(UserUtils.get(officehonor.getCreateBy().getId()).getCompany(), Officehonor.SERIAL_BIZCODE));
			super.save(officehonor);
		}
		//附件信息
		for (WorkClientAttachment workClientAttachment : officehonor.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(officehonor.getId());
				workClientAttachment.setAttachmentFlag("110");
				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(Officehonor officehonor) {
		super.delete(officehonor);
	}
	
	public Page<Office> findPageByoffice(Page<Office> page, Office office) {
		office.setPage(page);
		page.setList(dao.findListByoffice(office));
		return page;
	}
	//上传
	@Transactional(readOnly = false)
	public void uploadattachment(MultipartFile file,Officehonor officehonor) {
		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");
			officehonor.setImage(url);
			super.save(officehonor);
			Workattachment workattachment = new Workattachment();
			workattachment.setUrl(url);
			workattachment.setType(fileType);
			workattachment.setAttachmentName(fileName);
			workattachment.setAttachmentUser(UserUtils.getUser().getId());
			workattachment.setAttachmentId(officehonor.getId());
			workattachment.setAttachmentFlag("110");
			workattachmentService.save(workattachment);
		}
	}
	
	
}