package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.dto.person.PersonInfoAddRequest;
import com.ruoyi.web.domain.dto.person.PersonInfoEditRequest;
import com.ruoyi.web.domain.dto.person.PersonInfoListQueryRequest;
import com.ruoyi.web.domain.dto.person.PersonInfoQueryRequest;
import com.ruoyi.web.domain.vo.PersonInfoVO;
import com.ruoyi.web.service.PersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@Api(tags ="人员信息管理")
@RestController
@RequestMapping("/system/personinfo")
public class PersonInfoController{

    @Autowired
    private PersonInfoService personInfoService;

    /**
     * 添加人员
     *
     * @param personInfoAddRequest
     * @return
     */
    @ApiOperation("添加人员")
    @PostMapping("/add")
    public AjaxResult addPersonInfo(@RequestBody PersonInfoAddRequest personInfoAddRequest) {
        if (personInfoAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = personInfoService.addPersonInfo(personInfoAddRequest);
        return success(id);
    }

    /**
     * 删除人员
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除人员")
    @PostMapping("/delete")
    public AjaxResult deletePersonInfo(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = personInfoService.deletePersonInfo(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }


    /**
     * 编辑人员
     *
     * @param personInfoEditRequest
     * @return
     */
    @ApiOperation("编辑人员")
    @PostMapping("/edit")
    public AjaxResult editPersonInfo(@RequestBody PersonInfoEditRequest personInfoEditRequest) {
        if (personInfoEditRequest == null) {
            return error("请求参数为空");
        }
        personInfoService.editPersonInfo(personInfoEditRequest);
        return AjaxResult.success();
    }

    /**
     * 根据id获取人员
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取人员")
    @GetMapping("/get")
    public AjaxResult getPersonInfoById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        PersonInfoVO personInfoVO = personInfoService.getPersonInfoById(id);
        return success(personInfoVO);
    }

    /**
     * 根据id获取人员
     *
     * @param personInfoListQueryRequest
     * @return
     */
    @ApiOperation("获取人员列表")
    @PostMapping("/list")
    public AjaxResult getPersonInfoVOList(@RequestBody PersonInfoListQueryRequest personInfoListQueryRequest) {
        if (ObjectUtil.isEmpty(personInfoListQueryRequest)) {
            return error("请求参数为空");
        }
        List<PersonInfoVO> personInfoVOList = personInfoService.getPersonInfoVOList(personInfoListQueryRequest);
        return success(personInfoVOList);
    }

    /**
     * 分页获取人员列表  mybatis-plus
     *
     * @param personInfoQueryRequest
     * @return
     */
    @ApiOperation("分页获取人员列表")
    @PostMapping("/list/page")
    public AjaxResult listPersonInfoByPage(@RequestBody PersonInfoQueryRequest personInfoQueryRequest) {
        if (personInfoQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<PersonInfoVO> listPersonInfoByPage = personInfoService.getListPersonInfoByPage(personInfoQueryRequest);
        return success(listPersonInfoByPage);
    }

    @GetMapping("/encrypt")
    public String encryptIdCard(String idCard) {
        return personInfoService.encryptIdCard(idCard);
    }
}
