package com.ruoyi.web.controller.zhang;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
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.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfDoctor;
import com.ruoyi.domain.ZfRelation;
import com.ruoyi.domain.dto.Genealogy;
import com.ruoyi.domain.dto.GenealogyExportDto;
import com.ruoyi.domain.dto.RelationDto;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfFamilyService;
import com.ruoyi.service.ZfRoleService;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.*;
import java.util.regex.Pattern;
import static com.ruoyi.common.core.page.TableSupport.PAGE_NUM;
import static com.ruoyi.common.core.page.TableSupport.PAGE_SIZE;
/**
*
* 前端控制器
*
*
* @author ojq
* @since 2023-03-14
*/
@Slf4j
@RestController
@RequestMapping("/self/user")
public class ZInfoUserController extends BaseController {
@Resource
private ZInfoUserService zInfoUserService;
@Resource
private ZfFamilyService zfFamilyService;
@Resource
private ZfRoleService zfRoleService;
@Resource
private SysUserMapper sysUserMapper;
// @GetMapping("/all")
// public AjaxResult listAll(ZInfoUser zInfoUser){
// Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
// Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
// return zInfoUserService.selectInfoList(zInfoUser, pageNum, pageSize);
// }
/**
* 导出个人详细信息记录列表
*/
// @PreAuthorize("@ss.hasPermi('system:property:export')")
@Log(title = "个人详细信息记录", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, ZInfoUser zInfoUser) {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
zInfoUser.setUserId(userId);
List list = zInfoUserService.selectByCondition(zInfoUser);
log.info("导出记录为:{}", list);
ExcelUtil util = new ExcelUtil<>(ZInfoUser.class);
util.exportExcel(response, list, "个人详细信息记录数据");
}
/**
* 导入个人详细信息记录列表
*/
@Log(title = "用户管理", businessType = BusinessType.IMPORT)
@PostMapping("/importData")
public AjaxResult importData(@RequestParam("excelImport") MultipartFile file) throws Exception {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
ExcelUtil util = new ExcelUtil<>(ZInfoUser.class);
List eventList = util.importExcel(file.getInputStream());
log.info("个人详细信息列表为:{}", eventList);
if (eventList.size() > 1) {
return AjaxResult.error("导入个人信息只能有一条记录");
}
ZInfoUser zInfoUser = eventList.get(0);
zInfoUser.setUserId(userId);
if (zInfoUserService.updateById(zInfoUser)) {
return AjaxResult.success("导入数据成功");
}
return AjaxResult.error("导入数据失败");
}
/**
* 获取个人详细信息记录详细信息
*/
// @PreAuthorize("@ss.hasPermi('system:property:query')")
@GetMapping()
public AjaxResult getInfo() {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
return AjaxResult.success(zInfoUserService.getInfoById(userId));
}
@GetMapping("/{id}")
public AjaxResult getInfoById(@PathVariable("id") Long id) {
// SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = id;
return AjaxResult.success(zInfoUserService.getInfoById(userId));
}
//
/**
* 修改个人详细信息记录
*/
// @PreAuthorize("@ss.hasPermi('system:property:add')")
@Log(title = "个人详细信息记录", businessType = BusinessType.INSERT)
@PutMapping
public AjaxResult update(@RequestBody ZInfoUser zInfoUser) {
// if (!Pattern.matches("^[\\d]+(?:,[\\d]+)*$",zInfoUser.getFamilyId())) {
// throw new RuntimeException("请输入只有数字和英文逗号的字符串,且数字和逗号必须交替出现");
// }
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
zInfoUser.setUserId(userId);
return toAjax(zInfoUserService.saveOrUpdate(zInfoUser));
}
// /**
// * 修改个人详细信息记录
// */
//// @PreAuthorize("@ss.hasPermi('system:property:edit')")
// @Log(title = "个人详细信息记录", businessType = BusinessType.UPDATE)
// @PutMapping
// public AjaxResult edit(@RequestBody ZInfoUser zInfoUser)
// {
// return toAjax(zInfoUserService.updateById(zInfoUser));
// }
//
/**
* 删除个人详细信息记录
*/
// @PreAuthorize("@ss.hasPermi('system:property:remove')")
@Log(title = "个人详细信息记录", businessType = BusinessType.DELETE)
@DeleteMapping()
public AjaxResult remove() {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
return toAjax(zInfoUserService.removeById(userId));
}
/**
* 查询家庭主要成员及关系
*/
@GetMapping("/relation")
public AjaxResult listMyRelation() {
return zInfoUserService.searchMyRelation();
}
@PostMapping("/relation")
public AjaxResult addRelation(@RequestBody ZfRelation zfRelation){
return zInfoUserService.addRelation(zfRelation);
}
@PutMapping("/relation")
public AjaxResult updateRelation(@RequestBody ZfRelation zfRelation){
return zInfoUserService.updateRelation(zfRelation);
}
@DeleteMapping("/relation/{ids}")
public AjaxResult deleteRelation(@PathVariable Long[] ids){
return zInfoUserService.deleteRelation(ids);
}
// /**
// * 授权
// */
// @PostMapping("/empower")
// public AjaxResult empower(@RequestBody EmpowerDto empowerDto){
//
// return zInfoUserService.empower(empowerDto);
// }
/**
* 根据用户id判断是否是子孙 tpfid:自己id 根据用户id判断是否是异性 不是本家的人选
*/
@GetMapping("/uidAndUJd/{tpfid}/{sex}")
public AjaxResult findByUidAndUid(@PathVariable("tpfid") Long tpfid, @PathVariable("sex") Integer sex)
{
// Integer sex = zInfoUserService.getInfoById(tpfid).getSex();
//sex == 0 -> 男
if (sex == 1){
sex = 0;
}
else if (sex == 0){
sex = 1;
}
List zInfoUsers = zInfoUserService.getDataBySex(sex);
List> satisfyIds = new ArrayList<>();
HashMap bs = new HashMap<>();
for (ZInfoUser zInfoUser:zInfoUsers) {
if (!zInfoUserService.findByuid(zInfoUser.getUserId(), tpfid) && (zInfoUser.getMaritalStatus() == null || zInfoUser.getMaritalStatus() != 1)){
bs.put(zInfoUser.getUserId(),zInfoUser.getNickName());
}
}
satisfyIds.add(bs);
return AjaxResult.success(satisfyIds);
}
/**
* 根据用户id判断是否是子孙 uid:长辈id,tpfid:我自己的zinfoid
*/
@GetMapping("/uidUJd/{uid}/{tpfid}")
public AjaxResult findByUidAndUid2(@PathVariable("uid") Long uid ,@PathVariable("tpfid") Long tpfid){
return AjaxResult.success( zInfoUserService.findByuid(uid,tpfid));
}
@GetMapping("/sex/{sex}")
public AjaxResult findBySex(@PathVariable("sex") Integer sex)
{
// Integer sex = zInfoUserService.getInfoById(tpfid).getSex();
//sex == 0 -> 男
if (sex == 1){
sex = 0;
}
else if (sex == 0){
sex = 1;
}
List zInfoUsers = zInfoUserService.getDataBySex(sex);
List> satisfyIds = new ArrayList<>();
HashMap bs = new HashMap<>();
for (ZInfoUser zInfoUser:zInfoUsers) {
if (zInfoUser.getMaritalStatus() ==null || zInfoUser.getMaritalStatus() != 1 )
bs.put(zInfoUser.getUserId(),zInfoUser.getNickName());
}
satisfyIds.add(bs);
return AjaxResult.success(satisfyIds);
}
/**
* 根据用户id判断是否是异性
*/
// /**
// * 根据用户id判断是否是子孙(uid:?,tpfid:自己id) 根据用户id判断是否是异性 不是本家的人选
// */
// @GetMapping("/uidAndUJd/{uid}/{tpfid}/{sex}")
// public AjaxResult findByUidAndUid(@PathVariable("uid") Long uid,@PathVariable("tpfid") Long tpfid, @PathVariable("sex") Integer sex)
// {
//// Integer sex = zInfoUserService.getInfoById(tpfid).getSex();
// //sex == 0 -> 男
// if (sex == 1){
// sex = 0;
// }
// else if (sex == 0){
// sex = 1;
// }
// List zInfoUsers = zInfoUserService.getDataBySex(sex);
// List nickName = new ArrayList<>();
// for (ZInfoUser zInfoUser:zInfoUsers) {
// if (!zInfoUserService.findByuid(uid, tpfid)){
// if (zInfoUser.getIsMyFamily() == 1)
// nickName.add(zInfoUser.getNickName());
// }
// }
// return AjaxResult.success(nickName);
// }
/**
* 家根网
*/
@GetMapping("/root")
public AjaxResult listAllPeopleWithTree(@PathParam("depth") Integer depth,@PathParam("clanId") Integer clanId){
return zInfoUserService.listWithTree(depth, clanId);
}
/**
* 根据家庭成员找到所有人
*/
@GetMapping("/allFamInfo")
public AjaxResult listAllFamilyPeople(@PathParam("clanId") Integer clanId)
{
return zInfoUserService.listAllFamilyPeople(clanId);
}
/**
* 找到所有的成员
*/
@GetMapping("/all")
public AjaxResult listAllPeople(){
return zInfoUserService.listAllExceptAdmin();
}
/**
* 新增、修改父子关系
*/
@PutMapping("/setParent")
public AjaxResult addParent(@PathParam("fatherId")Long fatherId,@PathParam("motherId")Long motherId){
return zInfoUserService.addParent(fatherId,motherId);
}
/**
* 根据本人id查询本人信息以及配偶信息
*/
@GetMapping("/listMyself/{id}")
public AjaxResult listMySelfAndSpouse(@PathVariable("id") Long id){
return zInfoUserService.listMySelfAndSpouse(id);
}
/**
* 家谱
*/
@GetMapping("/list/genealogy")
public AjaxResult listGenealogy(Genealogy genealogy){
Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
return zInfoUserService.listGenealogy(genealogy,pageNum,pageSize);
}
/**
* 导出家谱
*/
@PostMapping("/export/genealogy")
public void exportGenealogy(@RequestBody List params, HttpServletResponse response){
List list= zInfoUserService.selectDatas(params);
ExcelUtil util = new ExcelUtil<>(Genealogy.class);
util.exportExcel(response,list,"家谱记录数据");
}
/**
* 家谱2.0
*/
@GetMapping("/list/genealogy2")
public AjaxResult listGenealogy2(Genealogy genealogy){
Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
return zInfoUserService.listGenealogy2(genealogy,pageNum,pageSize);
}
@GetMapping("/list/getInfoByfid/{fid}")
public AjaxResult getInfoByfid(@PathVariable("fid") Long fid)
{
return zInfoUserService.getInfoByfid(fid);
}
}