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

import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.dbhelper.DbHelper;
import com.tofly.feesapi.common.enums.SysEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/api/wechatpay/customerInfo")
@Api(tags = "微网厅-用户绑定接口")
public class BindCustomerEasyController {


    @GetMapping("/getCustomerInFo")
    @ApiOperation(value = "[Easy]获取用户欠费和余额信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone getCustomerInFo(@RequestParam("customerNo") String customerNo) throws Exception {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("用户编号不能为空！");
            return resultModel;
        }

        //endregion


        //region 业务处理
        DbHelper db = DbHelper.getDbHelper();
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("V_CUSTOMERNO", customerNo);
        params.put("V_CUR", arrayList);
        params.put("V_SFCG", "0");

        //这里要注意参数前面的map固定写法不然读取不了数据
        Map mp = db.procurdeCmd("{call PROC_Agentfee_customerinfo(" +
                "#{V_CUSTOMERNO,mode=IN,jdbcType=VARCHAR}," +
                "#{V_CUR,mode=OUT,jdbcType=CURSOR}," +
                "#{V_SFCG,mode=OUT,jdbcType=VARCHAR}" +
                ")}", params, SysEnum.MyBatis.getName());

        //存储过程返回的参数
        ArrayList<Object> arr = (ArrayList<Object>) mp.get("V_CUR");
        if (mp.get("V_SFCG").equals("1")) {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(未知原因)！");
            return resultModel;
        } else if (mp.get("V_SFCG").equals("2")) {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(用户不存在)");
            return resultModel;
        } else if (mp.get("V_SFCG").equals("3")) {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(扎账时间不能缴费，请明天再试，谢谢)！");
            return resultModel;
        } else if (mp.get("V_SFCG").equals("4")) {
            resultModel.setCode(-1);
            resultModel.setMessage("上次缴费下发未完成，不能继续缴费，请稍后再试，谢谢！");
            return resultModel;
        } else if (mp.get("V_SFCG").equals("5")) {
            resultModel.setCode(-1);
            resultModel.setMessage("上次充值未上表，不能继续缴费，请上表后再试，谢谢！");
            return resultModel;
        } else if (mp.get("V_SFCG").equals("-999")) {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(逻辑错误)！");
            return resultModel;
        } else {
            resultModel.setResult(arr.get(0));
            resultModel.setMessage("成功！");

            return resultModel;
        }
        //endregion
    }

    @GetMapping("/getCustomerBasicInFo")
    @ApiOperation(value = "[Easy]获取用户基础信息（总预存，扣减，余额，欠费等）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone getCustomerBasicInFo(@RequestParam("customerNo") String customerNo) throws Exception {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("用户编号不能为空！");
            return resultModel;
        }

        //endregion


        //region 业务处理
        DbHelper db = DbHelper.getDbHelper();
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("V_CUSTOMERNO", customerNo);
        params.put("V_CUR", arrayList);
        params.put("V_SFCG", "0");

        //这里要注意参数前面的map固定写法不然读取不了数据
        Map mp = db.procurdeCmd("{call PROC_AGENTFEE_CXCUSTOMERINFO(" +
                "#{V_CUSTOMERNO,mode=IN,jdbcType=VARCHAR}," +
                "#{V_CUR,mode=OUT,jdbcType=CURSOR}," +
                "#{V_SFCG,mode=OUT,jdbcType=VARCHAR}" +
                ")}", params, SysEnum.MyBatis.getName());

        //存储过程返回的参数
        ArrayList<Object> arr = (ArrayList<Object>) mp.get("V_CUR");
        if (mp.get("V_SFCG").equals("1")) {
            resultModel.setResult(arr.get(0));
            resultModel.setMessage("成功！");
            return resultModel;

        } else {

            resultModel.setCode(-1);
            resultModel.setMessage("查询失败！");
            return resultModel;
        }
        //endregion
    }

    @GetMapping("/getCustomerReadMeterInFo")
    @ApiOperation(value = "[Easy]获取用户抄表明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone getCustomerReadMeterInFo(@RequestParam("customerNo") String customerNo) throws Exception {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("用户编号不能为空！");
            return resultModel;
        }

        //endregion


        //region 业务处理
        DbHelper db = DbHelper.getDbHelper();
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("V_CUSTOMERNO", customerNo);
        params.put("V_CUR", arrayList);
        params.put("V_SFCG", "0");

        //这里要注意参数前面的map固定写法不然读取不了数据
        Map mp = db.procurdeCmd("{call PROC_AGENTFEE_CXCBINFO(" +
                "#{V_CUSTOMERNO,mode=IN,jdbcType=VARCHAR}," +
                "#{V_CUR,mode=OUT,jdbcType=CURSOR}," +
                "#{V_SFCG,mode=OUT,jdbcType=VARCHAR}" +
                ")}", params, SysEnum.MyBatis.getName());

        //存储过程返回的参数
        ArrayList<Object> arr = (ArrayList<Object>) mp.get("V_CUR");
        if (mp.get("V_SFCG").equals("1")) {

            resultModel.setResult(arr);
            resultModel.setMessage("成功！");
            return resultModel;
        } else {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(未知原因)！");
            return resultModel;
        }
        //endregion
    }


    @GetMapping("/getCustomerQfInFo")
    @ApiOperation(value = "[Easy]获取用户欠费明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone getCustomerQfInFo(@RequestParam("customerNo") String customerNo) throws Exception {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("用户编号不能为空！");
            return resultModel;
        }

        //endregion


        //region 业务处理
        DbHelper db = DbHelper.getDbHelper();
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("V_CUSTOMERNO", customerNo);
        params.put("V_CUR", arrayList);
        params.put("V_SFCG", "0");

        //这里要注意参数前面的map固定写法不然读取不了数据
        Map mp = db.procurdeCmd("{call PROC_AGENTFEE_CXQFINFO(" +
                "#{V_CUSTOMERNO,mode=IN,jdbcType=VARCHAR}," +
                "#{V_CUR,mode=OUT,jdbcType=CURSOR}," +
                "#{V_SFCG,mode=OUT,jdbcType=VARCHAR}" +
                ")}", params, SysEnum.MyBatis.getName());

        //存储过程返回的参数
        ArrayList<Object> arr = (ArrayList<Object>) mp.get("V_CUR");
        if (mp.get("V_SFCG").equals("1")) {

            resultModel.setResult(arr);
            resultModel.setMessage("成功！");
            return resultModel;
        } else {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(未知原因)！");
            return resultModel;
        }
        //endregion
    }

    @GetMapping("/getCustomerChargeInFo")
    @ApiOperation(value = "[Easy]获取用户交易明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone getCustomerChargeInFo(@RequestParam("customerNo") String customerNo) throws Exception {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("用户编号不能为空！");
            return resultModel;
        }

        //endregion


        //region 业务处理
        DbHelper db = DbHelper.getDbHelper();
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("V_CUSTOMERNO", customerNo);
        params.put("V_CUR", arrayList);
        params.put("V_SFCG", "0");

        //这里要注意参数前面的map固定写法不然读取不了数据
        Map mp = db.procurdeCmd("{call PROC_AGENTFEE_CXSFINFO(" +
                "#{V_CUSTOMERNO,mode=IN,jdbcType=VARCHAR}," +
                "#{V_CUR,mode=OUT,jdbcType=CURSOR}," +
                "#{V_SFCG,mode=OUT,jdbcType=VARCHAR}" +
                ")}", params, SysEnum.MyBatis.getName());

        //存储过程返回的参数
        ArrayList<Object> arr = (ArrayList<Object>) mp.get("V_CUR");
        if (mp.get("V_SFCG").equals("1")) {

            resultModel.setResult(arr);
            resultModel.setMessage("成功！");
            return resultModel;
        } else {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(未知原因)！");
            return resultModel;
        }
        //endregion
    }

    @GetMapping("/getBindCustomerAndQf")
    @ApiOperation(value = "[Easy]获取绑定用户和欠费信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信id", required = true, dataType = "String")
    })
    public ResultRespone getBindCustomerAndQf(@RequestParam("openid") String openid) throws Exception {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(openid)) {
            resultModel.setCode(-1);
            resultModel.setMessage("微信id不能为空！");
            return resultModel;
        }

        //endregion


        //region 业务处理
        DbHelper db = DbHelper.getDbHelper();
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("V_OPENID", openid);
        params.put("V_CUR", arrayList);
        params.put("V_SFCG", "0");

        //这里要注意参数前面的map固定写法不然读取不了数据
        Map mp = db.procurdeCmd("{call PROC_AGENTFEE_CXBINDCUSTOMERQF(" +
                "#{V_OPENID,mode=IN,jdbcType=VARCHAR}," +
                "#{V_CUR,mode=OUT,jdbcType=CURSOR}," +
                "#{V_SFCG,mode=OUT,jdbcType=VARCHAR}" +
                ")}", params, SysEnum.MyBatis.getName());

        //存储过程返回的参数
        ArrayList<Object> arr = (ArrayList<Object>) mp.get("V_CUR");
        if (mp.get("V_SFCG").equals("1")) {

            resultModel.setResult(arr);
            resultModel.setMessage("成功！");
            return resultModel;
        } else {
            resultModel.setCode(-1);
            resultModel.setMessage("查询失败(未知原因)！");
            return resultModel;
        }
    }
}
