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

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import com.jpsoft.shinestar.modules.workflow.dao.ProcessActUserDAO;
import com.jpsoft.shinestar.modules.workflow.dao.ProcessTemplateDAO;
import com.jpsoft.shinestar.modules.workflow.entity.ProcessActUser;
import com.jpsoft.shinestar.modules.workflow.entity.ProcessTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.workflow.dao.ProcessActivityDAO;
import com.jpsoft.shinestar.modules.workflow.entity.ProcessActivity;
import com.jpsoft.shinestar.modules.workflow.service.ProcessActivityService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="processActivityService")
public class ProcessActivityServiceImpl implements ProcessActivityService {
	@Resource(name="processActivityDAO")
	private ProcessActivityDAO processActivityDAO;

	@Resource(name="processActUserDAO")
	private ProcessActUserDAO processActUserDAO;

	@Resource(name="processTemplateDAO")
	private ProcessTemplateDAO processTemplateDAO;

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

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

		return processActivityDAO.insert(model);
	}

	@Override
	public int insertAndActUser(ProcessActivity model){
		int count = processActivityDAO.insert(model);

		int affectCount = 0;

		if(count>0) {
			//抄送用户
			String[] ccPersonIdArray = model.getCcPersonIdArray();
			if(ccPersonIdArray!=null) {
				for (String id : ccPersonIdArray) {
					ProcessActUser ccPerson = new ProcessActUser();
					ccPerson.setId(UUID.randomUUID().toString());
					ccPerson.setActivityId(model.getId());
					ccPerson.setPersonId(id);
					ccPerson.setUserType("2");
					ccPerson.setSource("1");//先固定抄送为人员
					ccPerson.setCreateBy(model.getCreateBy());
					ccPerson.setCreateTime(model.getCreateTime());
					ccPerson.setDelFlag(false);
					processActUserDAO.insert(ccPerson);
				}
			}

			String[] idArray;

			if(model.getSource().equals("3")){
				//相对职位
				String relativePosition = model.getRelativePosition();
				if(StringUtils.isNotEmpty(relativePosition)) {
					ProcessActUser person = new ProcessActUser();
					person.setId(UUID.randomUUID().toString());
					person.setActivityId(model.getId());
					person.setRelativePosition(relativePosition);
					person.setUserType("1");
					person.setSource("3");
					person.setCreateBy(model.getCreateBy());
					person.setCreateTime(model.getCreateTime());
					person.setDelFlag(false);
					affectCount = processActUserDAO.insert(person);
				}
			}
			else if(model.getSource().equals("2")){
				//部门角色
				String companyId = model.getCompanyId();
				String roleId = model.getRoleId();
				if(StringUtils.isNotEmpty(companyId)) {
					ProcessActUser person = new ProcessActUser();
					person.setId(UUID.randomUUID().toString());
					person.setActivityId(model.getId());
					person.setDepartmentId(companyId);
					person.setPersonRoleId(roleId);
					person.setUserType("1");
					person.setSource("2");
					person.setCreateBy(model.getCreateBy());
					person.setCreateTime(model.getCreateTime());
					person.setDelFlag(false);
					affectCount += processActUserDAO.insert(person);
				}
			}
			else{
				//指定审批人
				idArray = model.getApprovedPersonIdArray();
				if(idArray!=null) {
					for (String id : idArray) {
						ProcessActUser person = new ProcessActUser();
						person.setId(UUID.randomUUID().toString());
						person.setActivityId(model.getId());
						person.setPersonId(id);
						person.setUserType("1");
						person.setSource("1");
						person.setCreateBy(model.getCreateBy());
						person.setCreateTime(model.getCreateTime());
						person.setDelFlag(false);
						affectCount += processActUserDAO.insert(person);
					}
				}
			}
			ProcessTemplate processTemplate = processTemplateDAO.get(model.getProcessTemplateId());
			processTemplate.setUpdateBy(model.getUpdateBy());
			processTemplate.setUpdateTime(model.getUpdateTime());
			int OrderNum = processTemplate.getOrderNum();
			processTemplate.setOrderNum(OrderNum + 1);
			processTemplateDAO.update(processTemplate);
		}

		return affectCount;
	}

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


	@Override
	public int updateAndActUser(ProcessActivity model){
		int count = processActivityDAO.update(model);

		int affectCount = 0;

		if(count>0) {
			//抄送用户
			List<ProcessActUser> list = processActUserDAO.findItemByActIdAndUserType(model.getId(),"2");
			for (ProcessActUser processActUser:list) {
				processActUser.setUpdateBy(model.getUpdateBy());
				processActUser.setUpdateTime(model.getUpdateTime());
				processActUser.setDelFlag(true);
				processActUserDAO.update(processActUser);
			}

			String[] ccPersonIdArray = model.getCcPersonIdArray();
			if(ccPersonIdArray!=null) {
				for (String id : ccPersonIdArray) {
					ProcessActUser ccPerson = new ProcessActUser();
					ccPerson.setId(UUID.randomUUID().toString());
					ccPerson.setActivityId(model.getId());
					ccPerson.setPersonId(id);
					ccPerson.setUserType("2");
					ccPerson.setSource("1");//先固定抄送为人员
					ccPerson.setCreateBy(model.getCreateBy());
					ccPerson.setCreateTime(model.getCreateTime());
					ccPerson.setDelFlag(false);
					processActUserDAO.insert(ccPerson);
				}
			}

			String[] idArray;

			if(model.getSource().equals("3")){
				//相对职位
				List<ProcessActUser> list1 = processActUserDAO.findItemByActIdAndUserType(model.getId(),"1");
				for (ProcessActUser processActUser:list1) {
					processActUser.setUpdateBy(model.getUpdateBy());
					processActUser.setUpdateTime(model.getUpdateTime());
					processActUser.setDelFlag(true);
					processActUserDAO.update(processActUser);
				}

				String relativePosition = model.getRelativePosition();
				if(StringUtils.isNotEmpty(relativePosition)) {
					ProcessActUser person = new ProcessActUser();
					person.setId(UUID.randomUUID().toString());
					person.setActivityId(model.getId());
					person.setRelativePosition(relativePosition);
					person.setUserType("1");
					person.setSource("3");
					person.setCreateBy(model.getCreateBy());
					person.setCreateTime(model.getCreateTime());
					person.setDelFlag(false);
					affectCount = processActUserDAO.insert(person);
				}
			}
			else if(model.getSource().equals("2")){
				//部门角色
				List<ProcessActUser> list1 = processActUserDAO.findItemByActIdAndUserType(model.getId(),"1");
				for (ProcessActUser processActUser:list1) {
					processActUser.setUpdateBy(model.getUpdateBy());
					processActUser.setUpdateTime(model.getUpdateTime());
					processActUser.setDelFlag(true);
					processActUserDAO.update(processActUser);
				}

				String companyId = model.getCompanyId();
				String roleId = model.getRoleId();
				if(StringUtils.isNotEmpty(companyId)) {
					ProcessActUser person = new ProcessActUser();
					person.setId(UUID.randomUUID().toString());
					person.setActivityId(model.getId());
					person.setDepartmentId(companyId);
					person.setPersonRoleId(roleId);
					person.setUserType("1");
					person.setSource("2");
					person.setCreateBy(model.getCreateBy());
					person.setCreateTime(model.getCreateTime());
					person.setDelFlag(false);
					affectCount += processActUserDAO.insert(person);
				}

			}
			else{
				//指定审批人
				List<ProcessActUser> list1 = processActUserDAO.findItemByActIdAndUserType(model.getId(),"1");
				for (ProcessActUser processActUser:list1) {
					processActUser.setUpdateBy(model.getUpdateBy());
					processActUser.setUpdateTime(model.getUpdateTime());
					processActUser.setDelFlag(true);
					processActUserDAO.update(processActUser);
				}

				idArray = model.getApprovedPersonIdArray();
				if(idArray!=null) {
					for (String id : idArray) {
						ProcessActUser person = new ProcessActUser();
						person.setId(UUID.randomUUID().toString());
						person.setActivityId(model.getId());
						person.setPersonId(id);
						person.setUserType("1");
						person.setSource("1");
						person.setCreateBy(model.getCreateBy());
						person.setCreateTime(model.getCreateTime());
						person.setDelFlag(false);
						affectCount += processActUserDAO.insert(person);
					}
				}
			}
		}
		return affectCount;
	}


	@Override
	public int updateMove(ProcessActivity model,ProcessActivity swapProcessActivity){
		int count = processActivityDAO.update(model);
		int affectCount=0;
		if(count>0) {
			affectCount = processActivityDAO.update(swapProcessActivity);
		}
		return affectCount;
	}

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

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

		return count > 0 ? true : false;
	}

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


	@Override
	public  List<ProcessActivity> findByTemplate(String templateId){
		return processActivityDAO.findByTemplate(templateId);
	}

	@Override
	public  List<ProcessActivity> findByTemplateId(String templateId,String personRoleId){
		return processActivityDAO.findByTemplateId(templateId,personRoleId);
	}

	@Override
	public ProcessActivity findByActivityOrder(int activityOrder){
		return processActivityDAO.findByActivityOrder(activityOrder);
	}

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

        return page;
	}

	@Override
	public List<ProcessActivity> findByActivityOrderProcessTemplateId(String personRoleId,int activityOrder, String processTemplateId) {
		return processActivityDAO.findByActivityOrderProcessTemplateId(personRoleId,activityOrder,processTemplateId);
	}

	@Override
	public List<ProcessActivity> findByTemplateIdAndRoleId(String processTemplateId, String personRoleId) {
		return processActivityDAO.findByTemplateIdAndRoleId(processTemplateId,personRoleId);
	}

	@Override
	public List<ProcessActivity> findByTemplateIdAndRoleIdAndCondition(String processTemplateId, String personRoleId, String condition) {
		return processActivityDAO.findByTemplateIdAndRoleIdAndCondition(processTemplateId,personRoleId,condition);
	}
}