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.model.Users;
import com.saye.hospitalgd.scheduler.jobMethod.ReconciliationMethod;
import com.saye.hospitalgd.service.historyLog.ReconciliationLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
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 10:17
 */
@Controller
@RequestMapping("/reconciliationLog")
@Api(tags = "对账情况信息")
public class ReconciliationLogController {

    @Autowired
    private ReconciliationLogService reconciliationLogService;

    @RequestMapping("/toReconciliationLog")
    public String toReconciliationLog(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/reconciliationLog";
    }

    /**
     * @description: 查询对账的日志
     * @author thuang
     * @date 2021/8/30 14:30
     * @version 1.0
     */
    @RequestMapping("/findReconciliationLogPageList")
    @ResponseBody
    @ApiOperation(value = "查询对账的日志")
    public HashMap<Object, Object> findReconciliationLogPageList(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("status", status);
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<HashMap<Object, Object>>(reconciliationLogService.findReconciliationLogPageList(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/10/9 15:49
     * @version 1.0
     */
    @RequestMapping("/restartManager")
    @ResponseBody
    @ApiOperation(value = "重新开始对账")
    public HashMap<Object, Object> restartManager(@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"));

            //根据交易时间获取交易生成的定时任务
            HashMap<Object, Object> searchMap = new HashMap<>();
            searchMap.put("trade_date", trade_date);
            List<HashMap<Object, Object>> hisBillLogByParam = reconciliationLogService.findReconciliationLogByParam(searchMap);
            HashMap<Object, Object> hisBillLog = hisBillLogByParam.get(0);
            String quartz_id = StringDUtil.changeNullToEmpty(hisBillLog.get("QUARTZ_ID"));
            String quartz_name = StringDUtil.changeNullToEmpty(hisBillLog.get("QUARTZ_NAME"));

            Users user = (Users) SecurityUtils.getSubject().getPrincipal();
            String trueName = user.getTrueName();

            HashMap<Object, Object> resultMap = new ReconciliationMethod().getDate(quartz_id, quartz_name, trade_date, trueName);
            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;
    }

}
