package com.jpsoft.shinestar;

import com.jpsoft.shinestar.modules.base.dao.WorkPersonSchedulingDAO;
import com.jpsoft.shinestar.modules.base.entity.WorkPersonScheduling;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WorkPersonSchedulingTest {
    @Autowired
    private WorkPersonSchedulingDAO workPersonSchedulingDAO;

    @Transactional
    private void innerUpdate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date startDate = sdf.parse("2021-09-01");
            Date endDate = sdf.parse("2021-09-30");

            List<Map> mapList = workPersonSchedulingDAO.findRedundantSchedule(startDate,endDate);

            for (Map map : mapList) {
                Long personId = Long.valueOf(map.get("personId").toString());
                Date schedulingDay = (Date)map.get("schedulingDay");

                List<WorkPersonScheduling> list = workPersonSchedulingDAO.findByPersonIdSchedulingDay(personId,schedulingDay);

                if (list.size()>1){
                    WorkPersonScheduling wps = list.get(list.size()-1);
                    wps.setDelFlag(true);
                    wps.setUpdateTime(new Date());

                    workPersonSchedulingDAO.update(wps);
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testUpdate(){
        innerUpdate();
    }
}
