package com.mortals.xhx.daemon.task;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.module.bussinesscard.model.BussinesscardEntity;
import com.mortals.xhx.module.bussinesscard.service.BussinesscardService;
import com.mortals.xhx.module.staff.dao.StaffDao;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工名片日访问数初始化
 */
@Slf4j
@Service("StaffViewsByDayInit")
public class StaffViewsByDayInitTaskImpl implements ITaskExcuteService {
    @Autowired
    private StaffService staffService;
    @Autowired
    private BussinesscardService bussinesscardService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        List<StaffEntity> staffEntityList = staffService.getAllList();
        List<StaffEntity> updateList = new ArrayList<>();
        for (StaffEntity entity:staffEntityList){
            StaffEntity update = new StaffEntity();
            update.setId(entity.getId());
            update.setViewsByDay(0);
            updateList.add(update);
        }
        if(updateList.size()>0){
            staffService.update(updateList);
        }
        List<BussinesscardEntity> bussinesscardList = bussinesscardService.getAllList();
        List<BussinesscardEntity> updateBussinesscard = new ArrayList<>();
        for (BussinesscardEntity entity:bussinesscardList){
            BussinesscardEntity update = new BussinesscardEntity();
            update.setId(entity.getId());
            update.setViewsByDay(0);
            updateBussinesscard.add(update);
        }
        if(updateBussinesscard.size()>0){
            bussinesscardService.update(updateBussinesscard);
        }
    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }
}
