package com.mortals.xhx.daemon.task;

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.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.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()) {
            siteRest.getData().forEach(site -> {
                SiteMatterPdu siteMatterPdu = new SiteMatterPdu();
                siteMatterPdu.setPage(1);
                siteMatterPdu.setSize(-1);
                Rest<RespData<List<SiteMatterPdu>>> siteMatterRest = siteMatterFeign.list(siteMatterPdu);
                if (siteMatterRest.getCode() == YesNoEnum.YES.getValue()) {
                    //删除后新增
                    List<SheetMatterEntity> sheetMatterEntities = siteMatterRest.getData().getData().stream().map(siteMatter -> {
                        SheetMatterEntity sheetMatterEntity = new SheetMatterEntity();
                        sheetMatterEntity.initAttrValue();
                        sheetMatterEntity.setSiteId(siteMatter.getSiteId());
                        sheetMatterEntity.setMatterName(siteMatter.getMatterName());
                        sheetMatterEntity.setMatterNo(siteMatter.getMatterCode());
                        sheetMatterEntity.setDeptCode(siteMatter.getDeptCode());
                        sheetMatterEntity.setDeptName(siteMatter.getDeptName());
                        sheetMatterEntity.setAreaCode(siteMatter.getAreaCode());
                        return sheetMatterEntity;
                    }).collect(Collectors.toList());

                    if (!ObjectUtils.isEmpty(sheetMatterEntities)) {
                        sheetMatterService.save(sheetMatterEntities);
                    }
                }
            });
        }
    }
    @Override
    public void stopTask(ITask task) throws AppException {

    }
}
