package com.ruoyi.web.v1.controller;

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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.web.v2.utils.PhoneNumberValidator;
import com.ruoyi.web.v3.model.request.JsStaffPageReq;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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 com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.v1.model.Staff;
import com.ruoyi.web.v1.service.IStaffService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员Controller
 * 
 * @author coede
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/app/staff")
public class StaffController extends BaseController
{
    @Autowired
    private IStaffService staffService;

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询人员列表
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:list')")
    @GetMapping("/list")
    public AjaxResult list(JsStaffPageReq staff)
    {
//        startPage();
        Staff staffEntity = new Staff();
        BeanUtils.copyProperties(staff, staffEntity);
        Page<Staff> list = staffService.selectStaffList(staff.toPage(), staffEntity);
//        return getDataTable(list);
        return success(list);
    }

    /**
     * 导出人员列表
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:export')")
    @Log(title = "人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Staff staff)
    {
        List<Staff> list = staffService.selectStaffAll(staff);
        ExcelUtil<Staff> util = new ExcelUtil<Staff>(Staff.class);
        util.exportExcel(response, list, "人员数据");
    }

    /**
     * 获取人员详细信息
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            Staff staff = staffService.selectStaffById(id);
            ajax.put(AjaxResult.DATA_TAG, staff);
            ajax.put("postIds", sysPostService.selectPostListByStaffId(id));
        }
        return ajax;
    }

    /**
     * 新增人员
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:add')")
    @Log(title = "人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Staff staff)
    {
        if (ObjectUtil.isNotEmpty(staff.getPhone())) {
            if (!PhoneNumberValidator.isValidContact(staff.getPhone())) {

                return error("联系方式格式不正确！");
            }
        }
        staff.setCreateBy(getUsername());
        return toAjax(staffService.insertStaff(staff));
    }

    /**
     * 修改人员
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:edit')")
    @Log(title = "人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Staff staff)
    {
        if (ObjectUtil.isNotEmpty(staff.getPhone())) {
            if (!PhoneNumberValidator.isValidContact(staff.getPhone())) {

                return error("联系方式格式不正确！");
            }
        }
        staff.setUpdateBy(getUsername());
        return toAjax(staffService.updateStaff(staff));
    }

    /**
     * 删除人员
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:remove')")
    @Log(title = "人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(staffService.deleteStaffByIds(ids));
    }

    /**
     * 查询全部人员列表
     */
    // @PreAuthorize("@ss.hasPermi('app:staff:optionselect')")
    @GetMapping("/optionselect")
    public AjaxResult optionselect(Staff staff)
    {
        List<Staff> list = staffService.selectStaffAll(staff);
        return success(list);
    }
}
