package com.dxhy.core.task;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.dao.ConfirmDao;
import com.dxhy.core.dao.GsClientMapper;
import com.dxhy.core.entity.GsClient;
import com.dxhy.core.job.entity.SNSAPObject;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.entity.TAcOrg;
import com.dxhy.core.job.service.ApplyService;
import com.dxhy.core.job.service.ScheduleJobService;
import com.dxhy.core.job.service.SysDeptService;
import com.dxhy.core.util.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 申请统计，统计结果，申请确认，确认结果定时任务
 *
 * @author fth
 * @date 2019年05月28日
 **/
@Component("pullSapData")
@Slf4j

public class PullSapDataTask {
    @Resource
    private ApplyService applyService;
    @Resource
    ScheduleJobService scheduleJobService;
    @Resource
    SysDeptService sysDeptService;
    @Resource
    ConfirmDao confirmDao;
    @Value("${sdny.snYxUrl}")
    private String snYxUrl;
    @Value("${po.userName}")
    private String userName;
    @Value("${sdny.record.environment}")
    private String environment;

    @Value("${po.password}")
    private String password;
    @Resource
    GsClientMapper gsClientMapper;

    public void applyStat() {
        log.info("拉取sap数据获取");
        //获取当前年月
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        String format1 = format.format(date);
        String year = format1.substring(0, 4);
        String month = format1.substring(4, 6);

        //获取公司代码
        List<TAcOrg> taxNos=confirmDao.getPullSapDataTaxNo();
        for(TAcOrg tAcOrg:taxNos){
            SNSAPObject object = new SNSAPObject();
            object.setSYSID("FPXT");
            object.setIFYWID("FI842");
            object.setBSKEY(UUID.randomUUID().toString().replace("-", ""));
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            List<GsClient> gsClients = gsClientMapper.selectByGsdm(tAcOrg.getOrgcode());
            if(gsClients!=null && gsClients.size()>0){
                object.setZFILED5(gsClients.get(0).getClient());
            }else {
                object.setZFILED5(environment);
            }
            Map<String,String> requestMap = new HashMap<>();
            //公司代码
            requestMap.put("ZGSDM",tAcOrg.getOrgcode());
            //年
            requestMap.put("GJAHR",year);
            //月
            requestMap.put("MONAT",month);
            requestMap.put("ZDATEF","");
            requestMap.put("ZDATET","");
            object.setZDATA(JSONObject.toJSONString(requestMap));
            JSONObject request = new JSONObject();
            request.put("IS_INPUT",object);
            String req = request.toJSONString();
            log.info("税号:{},发送获取sap数据{}",tAcOrg.getTaxno(),req);
            String s = HttpRequestUtils.sendPo(snYxUrl, req, userName, password);
            log.info("税号:{},拉去sap接口返回数据{}",tAcOrg.getTaxno(),s);
        }

    }


}
