package com.pj.tb_people;

import java.io.IOException;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pj.api.consts.FeignFactory;
import com.pj.api.dto.AppUserDto;
import com.pj.api.dto.HtPeopleDto;
import com.pj.api.dto.PeopleDto;
import com.pj.tb_people.dto.LeaveGroupDto;
import com.pj.tb_people.dto.StartStopDto;
import com.pj.tb_people.vo.BindShopVO;
import com.pj.tb_people.vo.EditPhoneVO;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.multipart.MultipartFile;


/**
 * Controller: tb_people -- 边民
 *
 * @author qzy
 */
@RestController
@RequestMapping("/TbPeople/")
public class TbPeopleController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbPeopleService tbPeopleService;

    /**
     * 增
     */
    @RequestMapping("add")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_ADD)
    public AjaxJson add(TbPeople t) {
        tbPeopleService.add(t);
        t = tbPeopleService.getById(SP.publicMapper.getPrimarykey());
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_DEL)
    public AjaxJson delete(Long id) {
        tbPeopleService.delete(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_DEL)
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        int line = SP.publicMapper.deleteByIds(TbPeople.TABLE_NAME, ids);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_EDIT)
    public AjaxJson update(TbPeople t) {
        tbPeopleService.update(t);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("editPhone")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_EDIT)
    public AjaxJson editPhone(EditPhoneVO editPhoneVO) {
        tbPeopleService.editPhone(editPhoneVO);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("bindShop")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_BIND_SHOP)
    public AjaxJson bindShop(BindShopVO shopVO) {
        tbPeopleService.bindShop(shopVO);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("removeBind")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_BIND_SHOP)
    public AjaxJson removeBind(String id) {
        tbPeopleService.removeBind(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 移除组内边民
     */
    @RequestMapping("leaveGroup")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_EDIT)
    public AjaxJson leaveGroup(@Validated LeaveGroupDto leaveGroupDto) {
        return AjaxJson.toAjax(tbPeopleService.leaveGroup(leaveGroupDto));
    }

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

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    @SaCheckPermission(TbPeople.PERMISSION_CODE)
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbPeople> list = tbPeopleService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 查集合 - 查询没有进组的边民
     */
    @RequestMapping("getLeftPeople")
    @SaCheckPermission(TbPeople.PERMISSION_CODE)
    public AjaxJson getLeftPeople() {
        List<TbPeople> list = tbPeopleService.getLeftPeople();
        return AjaxJson.getPageData(list.size(), list);
    }

    /**
     * 改 - 删除状态(0=禁用,1=启用)
     */
    @RequestMapping("updateDeleteStatus")
    @SaCheckPermission(TbPeople.PERMISSION_CODE_EDIT)
    public AjaxJson updateDeleteStatus(String id, Integer value) {
        int line = SP.publicMapper.updateColumnById(TbPeople.TABLE_NAME, "delete_status", value, id);
        return AjaxJson.getByLine(line);
    }

    /**
     * 审核
     */
    @PostMapping("judge")
    public AjaxJson judge(@RequestBody TbPeople tbPeople) {
        boolean judge = tbPeopleService.judge(tbPeople);
        if (judge) {
            return AjaxJson.getSuccess("审核成功!");
        }
        return AjaxJson.getError("审核失败!");
    }

    /**
     * 启/停
     */
    @PostMapping("isLock")
    public AjaxJson isLock(@Validated @RequestBody StartStopDto startStopDto) {
        boolean lock = tbPeopleService.isLock(startStopDto);
        return AjaxJson.getSuccess(lock + "");
    }

    /**
     * 身份识别
     */
    @PostMapping("identification")
    //TODO: 身份识别
    public AjaxJson identification() {
        tbPeopleService.identification();
        return AjaxJson.getSuccess();
    }

    /**
     * 边民申请加入互助组
     *
     * @author loovi
     * @date
     */
    @RequestMapping("applyAddGroup")
    public AjaxJson addAppleGroup(@RequestParam("peopleIds") String peopleIds, @RequestParam("groupId") Long groupId) {
        String[] peopleIdarr = peopleIds.split(",");
        for (String peopleId : peopleIdarr) {
            tbPeopleService.applyAddGroup(Long.valueOf(peopleId), groupId);
        }
        return AjaxJson.getSuccess("申请成功等待组长批准");
    }

    /**
     * 获取申请互助组的申请人列表
     *
     * @author loovi
     * @date
     */
    @RequestMapping("getApplyList")
    public AjaxJson getApplyList(@RequestParam("leaderId") Long leaderId) {
        SoMap so = SoMap.getRequestSoMap();
        so.set("leader_id", leaderId);
        AjaxJson ajaxJson = tbPeopleService.getApplyList(so);
        return ajaxJson;
    }



    /**
     * 组长是否同意申请
     *
     * @author loovi
     * @date
     */
    @RequestMapping("passApply")
    public AjaxJson passApply(@RequestParam("groupId") Long groupId, @RequestParam("peopleIds") String peopleIds, @RequestParam("approve") int approve) {
        approve = 1;//同意
        String[] peopleIdarr = peopleIds.split(",");
        for (String peopleId : peopleIdarr) {
            tbPeopleService.passApply(groupId, Long.valueOf(peopleId), approve);
        }
        return AjaxJson.getSuccess("边民加入互助组成功");
    }

    /**
     * 边民扫脸确认进境申报
     *
     * @param orderID
     * @return
     */
    @PostMapping("entryDeclaration")
    public AjaxJson entryDeclaration(Long orderID) {
        boolean declaration = tbPeopleService.entryDeclaration(orderID);
        if (declaration) return AjaxJson.getSuccess();
        return AjaxJson.getError();
    }

    /**
     * 保存银行卡信息
     */
    @RequestMapping("saveBankInfo")
    public AjaxJson saveBankByPeople(@RequestParam("id") Long id, @RequestParam("bankNo") String bankNo, @RequestParam("bankAccount") String bankAccount, @RequestParam("bankName") String bankName) {
        boolean result = tbPeopleService.saveBankByPeople(id, bankNo, bankAccount,bankName);
        if (result) {
            return AjaxJson.getSuccess("修改成功!");
        }
        return AjaxJson.getError("修改失败!");
    }

    /**
     * 数据导入接口
     *
     * @param file
     * @return
     */
    @RequestMapping("peopleImport")
    @ResponseBody
    public AjaxJson dataImport(@RequestParam("file") MultipartFile file) {
        try {
            String importData = tbPeopleService.importData(file);
            AjaxJson.getSuccess(importData);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return AjaxJson.getError();
    }

    /**
     * 导出
     *
     * @param keyword
     * @return
     */
    @RequestMapping("peopleOutport")
    @ResponseBody
    public AjaxJson dataOutport(@RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "filepath", required = false) String filepath) {
        try {
            String importData = tbPeopleService.outportExcel(keyword, filepath);
            AjaxJson.getSuccess(importData);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return AjaxJson.getError();
    }

    @RequestMapping("getPeopleForGroup")
    @SaCheckPermission(TbPeople.PERMISSION_CODE)
    public AjaxJson getPeopleForGroup(@RequestParam Long tradeAreaId) {
        return AjaxJson.getSuccessData(tbPeopleService.getPeopleForGroup(tradeAreaId, 1));
    }


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

    /**
     * 注册身份时对应的表添加数据
     */
    @PostMapping("rpc/peopleDto")
    public boolean peopleDto(@RequestBody HtPeopleDto peopleDto) throws Exception {
        StaticLog.info("async people:{}", JSONUtil.toJsonStr(peopleDto));
        return tbPeopleService.peopleDto(peopleDto);
    }

    /**
     * 根据ID获取边民信息
     */
    @RequestMapping("rpc/getRpcById")
    public PeopleDto getRpcById(@RequestParam("id") Long peopleId) {
        PeopleDto rpcById = tbPeopleService.getRpcById(peopleId);
        return rpcById;
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("rpc/getPeopleList")
    public List<PeopleDto> getPeopleList(@RequestParam("groupId") Long groupId) {
        SoMap so = SoMap.getRequestSoMap();
        List<PeopleDto> list = tbPeopleService.getPeopleList(so.startPage(), groupId);
        return list;
    }

    /**
     * 根据航通主键查询appUser
     */
    @RequestMapping("rpc/getAppUserByBorderPutrecNo")
    public AppUserDto getAppUserByPlatSeqNo(@RequestParam("borderPutrecNo") String borderPutrecNo) {
        AppUserDto appUserByPlatSeqNo = tbPeopleService.getAppUserByBorderPutrecNo(borderPutrecNo);
        return appUserByPlatSeqNo;
    }
}
