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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.app.dto.FindFamilyMembersResponseDTO;
import org.jeecg.modules.system.app.entity.FamilyMembers;
import org.jeecg.modules.system.app.service.IFamilyMembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 家庭成员信息表
 * @Author: zx
 * @Date:   2025-07-07
 * @Version: V1.0
 */
@Tag(name="家庭成员信息表")
@RestController
@RequestMapping("/familyMembers")
@Slf4j
public class FamilyMembersController extends JeecgController<FamilyMembers, IFamilyMembersService> {
	@Autowired
	private IFamilyMembersService familyMembersService;
	
	/**
	 * 分页列表查询
	 *
	 * @param familyMembers
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "家庭成员信息表-分页列表查询")
	@Operation(summary="家庭成员信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FamilyMembers>> queryPageList(FamilyMembers familyMembers,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<FamilyMembers> queryWrapper = QueryGenerator.initQueryWrapper(familyMembers, req.getParameterMap());
		Page<FamilyMembers> page = new Page<FamilyMembers>(pageNo, pageSize);
		IPage<FamilyMembers> pageList = familyMembersService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param familyMembers
	 * @return
	 */
	@AutoLog(value = "家庭成员信息表-添加")
	@Operation(summary="家庭成员信息表-添加")
	@RequiresPermissions("familyMembers:nm_family_members:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody FamilyMembers familyMembers) {
		familyMembersService.save(familyMembers);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param familyMembersS
	 * @return
	 */
	@AutoLog(value = "家庭成员信息表-编辑")
	@Operation(summary="家庭成员信息表-编辑")
//	@RequiresPermissions("familyMembers:nm_family_members:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody List<FamilyMembers> familyMembersS) {
		if (familyMembersS==null||familyMembersS.isEmpty()){
			return Result.error("请求参数不能为空");
		}
		for (FamilyMembers membersS : familyMembersS) {
			if (StringUtils.isNotEmpty(membersS.getId())){
				FamilyMembers familyMembers = familyMembersService.getById(membersS.getId());
				if (StringUtils.isNotEmpty(membersS.getRealNameImg())){
					familyMembers.setRealNameImg(membersS.getRealNameImg());
					familyMembersService.updateById(familyMembers);
				}
			}
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "家庭成员信息表-通过id删除")
	@Operation(summary="家庭成员信息表-通过id删除")
	@RequiresPermissions("familyMembers:nm_family_members:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		familyMembersService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "家庭成员信息表-批量删除")
	@Operation(summary="家庭成员信息表-批量删除")
	@RequiresPermissions("familyMembers:nm_family_members:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.familyMembersService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "家庭成员信息表-通过id查询")
	@Operation(summary="家庭成员信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<FamilyMembers> queryById(@RequestParam(name="id",required=true) String id) {
		FamilyMembers familyMembers = familyMembersService.getById(id);
		if(familyMembers==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(familyMembers);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param familyMembers
    */
    @RequiresPermissions("familyMembers:nm_family_members:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FamilyMembers familyMembers) {
        return super.exportXls(request, familyMembers, FamilyMembers.class, "家庭成员信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("familyMembers:nm_family_members:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FamilyMembers.class);
    }

	 /**
	  * 根据用户id查询家庭成员列表
	  * @param userId
	  * @return
	  */

	 @Operation(summary="家庭成员信息表-通过用户id查询家庭成员信息")
	 @GetMapping(value = "/findByUserId")
	 public Result<FindFamilyMembersResponseDTO> findByUserId(@RequestParam(name="userId",required=true) String userId) {
		 return familyMembersService.findByUserId(userId);
	 }
}
