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.model.Dicinfo;
import com.saye.hospitalgd.service.RefundOrderService;
import com.saye.hospitalgd.service.system.DicinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： Mr.zs
 * @date： 2023/10/9
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Controller
@RequestMapping("/refundOrder")
public class RefundOrderController {

    @Autowired
    private DicinfoService dicinfoService;


    @Autowired
    RefundOrderService refundOrderService;

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

        //服务渠道
        List<Dicinfo> serCh = dicinfoService.findDicinfoTreeNodeList("SER_CH");
        modelMap.addAttribute("serChs", serCh);
        //支付来源
        List<Dicinfo> tra_type = dicinfoService.findDicinfoTreeNodeList("TRA_TYPE");
        modelMap.addAttribute("tra_types", tra_type);
        //聚合支付标识
        List<Dicinfo> payLog = dicinfoService.findDicinfoTreeNodeList("PAY_LOG");
        modelMap.addAttribute("payLogs", payLog);
        //支付方式
        List<Dicinfo> paySou = dicinfoService.findDicinfoTreeNodeList("PAY_SOU");
        modelMap.addAttribute("paySous", paySou);
        //退款状态
        List<Dicinfo> reState = dicinfoService.findDicinfoTreeNodeList("RE_STATE");
        modelMap.addAttribute("reStates", reState);

        return "financialReconciliation/RefundOrder";
    }

    @RequestMapping("/findRefundOrder")
    @ResponseBody
    public HashMap<Object, Object> findTradeRecords(String jyly, String startTime, String endTime, String likeFiled, int page, int limit) {

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

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("jyly", jyly);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("likeFiled", likeFiled);

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<HashMap<Object, Object>>(refundOrderService.findRefundOrder(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;
    }
}
