package com.pj.tb_purchaser;

import cn.hutool.core.util.IdcardUtil;
import com.pj.api.dto.PurchaserDto;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.tb_purchaser.param.PurchaserParam;
import com.pj.utils.sg.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;


/**
 * Controller: tb_purchaser -- 收购商(app接口)
 *
 * @author lbl
 */
@RestController
@RequestMapping("/app/TbPurchaser/")
public class TbPurchaserAppController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbPurchaserService tbPurchaserService;

    /**
     * 查个人信息 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(Long id) {
        TbPurchaser t = tbPurchaserService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 修改认证信息
     */
    @RequestMapping("update")
    public AjaxJson update(TbPurchaser t) {
        tbPurchaserService.update(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 收购商认证
     */
    @RequestMapping("identification")
    public AjaxJson identification( @Validated PurchaserParam purchaser) throws ParseException {
        if (!StpAPPUserUtil.isLogin()) {
            return AjaxJson.getError("用户未登录!!!");
        }
        APPLoginUserInfo appLoginInfo=StpAPPUserUtil.getAPPLoginInfo();
        boolean identification = tbPurchaserService.identification(purchaser,appLoginInfo.getLoginId(),appLoginInfo.getFk());
        if (!identification) return AjaxJson.getError("收购商认证信息提交失败!!!");

        return AjaxJson.getSuccess("收购商认证信息已提交,管理员审核中,请耐心等待!");
    }

    @RequestMapping("info")
    public AjaxJson info() {
		if (!StpAPPUserUtil.isLogin()){
			return AjaxJson.getError("用户未登录");
		}
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        TbPurchaser tbPurchaser = tbPurchaserService.getById(appLoginInfo.getFk());
        return AjaxJson.getSuccessData(tbPurchaser);
    }


    /**
     * 收购商认证状态查询
     */
    @RequestMapping("auditStatus")
    public AjaxJson auditStatus(Long appUserId) {
        return AjaxJson.getSuccessData(tbPurchaserService.auditStatus(appUserId));
    }

    /*------            正常业务👆👆    rpc远程调用👇👇                         -----------*/

    /**
     * 根据ID查商户
     *
     * @param id
     * @return
     */
    @RequestMapping("rpc/getByPurchaserId")
    public PurchaserDto getByPurchaserId(@RequestParam("id") Long id) {
        PurchaserDto byPurchaserId = tbPurchaserService.getByPurchaserId(id);
        return byPurchaserId;
    }

    /**
     * 收购商保存银行卡信息
     */
    @RequestMapping("rpc/saveBankInfo")
    public boolean saveBankByPurchaser(@RequestParam("id") Long id, @RequestParam("bankName") String bankName, @RequestParam("bankAccount") String bankAccount) {
        return tbPurchaserService.saveBankByPurchaser(id, bankName, bankAccount);
    }
}
