package org.jeecg.modules.app.controller.my;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.app.dto.AddFamilyMembersDTO;
import org.jeecg.modules.app.dto.FindFamilyMembersDTO;
import org.jeecg.modules.system.app.entity.FamilyMembers;
import org.jeecg.modules.system.app.service.IFamilyMembersService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 家庭成员
 */
@Slf4j
@Tag(name = "APP家庭成员信息")
@RestController
@RequestMapping("/app/my/familyMembers")
public class AppFamilyMembersController {

    @Autowired
    IFamilyMembersService iFamilyMembersService;

    @Autowired
    ISysUserService iSysUserService;


    @GetMapping("/findFamilyMembers")
    @Operation(summary = "分页查询家庭成员")
    public Result<IPage<FamilyMembers>> findFamilyMembers(FindFamilyMembersDTO findFamilyMembersDTO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        findFamilyMembersDTO.setUserId(loginUser.getId());
        return iFamilyMembersService.findFamilyMembers(findFamilyMembersDTO);
    }


    @PostMapping("/addFamilyMembers")
    @Operation(summary = "添加家庭成员")
    @Transactional
    public Result<String> addFamilyMembers(@Validated @RequestBody AddFamilyMembersDTO addFamilyMembersDTO){
        if (addFamilyMembersDTO.getRealNameStatus()!=1){
            return Result.error("该添加人员未实名，无法进行添加");
        }
        if (StringUtils.isNotEmpty(addFamilyMembersDTO.getUserId())){
            SysUser sysUser = iSysUserService.getById(addFamilyMembersDTO.getUserId());
            if (sysUser==null||sysUser.getDelFlag()==1){
                return Result.error("该添加人员未查询到所属用户，无法进行添加");
            }
            iFamilyMembersService.addFamilyMembers(addFamilyMembersDTO);
        }
        LoginUser principal = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId=principal.getId();
        addFamilyMembersDTO.setUserId(userId);
        return iFamilyMembersService.addFamilyMembers(addFamilyMembersDTO);
    }

    @GetMapping("/FindByIdFamilyMembers")
    @Operation(summary = "根据id查询家庭成员")
    public Result<FamilyMembers> addFamilyMembers(@RequestParam("id") String id){
        return Result.ok(iFamilyMembersService.getById(id));
    }

    @PostMapping("/editFamilyMembers")
    @Operation(summary = "修改家庭成员")
    public Result<String> editFamilyMembers(@Validated @RequestBody AddFamilyMembersDTO addFamilyMembersDTO){
        return iFamilyMembersService.editFamilyMembers(addFamilyMembersDTO);
    }


    @GetMapping("/deleteFamilyMembers")
    @Operation(summary = "删除家庭成员")
    public Result<String> deleteFamilyMembers(@RequestParam("id") String id){
        return iFamilyMembersService.deleteFamilyMembers(id);
    }

}
