package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.MzDepositFile;
import cn.hnthyy.thmz.entity.his.mz.MzyReqrec;
import cn.hnthyy.thmz.entity.his.zy.ZyDepositFile;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.entity.zz.WxPayOrder;
import cn.hnthyy.thmz.service.his.mz.MzDepositFileService;
import cn.hnthyy.thmz.service.his.mz.MzyReqrecService;
import cn.hnthyy.thmz.service.his.zy.ZyDepositFileService;
import cn.hnthyy.thmz.service.thmz.PaidForZZService;
import cn.hnthyy.thmz.vo.PageViewVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
public class PaidForZZController {
    @Autowired
    private PaidForZZService paidForZZService;
    @Autowired
    private MzDepositFileService mzDepositFileService;
    @Autowired
    private MzyReqrecService mzyReqrecService;
    @Autowired
    private ZyDepositFileService zyDepositFileService;
    /**
     * 查询自助缴费列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listOrdersByConditions", method = {RequestMethod.POST})
    public Map<String, Object> listOrdersByConditions(@RequestBody WxPayOrder wxPayOrder) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (wxPayOrder == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询自助缴费列表参数不能为空");
                return resultMap;
            }

            if(StringUtils.isBlank(wxPayOrder.getBeginTime())){
                resultMap.put("code", -1);
                resultMap.put("message", "查询自助缴费列表开始时间参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(wxPayOrder.getEndTime())){
                resultMap.put("code", -1);
                resultMap.put("message", "查询自助缴费列表结束时间参数不能为空");
                return resultMap;
            }
            if(wxPayOrder.getOrderType()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "查询自助缴费订单类型参数不能为空");
                return resultMap;
            }
            if(wxPayOrder.getPayStatus()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "查询自助缴费列表状态参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(wxPayOrder.getPatientName())){
                wxPayOrder.setPatientName(null);
            }
            if(StringUtils.isBlank(wxPayOrder.getPatientId())){
                wxPayOrder.setPatientId(null);
            }
            PageViewVo pageViewVo= paidForZZService.listWxPayOrder(wxPayOrder);
            resultMap.put("code", 0);
            resultMap.put("message", "查询自助缴费列表成功");
            resultMap.put("pageViewVo", pageViewVo);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询自助缴费列表失败，系统出错，请联系管理员");
            log.error("查询自助缴费列表失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }



    /**
     * 自助退费
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/refundOrder", method = {RequestMethod.POST})
    public Map<String, Object> refundOrder(@RequestBody WxPayOrder wxPayOrder, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (wxPayOrder == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "自助退费参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(wxPayOrder.getTradeNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "交易订单号不能为空");
                return resultMap;
            }
            if (wxPayOrder.getId() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "订单ID不能为空");
                return resultMap;
            }
            if (wxPayOrder.getTotalFee() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "退费金额不能为空");
                return resultMap;
            }
            if(wxPayOrder.getOrderType()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "订单类型参数不能为空");
                return resultMap;
            }
            if(wxPayOrder.getOrderType()==1){
                MzyReqrec mzyReqrec= mzyReqrecService.queryMzyReqrecByAgtordnum(wxPayOrder.getTradeNo(),null,null);
                if(mzyReqrec!=null){
                    resultMap.put("code", -1);
                    resultMap.put("message", "当前缴费已经成功挂号，无法退费，如需退费，请到挂号列表进行对应操作！");
                    return resultMap;
                }
                mzyReqrec= mzyReqrecService.queryMzyReqrecByAgtordnum(wxPayOrder.getTradeNo(),null,"mzy_reqrec_b");
                if(mzyReqrec!=null){
                    resultMap.put("code", -1);
                    resultMap.put("message", "当前缴费已经成功挂号，无法退费，如需退费，请到挂号列表进行对应操作！");
                    return resultMap;
                }
            }
            if(wxPayOrder.getOrderType()==2){
                MzDepositFile mzDepositFile= mzDepositFileService.queryMzDepositFileByAgtordnum(wxPayOrder.getTradeNo(),null,null);
                if(mzDepositFile!=null){
                    resultMap.put("code", -1);
                    resultMap.put("message", "当前缴费已经成功缴纳门诊费用，无法退费，如需退费，请到收费列表进行对应操作！");
                    return resultMap;
                }
                mzDepositFile= mzDepositFileService.queryMzDepositFileByAgtordnum(wxPayOrder.getTradeNo(),null,"mz_deposit_file_b");
                if(mzDepositFile!=null){
                    resultMap.put("code", -1);
                    resultMap.put("message", "当前缴费已经成功缴纳门诊费用，无法退费，如需退费，请到收费列表进行对应操作！");
                    return resultMap;
                }
            }
            if(wxPayOrder.getOrderType()==3){
                ZyDepositFile zyDepositFile= zyDepositFileService.queryZyDepositFileByAgtordnum(wxPayOrder.getTradeNo(),null,null);
                if(zyDepositFile!=null){
                    resultMap.put("code", -1);
                    resultMap.put("message", "当前缴费已经成功缴纳住院预缴金，无法退费，如需退费，请到住院鱼胶金列表进行对应操作！");
                    return resultMap;
                }
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            wxPayOrder.setRefundOpCode(tokenUser.getUserIdCode());
            Map<String,Object> map= paidForZZService.refundOrder(wxPayOrder);
            if(map !=null && (Integer)map.get("code")==200){
                resultMap.put("code", 0);
                resultMap.put("message", "退费成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "退费失败");
            return resultMap;
        }catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "退费失败，系统出错，请联系管理员");
            log.error("退费失败");
            return resultMap;
        }
    }

}
