package com.mortals.xhx.daemon.task;

import cn.hutool.core.collection.ListUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SiteMatterPdu;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.feign.site.ISiteMatterFeign;
import com.mortals.xhx.module.sheet.model.SheetMatterEntity;
import com.mortals.xhx.module.sheet.model.SheetMatterQuery;
import com.mortals.xhx.module.sheet.service.SheetMatterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 同步事项列表
 */
@Slf4j
@Service("SyncSiteMatterTask")
public class SyncSiteMatterTaskImpl implements ITaskExcuteService {

    @Autowired
    private ISiteFeign siteFeign;
    @Autowired
    private ISiteMatterFeign siteMatterFeign;
    @Autowired
    private SheetMatterService sheetMatterService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("开始同步事项列表！");
        SitePdu sitePdu = new SitePdu();
        sitePdu.setId(1L);
        Rest<List<SitePdu>> siteRest = siteFeign.getFlatSitesBySiteId(sitePdu);
        if (siteRest.getCode() == YesNoEnum.YES.getValue()) {
            log.info("站点总数量：{}",siteRest.getData().size());
            siteRest.getData().forEach(site -> {
                sheetMatterService.getDao().delete(new SheetMatterQuery().siteId(site.getId()));
                log.info("删除事项：{}",siteRest.getData().size());
                int page=100;
               int pageNum=100;
               for(int i=1;i<=page;i++){
                   SiteMatterPdu siteMatterPdu = new SiteMatterPdu();
                   siteMatterPdu.setSiteId(site.getId());
                   siteMatterPdu.setPage(i);
                   siteMatterPdu.setSize(pageNum);
                   log.info("请求页数列表:"+i);
                   Rest<RespData<List<SiteMatterPdu>>> siteMatterRest = siteMatterFeign.list(siteMatterPdu);
                   if (siteMatterRest.getCode() == YesNoEnum.YES.getValue()) {
                       if(siteMatterRest.getData().getData().size()==0){
                           log.info("数据没有！跳出循环");
                           break;
                       }
                       //删除后新增
                       log.info("返回事项总数量：{}",siteMatterRest.getData().getData().size());
                       List<SheetMatterEntity> sheetMatterEntities = siteMatterRest.getData().getData().stream().map(siteMatter -> {
                           SheetMatterEntity sheetMatterEntity = new SheetMatterEntity();
                           sheetMatterEntity.initAttrValue();
                           sheetMatterEntity.setId(siteMatter.getMatterId());
                           sheetMatterEntity.setSiteId(siteMatter.getSiteId());
                           sheetMatterEntity.setMatterName(siteMatter.getMatterName());
                           sheetMatterEntity.setMatterNo(siteMatter.getMatterCode());
                           sheetMatterEntity.setDeptCode(siteMatter.getDeptCode());
                           sheetMatterEntity.setDeptName(siteMatter.getDeptName());
                           sheetMatterEntity.setAreaCode(siteMatter.getAreaCode());
                           sheetMatterEntity.setSource(siteMatter.getSource());
                           return sheetMatterEntity;
                       }).collect(Collectors.toList());

                       if (!ObjectUtils.isEmpty(sheetMatterEntities)) {
                          // sheetMatterService.getDao().delete(new SheetMatterQuery().siteId(site.getId()));
                           log.info("新增事项数量：{}",sheetMatterEntities.size());
                           sheetMatterService.save(sheetMatterEntities);
                       }
                   }else{
                       log.info("请求错误，code:{} msg:{}",siteMatterRest.getCode(),siteMatterRest.getMsg());
                   }
               }

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

    }
}
