package com.srs.web.controller.comprehensive;

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

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.comprehensive.domain.CphFamilyMember;
import com.srs.comprehensive.service.ICphFamilyMemberService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

/**
 * 家庭成员Controller
 *
 * @author srs
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/comprehensive/member")
@Api(value = "家庭成员管理", tags = "家庭成员管理")
public class CphFamilyMemberController extends BaseController {
    @Autowired
    private ICphFamilyMemberService cphFamilyMemberService;

    @PostMapping("/editOwnFamily")
    @ApiOperation("修改学生自己的家庭成员")
    public AjaxResult editOwnFamily(@RequestBody CphFamilyMember param){
        if(param.stuNo == null || "".equals(param.stuNo)){
            param.stuNo = getUsername();
        }

        return cphFamilyMemberService.editOwnFamily(param);
    }

    @PostMapping("/deleteOwnFamily/{id}")
    @ApiOperation("删除学生自己的家庭成员")
    public AjaxResult deleteOwnFamily(@PathVariable("id") Long id, @Param("stuNo") String stuNo){
        CphFamilyMember param = new CphFamilyMember();
        param.setId(id);
        if(stuNo  == null || "".equals(stuNo)){
            param.setStuNo(getUsername());
        }

        return cphFamilyMemberService.deleteOwnFamily(param);
    }

    @GetMapping("/deleteFamilyByFdy")
    @ApiOperation("删除学生自己的家庭成员")
    public AjaxResult deleteFamilyByFdy(@Param("id") Long id, @Param("stuNo") String stuNo){
        CphFamilyMember param = new CphFamilyMember();
        param.setId(id);
        if(stuNo  == null || "".equals(stuNo)){
            param.setStuNo(getUsername());
        }

        return cphFamilyMemberService.deleteMemberByFdy(param);
    }


    @PostMapping("/addOwnFamily")
    @ApiOperation("添加学生自己的家庭成员")
    public AjaxResult addOwnFamily(@RequestBody CphFamilyMember param){
        if(param.stuNo == null || "".equals(param.stuNo)){
            param.setStuNo(getUsername());
        }

        return toAjax(cphFamilyMemberService.insertCphFamilyMember(param));
    }


    @GetMapping("/getOwnFamily")
    @ApiOperation("查询学生自己的家庭成员列表")
    public AjaxResult getOwnFamily(@Param("stuNo") String stuNo){
        List<CphFamilyMember> list = new ArrayList<>();
        if("".equals(stuNo) || stuNo == null){
            list = cphFamilyMemberService.getStuFamily(getUsername());
        }else{
            list = cphFamilyMemberService.getStuFamily(stuNo);
        }

        return AjaxResult.success(list);
    }



    /**
     * 查询家庭成员列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:member:list')")
    @GetMapping("/list")
    @ApiOperation("查询家庭成员列表")
    public TableDataInfo list(CphFamilyMember cphFamilyMember) {
        startPage();
        List<CphFamilyMember> list = cphFamilyMemberService.selectCphFamilyMemberList(cphFamilyMember);
        return getDataTable(list);
    }

    /**
     * 导出家庭成员列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:member:export')")
    @Log(title = "家庭成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出家庭成员列表")
    public void export(HttpServletResponse response, CphFamilyMember cphFamilyMember) {
        List<CphFamilyMember> list = cphFamilyMemberService.selectCphFamilyMemberList(cphFamilyMember);
        ExcelUtil<CphFamilyMember> util = new ExcelUtil<CphFamilyMember>(CphFamilyMember.class);
        util.exportExcel(response, list, "家庭成员数据");
    }

    /**
     * 获取家庭成员详细信息
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:member:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取家庭成员详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cphFamilyMemberService.selectCphFamilyMemberById(id));
    }

    /**
     * 新增家庭成员
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:member:add')")
    @Log(title = "家庭成员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增家庭成员")
    public AjaxResult add(@RequestBody CphFamilyMember cphFamilyMember) {
        return toAjax(cphFamilyMemberService.insertCphFamilyMember(cphFamilyMember));
    }

    /**
     * 修改家庭成员
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:member:edit')")
    @Log(title = "家庭成员", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改家庭成员")
    public AjaxResult edit(@RequestBody CphFamilyMember cphFamilyMember) {
        return toAjax(cphFamilyMemberService.updateCphFamilyMember(cphFamilyMember));
    }

    /**
     * 删除家庭成员
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:member:remove')")
    @Log(title = "家庭成员", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除家庭成员")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cphFamilyMemberService.deleteCphFamilyMemberByIds(ids));
    }
}
