package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxFamilyMemberBo;
import com.propertyManagement.business.domain.vo.WxFamilyMemberVo;
import com.propertyManagement.business.service.IWxFamilyMemberService;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.helper.LoginHelper;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 家庭成员
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/familyMember")
public class WxFamilyMemberController extends BaseController {

    private final IWxFamilyMemberService iWxFamilyMemberService;

    /**
     * 查询家庭成员列表
     */
    @SaCheckPermission("business:familyMember:list")
    @GetMapping("/list")
    public TableDataInfo<WxFamilyMemberVo> list(WxFamilyMemberBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxFamilyMemberService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询家庭成员列表
     */
    @SaCheckPermission("business:familyMember:list")
    @GetMapping("/listAll")
    public R<List<WxFamilyMemberVo>> listAll(WxFamilyMemberBo bo) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return R.ok(iWxFamilyMemberService.queryList(bo));
    }

    /**
     * 导出家庭成员列表
     */
    @SaCheckPermission("business:familyMember:export")
    @Log(title = "家庭成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxFamilyMemberBo bo, HttpServletResponse response) {
        List<WxFamilyMemberVo> list = iWxFamilyMemberService.queryList(bo);
        ExcelUtil.exportExcel(list, "家庭成员", WxFamilyMemberVo.class, response);
    }

    /**
     * 获取家庭成员详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:familyMember:query")
    @GetMapping("/{id}")
    public R<WxFamilyMemberVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWxFamilyMemberService.queryById(id));
    }

    /**
     * 新增家庭成员
     */
    @SaCheckPermission("business:familyMember:add")
    @Log(title = "家庭成员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxFamilyMemberBo bo) {
        return toAjax(iWxFamilyMemberService.insertByBo(bo));
    }

    /**
     * 修改家庭成员
     */
    @SaCheckPermission("business:familyMember:edit")
    @Log(title = "家庭成员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxFamilyMemberBo bo) {
        return toAjax(iWxFamilyMemberService.updateByBo(bo));
    }

    /**
     * 删除家庭成员
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:familyMember:remove")
    @Log(title = "家庭成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWxFamilyMemberService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
