package com.pj.tb_people;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.api.dto.BankInfoDto;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.tb_group.TbGroup;
import com.pj.tb_group.TbGroupService;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Controller: tb_people -- 边民(app接口)
 *
 * @Author Mechrevo
 * @Date 2023 07 24 14 56
 **/
@RestController
@RequestMapping("/app/TbPeople/")
public class TbPeopleAppController {

    @Autowired
    private TbPeopleService tbPeopleService;
    @Resource
    private TbGroupService tbGroupService;

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

    /**
     * 改个人信息
     */
    @RequestMapping("update")
    public AjaxJson update(TbPeople t) {
        if (!StpAPPUserUtil.isLogin()) {
            return AjaxJson.getError("未登录");
        }
        tbPeopleService.update(t);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("getGroupPeople")
    public AjaxJson getGroupPeople() {
        APPLoginUserInfo loginUserInfo = StpAPPUserUtil.getAPPLoginInfo();
        Long fkId = loginUserInfo.getFk();
        if (loginUserInfo.getUserType() != 2) {
            return AjaxJson.getSuccess();
        }
        TbPeople t = tbPeopleService.getById(fkId);
        if (t.getGroupId()==null){
            return AjaxJson.getSuccess();
        }
        SoMap soMap = SoMap.getRequestSoMap();
        soMap.put("groupId", t.getGroupId());
        List<TbPeople> list = tbPeopleService.getList(soMap.startPage());
        return AjaxJson.getPageData(soMap.getDataCount(), list);
    }

    /**
     * 加入互助组
     */
    @RequestMapping("joinGroup")
    public AjaxJson joinGroup(@RequestParam Long groupId, @RequestParam String groupName) {
        if (!StpAPPUserUtil.isLogin()) {
            return AjaxJson.getError("未登录");
        }
        TbPeople t = tbPeopleService.getById(StpAPPUserUtil.getAPPLoginInfo().getFk());
        t.setGroupId(groupId).setGroupName(groupName);
        tbPeopleService.updateById(t);
        return AjaxJson.getSuccess();
    }


    /**
     * 互助组信息查询
     */
    @RequestMapping("getGroupInfo")
    public AjaxJson getGroupInfo() {
        if (!StpAPPUserUtil.isLogin()) {
            return AjaxJson.getError("未登录");
        }
        APPLoginUserInfo loginUserInfo = StpAPPUserUtil.getAPPLoginInfo();
        Long fkId = loginUserInfo.getFk();
        if (loginUserInfo.getUserType() != 1) {
            return AjaxJson.getSuccess();
        }
        TbPeople t = tbPeopleService.getById(fkId);
        Long groupId = t.getGroupId();
        TbGroup tbGroup = tbGroupService.getById(groupId);
        if (tbGroup != null) {
            return AjaxJson.getSuccessData(tbGroup);
        }
        return AjaxJson.getSuccess();
    }


    /**
     * 边民保存银行卡信息
     */
    @RequestMapping("rpc/saveBankInfo")
    @Deprecated
    public boolean saveBankByPeople(@RequestParam("id") Long id, @RequestParam("bankName") String bankName, @RequestParam("bankAccount") String bankAccount) {
       return false;
       // return tbPeopleService.saveBankByPeople(id, bankName, bankAccount,bankName);
    }

    /**
     * 根据边民姓名与身份证号查询边民是否存在
     */
    @PostMapping("rpc/getByidCard")
    public boolean getByidCard(@RequestParam("name") String name, @RequestParam("idCard") String idCard) {
        return tbPeopleService.getByidCard(name, idCard);
    }

    /**
     * 边民备案验证查询-银行回复新开户或变更数据
     */
    @PostMapping("rpc/updateBySXB005")
    public boolean updateBySXB005(@RequestBody BankInfoDto bankInfoDto) {
        return tbPeopleService.updateBySXB005(bankInfoDto);
    }
}
