package modules.withdraw;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import common.interceptor.LoginInterceptor;
import common.interceptor.empty.EmptyInterface;
import common.interceptor.role.RequiredRoleInterface;
import common.model.User;
import common.model.Withdraw;
import common.utils.http.MyController;
import common.utils.http.MyRet;
import modules.user.UserController;
import modules.user.UserService;

public class WithdrawController extends MyController {
    @Inject
    WithdrawService service;
    @Inject
    UserService userService;
    
    public void hello() {
        renderJson(MyRet.ok(service.hello()));
    }
    
    @Before(LoginInterceptor.class)
    @EmptyInterface({"amount"})
    public void create() {
        // 通过 MyController 获取解析后的 JSON 对象，拦截器也使用了这个方法
        JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);

        // 提现数量解析
        String amountStr = requestBodyJson.getString("amount");
        long amount;
        try {
            amount = Long.parseLong(amountStr);

            if (amount < 1100) {
                renderJson(MyRet.fail("至少要提现1100！"));
                return;
            }
        } catch (Exception e) {
            renderJson(MyRet.fail("提现数量(amount)格式不正确: " + e.getMessage()));
            return;
        }

        User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
        if (StrKit.isBlank(user.getHygWorkerId())) {
            renderJson(MyRet.fail("请先进行实名认证").setCode(MyRet.CODE_NO_AUTH));
            return;
        }
        
        renderJson(service.create(amount, user));
    }

    @Before(LoginInterceptor.class)
    @EmptyInterface({"page_size", "page_number"})
    public void list() {
        JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);

        // 页面大小
        String pageSizeStr = requestBodyJson.getString("page_size");
        int pageSizeInt;
        try {
            pageSizeInt = Integer.parseInt(pageSizeStr);

            if (pageSizeInt <= 0) {
                renderJson(MyRet.fail("页面大小(page_size)期待是正整数，你传的是: " + pageSizeStr));
                return;
            }
        } catch (Exception e) {
            renderJson(MyRet.fail("页面大小(page_size)格式不正确: " + e.getMessage()));
            return;
        }

        // 页码
        String pageNumberStr = requestBodyJson.getString("page_number");
        int pageNumberInt;
        try {
            pageNumberInt = Integer.parseInt(pageNumberStr);

            if (pageNumberInt <= 0) {
                renderJson(MyRet.fail("页码(page_number)期待是正整数，你传的是: " + pageNumberStr));
                return;
            }
        } catch (Exception e) {
            renderJson(MyRet.fail("页码(page_number)格式不正确: " + e.getMessage()));
            return;
        }

        Integer status;
        try {
            status = requestBodyJson.getInteger("status");
        } catch (Exception e) {
            renderJson(MyRet.fail("你传入的 status 有些问题：" + e.getMessage()).setData(requestBodyJson));

            return;
        }
        
        User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
        
        renderJson(service.list(pageNumberInt, pageSizeInt, status, user.getId()));
    }
    
    @Before(LoginInterceptor.class)
    @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
    @EmptyInterface({"withdraw_sn"})
    public void pass() {
        JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
        
        String withdraw_sn = requestBodyJson.getString("withdraw_sn");
        
        if (StrKit.isBlank(withdraw_sn)) {
            renderJson(MyRet.fail("withdraw_sn 不能为空"));
            return;
        }
        Withdraw withdraw = service.findByWithdrawSn(withdraw_sn);
        if (withdraw == null) {
            renderJson(MyRet.fail("withdraw_sn 找不到对应的单据"));
            return;
        }
        
        // 订单状态过滤
        if (withdraw.getStatus() != 10) {
            renderJson(MyRet.fail("只有未审批的才能进行打款"));
            return;
        }

        // 获取操作人的id
        long approverId = userService.findUserByMobileNumber(getSessionAttr("mobile_number")).getId();

        renderJson(service.pass(withdraw, approverId));
    }

    @Before(LoginInterceptor.class)
    @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
    @EmptyInterface({"withdraw_sn", "reason"})
    public void refuse() {
        JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);

        String withdraw_sn = requestBodyJson.getString("withdraw_sn");
        String reason = requestBodyJson.getString("reason");

        Withdraw withdraw = service.findByWithdrawSn(withdraw_sn);
        if (withdraw == null) {
            renderJson(MyRet.fail("withdraw_sn 找不到对应的单据"));
            return;
        }

        // 订单状态过滤
        if (withdraw.getStatus() != 10) {
            renderJson(MyRet.fail("只有未审批的才能进行拒绝"));
            return;
        }

        // 获取操作人的id
        long approverId = userService.findUserByMobileNumber(getSessionAttr("mobile_number")).getId();

        renderJson(service.refuse(withdraw, reason, approverId));
    }

    @Before(LoginInterceptor.class)
    @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
    @EmptyInterface({"page_size", "page_number"})
    public void listByAdmin() {
        JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);

        // 页面大小
        String pageSizeStr = requestBodyJson.getString("page_size");
        int pageSizeInt;
        try {
            pageSizeInt = Integer.parseInt(pageSizeStr);

            if (pageSizeInt <= 0) {
                renderJson(MyRet.fail("页面大小(page_size)期待是正整数，你传的是: " + pageSizeStr));
                return;
            }
        } catch (Exception e) {
            renderJson(MyRet.fail("页面大小(page_size)格式不正确: " + e.getMessage()));
            return;
        }

        // 页码
        String pageNumberStr = requestBodyJson.getString("page_number");
        int pageNumberInt;
        try {
            pageNumberInt = Integer.parseInt(pageNumberStr);

            if (pageNumberInt <= 0) {
                renderJson(MyRet.fail("页码(page_number)期待是正整数，你传的是: " + pageNumberStr));
                return;
            }
        } catch (Exception e) {
            renderJson(MyRet.fail("页码(page_number)格式不正确: " + e.getMessage()));
            return;
        }

        Integer status;
        try {
            status = requestBodyJson.getInteger("status");
        } catch (Exception e) {
            renderJson(MyRet.fail("你传入的 status 有些问题：" + e.getMessage()).setData(requestBodyJson));

            return;
        }

        renderJson(service.list(pageNumberInt, pageSizeInt, status, null));
    }
}
