package cn.com.countrygarden.bi.main.configurer;

import cn.com.countrygarden.bi.main.service.performance.UserTargetService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务：
 * 将ERP中用户是否删除，同步到biapp的UserTargetSchedule【选择性更新】
 *
 * @author Max Date:2018/09/26
 * @version 1.0
 * @since 1.8
 */
@Configuration
@ComponentScan("cn.com.countrygarden.bi.main.*")
@EnableScheduling
public class ErpToUserTargetSchedule {

    private final Logger log = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private UserTargetService userTargetService;

    @Autowired
    private TbSysUserService tbSysUserService;


    @Scheduled(cron = "0 0/20 * * * ?")
    public void syncErpToUserTarget() {
        log.info("=====start erp to userTarget table");
        Map<String,Integer> map=new HashMap<>();
        map.put("pageCurrent",-1);
        //获取userTarget表中的所有userCode
        List<String> userTargetList = userTargetService.selectAllUserCode(map);
        //遍历数据，如果从erp获取不到相关数据，则默认删除
        for (String userCode : userTargetList) {
            try {
                tbSysUserService.findByUserCode(userCode);
            } catch (Exception e) {
                log.error("erp to userTarget error:userCode=" + userCode);
                //执行更新操作，将该userCode的所有记录，进行逻辑删除
                userTargetService.updateIsDelete(userCode);
            }
        }
        log.info("=====end erp to userTarget table");
    }
}
