package com.ruoyi.ibs.list.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.list.domain.FamilyMember;
import com.ruoyi.ibs.list.domain.Member;
import com.ruoyi.ibs.list.domain.RelationPath;
import com.ruoyi.ibs.list.service.FamilyPathService;
import com.ruoyi.ibs.list.service.IFamilyMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/system/member")
@Api("家庭成员管理")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private FamilyPathService pathService;


    /**
     * 查询【请填写功能名称】列表
     */
    //@PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    @ApiOperation("查询家庭成员列表")
    public TableDataInfo list(FamilyMember familyMember)
    {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        return getDataTable(list);
    }

    @GetMapping("listByIdCard")
    @ApiOperation("根据身份证号查询家庭成员列表")
    public TableDataInfo listByIdCard(String idCard)
    {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberListByIdCard(idCard);
        return getDataTable(list);
    }

    @GetMapping("/getFamilyTreeByIdCard")
    @ApiOperation("根据客户号查询家庭机构树")
    public Map<String, Object>  list(String idCard)
    {
    return familyMemberService.buildFamilyGraph(idCard);
    }



    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, FamilyMember familyMember)
    {
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value = "/{idCard}")
    public AjaxResult getInfo(@PathVariable("idCard") String idCard)
    {
        return success(familyMemberService.selectFamilyMemberByIdCard(idCard));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:member:add')")
    @PostMapping
    public AjaxResult add(@RequestBody FamilyMember familyMember)
    {
        return toAjax(familyMemberService.insertFamilyMember(familyMember));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody FamilyMember familyMember)
    {
        return toAjax(familyMemberService.updateFamilyMember(familyMember));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:member:remove')")
	@DeleteMapping("/{idCards}")
    public AjaxResult remove(@PathVariable String[] idCards)
    {
        return toAjax(familyMemberService.deleteFamilyMemberByIdCards(idCards));
    }


    @PostMapping("/addRelatePerson")
    @ApiOperation("为某个客户增加家庭成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "card", value = "客户基准身份证号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "relation", value = "关系类型,支持父节点father,母节点mother,兄弟姐妹节点GG||JJ||DD||MM，子节点son，孙子grandson，孙女granddaughter，配偶节点spouse", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult addRelatePerson(String card,String relation, Member memeber)
    {
        if(relation.equals("GG") || relation.equals("JJ") || relation.equals("DD") || relation.equals("MM")){
            relation = "brother";
        }
        if(relation.equals("doughter")){
            relation = "son";
        }
        if(relation.equals("granddoughter")){
            relation = "grandson";
        }
        return toAjax(familyMemberService.addRelatePerson(card,relation,memeber));
    }

    @PostMapping("/removePerson")
    @ApiOperation("删除家庭成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCard", value = "删除人身份证号，只允许删除叶子节点", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult addRelatePerson(String idCard)
    {
        return toAjax(familyMemberService.removeRelatePerson(idCard));
    }


    @PostMapping("/addSelf")
    @ApiOperation("将自身加入家族图谱")

    public AjaxResult addSelf(Member memeber)
    {
        return toAjax(familyMemberService.addSelf(memeber));
    }

    @GetMapping("/getById")
    @ApiOperation("根据客户身份证号查询其与其余家庭成员的关系路径")
    public ResponseEntity<Map<String, RelationPath>> getFamilyTree1(String idCard) {
        return ResponseEntity.ok(pathService.findAllPaths(idCard));
    }
}
