package com.tofly.feesapi.wxgl.wechatpay.controller;

import cn.hutool.core.util.ObjUtil;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.wxgl.wechatpay.entity.PaySelect;
import com.tofly.feesapi.wxgl.wechatpay.entity.vo.*;
import com.tofly.feesapi.wxgl.wechatpay.service.IPaySelectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/wechatpay/payselect")
@Api(tags = "微信公众号-支付查询接口")
public class PaySelectController {

    @Autowired
    private IPaySelectService iPaySelectService;

    /**
     * 获取用户欠费信息
     */
    @GetMapping("/yhqfxx")
    @ApiOperation(value = "用户欠费信息合计查询")
    public ResultRespone<PaySelectQfVo> getYhqfxx(@ModelAttribute PaySelect dto) {
        ResultRespone<PaySelectQfVo> resultRespone = new ResultRespone<>();
        try {
            PaySelectQfVo yhqfxx = iPaySelectService.getYhqfxx(dto);
            if (ObjUtil.isNull(yhqfxx)) {
                resultRespone.setCode(2);
                resultRespone.setResult(null);
            } else {
                resultRespone.setCode(1);
                resultRespone.setResult(yhqfxx);
            }
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    /**
     * 获取用户订单状态
     */
    @GetMapping("/orderstatus")
    @ApiOperation(value = "获取用户订单状态")
    public ResultRespone<PaySelectOrderStatusVo> getOrderStatus(@ModelAttribute PaySelect dto) {
        ResultRespone<PaySelectOrderStatusVo> resultRespone = new ResultRespone<>();

        if (null == dto.getSflsbh()) {
            resultRespone.setCode(-1);
            resultRespone.setMessage("收费流水编号不能为空！");
            return resultRespone;
        }

        try {
            PaySelectOrderStatusVo orderStatus = iPaySelectService.getOrderStatus(dto);
            resultRespone.setCode(1);
            resultRespone.setResult(orderStatus);
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    /**
     * 获取日收费明细列表
     */
    @GetMapping("/getsfmxlist")
    @ApiOperation(value = "获取日收费明细列表", response = PaySelectSfxmVo.class, responseContainer = "List")
    public ResultRespone<List<PaySelectSfxmVo>> getSfmxList(@ModelAttribute PaySelect dto) {
        ResultRespone<List<PaySelectSfxmVo>> resultRespone = new ResultRespone<>();
        if (null == dto.getSfrybm()) {
            resultRespone.setCode(-1);
            resultRespone.setMessage("收费人员编码不能为空！");
            return resultRespone;
        }
        if (null == dto.getSfrq()) {
            resultRespone.setCode(-1);
            resultRespone.setMessage("收费日期不能为空！");
            return resultRespone;
        }
        try {
            List<PaySelectSfxmVo> sfmxList = iPaySelectService.getSfmxList(dto);
            resultRespone.setCode(1);
            resultRespone.setResult(sfmxList);
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    /**
     * 获取用户收费明细
     */
    @GetMapping("/getyhsfmxlist")
    @ApiOperation(value = "获取用户收费明细", response = PaySelectSfxmVo.class, responseContainer = "List")
    public ResultRespone<List<PaySelectSfxmVo>> getyhsfmxlist(@ModelAttribute PaySelect dto) {
        ResultRespone<List<PaySelectSfxmVo>> resultRespone = new ResultRespone<>();
        resultRespone.setCode(0);
        if (null == dto.getYhbh()) {
            resultRespone.setCode(-1);
            resultRespone.setMessage("用户编码不能为空！");
            return resultRespone;
        }
        try {
            List<PaySelectSfxmVo> sfmxList = iPaySelectService.getYhSfmxList(dto);
            if (sfmxList.size()>0)
            {
                resultRespone.setMessage("查询成功");
                resultRespone.setResult(sfmxList);
            }
            else {
                resultRespone.setCode(-1);
                resultRespone.setMessage("无数据");
            }

        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }


    /**
     * 获取用户抄表明细
     */
    @GetMapping("/getyhcbmxlist")
    @ApiOperation(value = "获取用户抄表明细", response = PaySelectSfxmVo.class, responseContainer = "List")
    public ResultRespone<List<QfSelectCbxmVo>> getyhcbmxlist(@ModelAttribute PaySelect dto) {
        ResultRespone<List<QfSelectCbxmVo>> resultRespone = new ResultRespone<>();
        resultRespone.setCode(0);
        if (null == dto.getYhbh()) {
            resultRespone.setCode(-1);
            resultRespone.setMessage("用户编码不能为空！");
            return resultRespone;
        }
        try {
            List<QfSelectCbxmVo> cbmxList = iPaySelectService.getYhCbmxList(dto);
            if (cbmxList.size()>0)
            {
                resultRespone.setMessage("查询成功");
                resultRespone.setResult(cbmxList);
            }
            else {
                resultRespone.setCode(-1);
                resultRespone.setMessage("无数据");
            }

        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    /**
     * 获取用户信息和汇总信息
     */
    @GetMapping("/getyhxxlist")
    @ApiOperation(value = "获取用户信息和汇总信息", response = PaySelectSfxmVo.class, responseContainer = "List")
    public ResultRespone<List<YhSelectxxVo>> getyhxxlist(@ModelAttribute PaySelect dto) {
        ResultRespone<List<YhSelectxxVo>> resultRespone = new ResultRespone<>();
        resultRespone.setCode(0);
        if (null == dto.getYhbh()) {
            resultRespone.setCode(-1);
            resultRespone.setMessage("用户编码不能为空！");
            return resultRespone;
        }
        try {
            List<YhSelectxxVo> yhxxList = iPaySelectService.getYhxxList(dto);
            if (yhxxList.size()>0)
            {
                resultRespone.setMessage("查询成功");
                resultRespone.setResult(yhxxList);
            }
            else {
                resultRespone.setCode(-1);
                resultRespone.setMessage("无数据");
            }

        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }
}
