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

import java.util.Date;
import java.util.List;

import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workdailyroutine.dao.WorkDailyRoutineDao;
import com.jeeplus.modules.workdailyroutine.entity.WorkDailyRoutine;
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.workdailyroutine.entity.WorkDailyRoutineDetail;
import com.jeeplus.modules.workdailyroutine.dao.WorkDailyRoutineDetailDao;

/**
 * 日常事务Service
 * @author ssrh
 * @version 2018-07-11
 */
@Service
@Transactional(readOnly = true)
public class WorkDailyRoutineDetailService extends CrudService<WorkDailyRoutineDetailDao, WorkDailyRoutineDetail> {

    @Autowired
    private WorkDailyRoutineDao workDailyRoutineDao;
    @Autowired
    private WorkClientAttachmentDao workClientAttachmentDao;

	public WorkDailyRoutineDetail get(String id) {
        WorkDailyRoutineDetail workDailyRoutineDetail = super.get(id);
        if(workDailyRoutineDetail!=null && workDailyRoutineDetail.getWorkDailyRoutine()!=null && StringUtils.isNotBlank(workDailyRoutineDetail.getWorkDailyRoutine().getId())){
            workDailyRoutineDetail.setWorkDailyRoutine(workDailyRoutineDao.get(workDailyRoutineDetail.getWorkDailyRoutine().getId()));
        }
        return workDailyRoutineDetail;
	}
	
	public List<WorkDailyRoutineDetail> findList(WorkDailyRoutineDetail workDailyRoutineDetail) {
		return super.findList(workDailyRoutineDetail);
	}
	
	public Page<WorkDailyRoutineDetail> findPage(Page<WorkDailyRoutineDetail> page, WorkDailyRoutineDetail workDailyRoutineDetail) {
		return super.findPage(page, workDailyRoutineDetail);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkDailyRoutineDetail workDailyRoutineDetail) {
	    if(workDailyRoutineDetail.getIsNewRecord()) {
            workDailyRoutineDetail.setId(IdGen.uuid());
            dao.insert(workDailyRoutineDetail);
        }else {
	        dao.update(workDailyRoutineDetail);
        }
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkDailyRoutineDetail workDailyRoutineDetail) {
		super.delete(workDailyRoutineDetail);
	}

    public Integer countUndone(WorkDailyRoutineDetail select) {
        return dao.countUndone(select);
    }

    public WorkDailyRoutineDetail getByDailyId(String notifyId) {
        WorkDailyRoutineDetail select = new WorkDailyRoutineDetail();
        select.setWorkDailyRoutine(new WorkDailyRoutine(notifyId));
        select.setOperator(UserUtils.getUser());
        List<WorkDailyRoutineDetail> list = this.findList(select);
        if (list==null||list.size()<=0)return null;
        WorkDailyRoutineDetail workDailyRoutineDetail = list.get(0);
        if(workDailyRoutineDetail!=null && workDailyRoutineDetail.getWorkDailyRoutine()!=null && StringUtils.isNotBlank(workDailyRoutineDetail.getWorkDailyRoutine().getId())){
            WorkDailyRoutine workDailyRoutine = workDailyRoutineDao.get(workDailyRoutineDetail.getWorkDailyRoutine().getId());
            workDailyRoutineDetail.setWorkDailyRoutine(workDailyRoutine);
            if(workDailyRoutine!=null&&StringUtils.isNotBlank(workDailyRoutine.getId())){
                WorkClientAttachment attachment = new WorkClientAttachment();
                attachment.setAttachmentId(workDailyRoutine.getId());
                workDailyRoutine.setWorkAttachments(workClientAttachmentDao.findList(attachment));
            }
            if(workDailyRoutineDetail.getUpdateDate()==null){
                workDailyRoutineDetail.setUpdateDate(new Date());
            }
        }
        return workDailyRoutineDetail;
    }
}