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

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

import com.jpsoft.shinestar.modules.base.dao.WorkShiftCompanyDAO;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.WorkShiftCompany;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.WorkShiftInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.WorkShiftInfo;
import com.jpsoft.shinestar.modules.base.service.WorkShiftInfoService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="workShiftInfoService")
public class WorkShiftInfoServiceImpl implements WorkShiftInfoService {
	@Resource(name="workShiftInfoDAO")
	private WorkShiftInfoDAO workShiftInfoDAO;

	@Resource(name="workShiftCompanyDAO")
	private WorkShiftCompanyDAO workShiftCompanyDAO;

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

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

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

	@Override
	public int updateAndInsert(WorkShiftInfo workShiftInfo,List<CompanyInfo> list){
		List<WorkShiftCompany> workShiftCompanyList = workShiftCompanyDAO.findByShiftId(workShiftInfo.getId());

		int count = 0;

		//先删除现有的
		for (WorkShiftCompany workShiftCompany:workShiftCompanyList) {
			workShiftCompany.setDelFlag(true);
			workShiftCompany.setUpdateBy(workShiftInfo.getUpdateBy());
			workShiftCompany.setUpdateTime(workShiftInfo.getUpdateTime());
			workShiftCompanyDAO.update(workShiftCompany);
		}

		//然后添加
		for (CompanyInfo companyInfo:list) {
			WorkShiftCompany workShiftCompany = new WorkShiftCompany();
			workShiftCompany.setId(UUID.randomUUID().toString());
			workShiftCompany.setCompanyId(companyInfo.getId());
			workShiftCompany.setShiftId(workShiftInfo.getId());
			workShiftCompany.setCreateBy(workShiftInfo.getUpdateBy());
			workShiftCompany.setCreateTime(workShiftInfo.getUpdateTime());
			workShiftCompany.setDelFlag(false);

			count += workShiftCompanyDAO.insert(workShiftCompany);
		}
		return count;
	}

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

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = workShiftInfoDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<WorkShiftInfo> list() {
		// TODO Auto-generated method stub
		return workShiftInfoDAO.list();
	}
		
	@Override
	public Page<WorkShiftInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
        Page<WorkShiftInfo> page = PageHelper.startPage(pageNumber,pageSize).doSelectPage(()->{
            workShiftInfoDAO.search(searchParams,sortList);
        });
        
        return page;
	}

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

	@Override
	public List<WorkShiftInfo> findByCompanyIds(List<String> companyIds){
		return workShiftInfoDAO.findByCompanyIds(companyIds);
	}

	@Override
	public String findNumberById(String id){
		return workShiftInfoDAO.findNumberById(id);
	}

	@Override
	public List<String> sortIdsByNumber(List<String> shiftIds){
		return workShiftInfoDAO.sortIdsByNumber(shiftIds);
	}

	@Override
	public WorkShiftInfo findByNumber(String number){
		return workShiftInfoDAO.findByNumber(number);
	}

}