package com.saye.hospitalgd.controller.historyLog;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.scheduler.MySchedulerFactory;
import com.saye.hospitalgd.scheduler.job.BankGetData;
import com.saye.hospitalgd.service.BankbillGetinfoService;
import com.saye.hospitalgd.service.ThirdFtpConfigService;
import com.saye.hospitalgd.service.ThirdSftpConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 三方账单查询信息
 * @date 2021/8/31 9:04
 */
@Controller
@RequestMapping("/thirdBillLog")
@Api(tags = "三方账单查询信息")
public class ThirdBillLogController {

    @Autowired
    private BankbillGetinfoService bankbillGetinfoService;

    @Autowired
    private MySchedulerFactory mySchedulerFactory;

    @Autowired
    private ThirdFtpConfigService thirdFtpConfigService;



    @Autowired
    private ThirdSftpConfigService thirdSftpConfigService;

    @RequestMapping("/toThirdBillLog")
    public String toThirdBillLog(ModelMap modelMap) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.DATE, -1);
        Date startDate = calendar.getTime();

        String startTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, startDate);
        String endTime = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd);

        modelMap.addAttribute("startTime", startTime);
        modelMap.addAttribute("endTime", endTime);

        return "historyLog/thirdBillLog";
    }

    /**
     * @description: 查询三方调用的日志
     * @author thuang
     * @date 2021/8/30 14:30
     * @version 1.0
     */
    @RequestMapping("/findThirdBillLogPageList")
    @ResponseBody
    @ApiOperation(value = "查询三方调用的日志")
    public HashMap<Object, Object> findThirdBillLogPageList(String business, String status, String startTime, String endTime, int page, int limit) {
        HashMap<Object, Object> responseMap = new HashMap<Object, Object>();

        try {

            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("business", business);
            map.put("is_ok", status);
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<HashMap<Object, Object>>(bankbillGetinfoService.findBankbillGetinfoList(map));

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("count", pageInfo.getTotal());
            responseMap.put("data", pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }

        return responseMap;
    }

    /**
     * @description: 重新执行定时任务
     * @author thuang
     * @date 2021/9/13 13:08
     * @version 1.0
     */
    @RequestMapping("/restartGetBill")
    @ResponseBody
    @ApiOperation(value = "重新执行定时任务")
    public HashMap<Object, Object> restartGetBill(@RequestBody HashMap<Object, Object> map) {
        HashMap<Object, Object> responseMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        try {
            //交易时间
            String trade_date = StringDUtil.changeNullToEmpty(map.get("trade_date"));
            String bill_table_name = StringDUtil.changeNullToEmpty(map.get("bill_table_name"));

            //根据交易时间获取交易生成的定时任务
            HashMap<Object, Object> searchMap = new HashMap<>();
            searchMap.put("trade_date", trade_date);
            searchMap.put("bill_table_name", bill_table_name);
            HashMap<Object, Object> bankbillGetinfo = bankbillGetinfoService.findBankbillGetinfoById(searchMap);
            String quartz_id = StringDUtil.changeNullToEmpty(bankbillGetinfo.get("QUARTZ_ID"));
            String quartz_name = StringDUtil.changeNullToEmpty(bankbillGetinfo.get("QUARTZ_NAME"));
            String thirdConfigId = StringDUtil.changeNullToEmpty(bankbillGetinfo.get("THIRDCONFIGID"));

            //查询配置
            HashMap<Object,Object> searchConfigMap=new HashMap<>();
            searchConfigMap.put("thirdConfigId",thirdConfigId);

            HashMap<Object, Object> resultMap = new HashMap<>();

            if (thirdConfigId.equals("999")){
                //这里暂时将建行龙支付的情况写死在这里,后续优化
                searchConfigMap.put("FUBS","1");
                List<HashMap<Object, Object>> wlifList = thirdFtpConfigService.findWLIF(searchConfigMap);
                resultMap = BankGetData.oneExecute(quartz_id, quartz_name, trade_date,wlifList.get(0));
            }else {
                List<HashMap<Object,Object>> thirdFtpConfigList =thirdFtpConfigService.findThirdFtpConfigList(searchConfigMap);
                resultMap = BankGetData.oneExecute(quartz_id, quartz_name, trade_date,thirdFtpConfigList.get(0));
            }

//
//            HashMap<Object, Object> searchConfigMap = new HashMap<>();
//            searchConfigMap.put("thirdConfigId", thirdConfigId);
//            List<HashMap<Object, Object>> thirdFtpConfigList = thirdFtpConfigService.findThirdFtpConfigList(searchConfigMap);

            errCode = StringDUtil.changeNullToEmpty(resultMap.get("errCode"));
            errMsg = StringDUtil.changeNullToEmpty(resultMap.get("errMsg"));
        } catch (Exception e) {
            e.printStackTrace();

            errCode = "999";
            errMsg = "重新执行定时任务失败，原因：" + e.getMessage();
        }
        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);

        return responseMap;
    }
}
