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

import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.entity.Office;
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.worksupplier.entity.WorkSupplier;
import com.jeeplus.modules.worksupplier.dao.WorkSupplierDao;

/**
 * 供应商管理Service
 * @author 供应商管理
 * @version 2018-07-10
 */
@Service
@Transactional(readOnly = true)
public class WorkSupplierService extends CrudService<WorkSupplierDao, WorkSupplier> {
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private WorkSupplierDao workSupplierDao;
	@Autowired
	private OfficeDao officeDao;

	public WorkSupplier get(String id) {
		WorkSupplier workSupplier = super.get(id);
		//附件
		WorkClientAttachment workClientAttachment = new WorkClientAttachment();
		workClientAttachment.setAttachmentId(id);
		workClientAttachment.setAttachmentFlag("101");
		workSupplier.setWorkAttachments(workClientAttachmentDao.findList(workClientAttachment));
		return workSupplier;
	}
	
	public List<WorkSupplier> findList(WorkSupplier workSupplier) {
		return super.findList(workSupplier);
	}
	
	public Page<WorkSupplier> findPage(Page<WorkSupplier> page, WorkSupplier workSupplier) {
		workSupplier.getSqlMap().put("dsf", dataScopeBranchOfficeFilter(workSupplier.getCurrentUser(), "s"));
		workSupplier.setPage(page);
		page.setList(findList(workSupplier));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(WorkSupplier workSupplier) {
		super.save(workSupplier);
		//附件信息
		for (WorkClientAttachment workClientAttachment : workSupplier.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(workSupplier.getId());
				workClientAttachment.setAttachmentFlag("101");
				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(WorkSupplier workSupplier) {
		super.delete(workSupplier);
	}
	@Transactional(readOnly = false)
	public void updateSign(WorkSupplier workSupplier) {
		workSupplierDao.updateSign(workSupplier);
	}

	public Page<WorkSupplier> findPageClientInfo(Page<WorkSupplier> page, WorkSupplier workSupplier) {
		workSupplier.setPage(page);
		page.setList(findClientInfoList(workSupplier));
		return page;
	}
	public List<WorkSupplier> findClientInfoList(WorkSupplier workSupplier) {
		Office office =officeDao.get(UserUtils.getSelectOffice().getId());
		if(StringUtils.isNotBlank(office.getBranchOffice())){
			workSupplier.setBranchOffice(office.getBranchOffice());
		}else{
			workSupplier.setCompanyId(UserUtils.getSelectCompany().getId());
		}
		return dao.findClientInfoList(workSupplier);
	}
}