package com.jpsoft.shinestar.modules.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.CompanyPositionDAO;
import com.jpsoft.shinestar.modules.base.entity.CompanyPosition;
import com.jpsoft.shinestar.modules.base.service.CompanyPositionService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Transactional
@Component(value="companyPositionService")
public class CompanyPositionServiceImpl implements CompanyPositionService {
	@Resource(name="companyPositionDAO")
	private CompanyPositionDAO companyPositionDAO;

	@Override
	public CompanyPosition get(String id) {
		// TODO Auto-generated method stub
		return companyPositionDAO.get(id);
	}

	@Override
	public int insert(CompanyPosition model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());

		return companyPositionDAO.insert(model);
	}

	@Override
	public int update(CompanyPosition model) {
		// TODO Auto-generated method stub
		return companyPositionDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return companyPositionDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = companyPositionDAO.exist(id);

		return count > 0 ? true : false;
	}

	@Override
	public List<CompanyPosition> findByCompanyId(String companyId){
		return companyPositionDAO.findByCompanyId(companyId);
	}

	@Override
	public List<CompanyPosition> list() {
		// TODO Auto-generated method stub
		return companyPositionDAO.list();
	}

	@Override
	public Page<CompanyPosition> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<CompanyPosition> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            companyPositionDAO.search(searchParams,sortList);
        });

        return page;
	}

	@Override
	public List<String> findAllPosition(CompanyPosition companyPosition) {

		List<String> structureNameList = new ArrayList<>();
		String structureName = null;
		if (StringUtils.isNotBlank(companyPosition.getPosition1Name())) {
			structureName = companyPosition.getPosition1Name();
		}
		if (StringUtils.isNotBlank(companyPosition.getPosition2Name())) {
			structureName = structureName + "," + companyPosition.getPosition2Name();
		}
		if (StringUtils.isNotBlank(companyPosition.getPosition3Name())) {
			structureName = structureName + "," + companyPosition.getPosition3Name();
		}
		if (StringUtils.isNotBlank(companyPosition.getPosition4Name())) {
			structureName = structureName + "," + companyPosition.getPosition4Name();
		}
		if (StringUtils.isNotBlank(companyPosition.getPosition5Name())) {
			structureName = structureName + "," + companyPosition.getPosition5Name();
		}
		if (StringUtils.isNotBlank(structureName)) {
			//  throw new Exception(companyInfo.getName() + "未设置机构层级");
			structureNameList = Arrays.asList(structureName.split(","));
		}
		return structureNameList;
	}
}