package org.linlinjava.jz.hm.job;

import com.google.common.collect.Lists;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.jz.db.domain.HmEmployClock;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.util.hm.HmNoticeEnum;
import org.linlinjava.jz.dto.HmNoticeDto;
import org.linlinjava.jz.dto.HmNoticeDto.UserNoticeDto;
import org.linlinjava.jz.hm.util.DateUtil;
import org.linlinjava.jz.service.HmEmployClockService;
import org.linlinjava.jz.service.NoticeService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fuxubing
 */
@Component
public class HmNoticeJob {
    private final Log logger = LogFactory.getLog(HmNoticeJob.class);
    @Resource
    private HmEmployClockService hmEmployClockService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private HmEmployRelationService hmEmployRelationService;

    @Scheduled(cron = "0 0 2 * * ?")
    public void sendNotice() {
        //昨日
        LocalDateTime localDateTime = LocalDate.now().plusDays(-1).atStartOfDay();
        List<HmEmployClock> hmEmployClocks = hmEmployClockService.queryAbsentOfWork(localDateTime);

        hmEmployClocks.forEach(item -> {
            logger.info("缺勤发送通知");

            HmEmployRelation hmEmployRelation = hmEmployRelationService.queryById(item.getEmployRelationId());

            HmNoticeDto hmNoticeDto = new HmNoticeDto();
            hmNoticeDto.setAppUserId(Lists.newArrayList(hmEmployRelation.getUserButlerId(), hmEmployRelation.getUserEmployerId(), hmEmployRelation.getUserServantId()));
           // hmNoticeDto.setContent("保姆缺勤");
            hmNoticeDto.setEmployRelationId(hmEmployRelation.getId());
            hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_6.getType());
            hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_6.getNoticeUserType());
            hmNoticeDto.setrId(String.valueOf(item.getId()));
            hmNoticeDto.setCreateUserId(hmEmployRelation.getUserServantId());

            
            String time=DateUtil.DateFormatToyyyyMMdd(localDateTime);
            UserNoticeDto ud=new UserNoticeDto();
            ud.setContent(HmNoticeEnum.getNoClockServant(time));
            ud.setUserId(hmEmployRelation.getUserServantId());
            
            UserNoticeDto ud2=new UserNoticeDto();
            ud2.setContent(HmNoticeEnum.getNoClockCustomer(time));
            ud2.setUserId(hmEmployRelation.getUserEmployerId());
            
            List<UserNoticeDto> userNoticeList=new ArrayList<UserNoticeDto>();
            userNoticeList.add(ud);
            userNoticeList.add(ud2);
			hmNoticeDto.setUserNoticeList(userNoticeList);
            
            noticeService.create(hmNoticeDto);
        });
    }

}
