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

import java.util.ArrayList;
import java.util.List;

import com.jeeplus.modules.workcontent.entity.WorkContentCompanyType;
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.workcontent.entity.WorkContentType;
import com.jeeplus.modules.workcontent.dao.WorkContentTypeDao;

/**
 * 工作内容类型Service
 * @author ssrh
 * @version 2018-06-01
 */
@Service
@Transactional(readOnly = true)
public class WorkContentTypeService extends CrudService<WorkContentTypeDao, WorkContentType> {

	public WorkContentType get(String id) {
		return super.get(id);
	}

	@Transactional(readOnly = false)
	public List<WorkContentType> findList(WorkContentType workContentType) {
        List<WorkContentType> list = super.findList(workContentType);
        if (list==null||list.isEmpty()){
            int count = dao.countContentType(workContentType.getCompany());
            if(count==0){
                list = dao.findAllList(new WorkContentType());
                List<WorkContentCompanyType> workContentCompanyTypeList = new ArrayList<>(list.size());
                for (WorkContentType contentType : list) {
                    WorkContentCompanyType companyType = new WorkContentCompanyType();
                    workContentCompanyTypeList.add(companyType);
                    companyType.setCompany(workContentType.getCompany());
                    companyType.setTypeId(contentType.getTypeId());
                    companyType.setDelFlag("0");
                }
                dao.batchInsertComType(workContentCompanyTypeList);
            }
        }
        return list;
    }
    @Transactional(readOnly = false)
    public List<WorkContentType> findProjectList(WorkContentType workContentType) {
        List<WorkContentType> list = dao.findProjectList(workContentType);
        if (list==null||list.isEmpty()){
            int count = dao.countContentType(workContentType.getCompany());
            if(count==0){
                list = dao.findAllList(new WorkContentType());
                List<WorkContentCompanyType> workContentCompanyTypeList = new ArrayList<>(list.size());
                for (WorkContentType contentType : list) {
                    WorkContentCompanyType companyType = new WorkContentCompanyType();
                    workContentCompanyTypeList.add(companyType);
                    companyType.setCompany(workContentType.getCompany());
                    companyType.setTypeId(contentType.getTypeId());
                    companyType.setDelFlag("0");
                }
                dao.batchInsertComType(workContentCompanyTypeList);
            }
        }
        return list;
    }
	public Page<WorkContentType> findPage(Page<WorkContentType> page, WorkContentType workContentType) {
		return super.findPage(page, workContentType);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkContentType workContentType) {
		super.save(workContentType);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkContentType workContentType) {
		super.delete(workContentType);
	}

    @Transactional(readOnly = false)
    public void deleteByLogic(WorkContentType workContentType) {
        dao.deleteByLogic(workContentType);
    }

    @Transactional(readOnly = false)
    public String changeStatus(WorkContentType workContentType) {
	    String msg = null;
        if("1".equals(workContentType.getDelFlag())){
	        workContentType.setDelFlag(WorkContentType.DEL_FLAG_NORMAL);
	        msg = "恢复";
        }else {
            workContentType.setDelFlag(WorkContentType.DEL_FLAG_DELETE);
            msg = "禁用";
        }
	    dao.changeStatus(workContentType);
        return msg;
    }
}