package org.linlinjava.jz.hm;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.linlinjava.jz.db.domain.HmEmployClock;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.util.hm.HmNoticeEnum;
import org.linlinjava.jz.dto.HmNoticeDto;
import org.linlinjava.jz.hm.service.HmEmployRelationService;
import org.linlinjava.jz.service.HmEmployClockService;
import org.linlinjava.jz.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

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

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class HmNoticeTaskTest {
    @Autowired
    private HmEmployRelationService hmEmployRelationService;
    @Autowired
    private HmEmployClockService hmEmployClockService;
    @Resource
    private NoticeService noticeService;

    @Test
    public void run() {

        //昨日
        LocalDateTime localDateTime = LocalDate.now().plusDays(-1).atStartOfDay();
        List<HmEmployClock> hmEmployClocks = hmEmployClockService.queryAbsentOfWork(localDateTime);

        hmEmployClocks.forEach(item -> {

            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());

            noticeService.create(hmNoticeDto);
        });
    }
}
