package com.ruoyi.pay.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PayEmployeeInfoVo;
import com.ruoyi.pay.domain.bo.PayEmployeeInfoBo;
import com.ruoyi.pay.service.IPayEmployeeInfoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工信息
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/employeeInfo")
public class PayEmployeeInfoController extends BaseController {

    private final IPayEmployeeInfoService iPayEmployeeInfoService;

    /**
     * 查询员工信息列表
     */
    @SaCheckPermission("pay:employeeInfo:list")
    @GetMapping("/list")
    public TableDataInfo<PayEmployeeInfoVo> list(PayEmployeeInfoBo bo, PageQuery pageQuery) {
        return iPayEmployeeInfoService.queryPageList(bo, pageQuery);
    }
    /**
     * 获取内外贸人员列表
     */
    @SaCheckPermission("pay:employeeInfo:list")
    @GetMapping("/salesman")
    public List<PayEmployeeInfoVo> getSalesmanList(PayEmployeeInfoBo bo) {
        return iPayEmployeeInfoService.getSalesmanList(bo);
    }

    /**
     * 导出员工信息列表
     */
    @SaCheckPermission("pay:employeeInfo:export")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayEmployeeInfoBo bo, HttpServletResponse response) {
        List<PayEmployeeInfoVo> list = iPayEmployeeInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工信息", PayEmployeeInfoVo.class, response);
    }

    /**
     * 获取员工信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:employeeInfo:query")
    @GetMapping("/{id}")
    public R<PayEmployeeInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPayEmployeeInfoService.queryById(id));
    }

    /**
     * 新增员工信息
     */
    @SaCheckPermission("pay:employeeInfo:add")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayEmployeeInfoBo bo) {
        PayEmployeeInfoBo em = new PayEmployeeInfoBo();
        em.setPhone(bo.getPhone());
        List<PayEmployeeInfoVo> payEmployeeInfoVos = iPayEmployeeInfoService.queryList(em);
        if (payEmployeeInfoVos!=null&&payEmployeeInfoVos.size()>0) { //  表示重手机号
            throw new ServiceException("该手机号已注册");
        }
        em.setPhone(null);
        em.setName(bo.getName());
        List<PayEmployeeInfoVo> payEmployeeInfoVos1 = iPayEmployeeInfoService.queryList(em);
        if (payEmployeeInfoVos1!=null&&payEmployeeInfoVos1.size()>0) { //  表示重名
            throw new ServiceException("系统中存在同名员工");
        }
        return toAjax(iPayEmployeeInfoService.insertByBo(bo));
    }

    /**
     * 修改员工信息
     */
    @SaCheckPermission("pay:employeeInfo:edit")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayEmployeeInfoBo bo) {
        return toAjax(iPayEmployeeInfoService.updateByBo(bo));
    }

    /**
     * 批量修改员工每日应出勤小时数
     */
    @SaCheckPermission("pay:empinfo:edithour")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edithour")
    public R<Void> edit(@RequestParam("oldHour") String oldHour, @RequestParam("newHour") String newHour,@RequestParam("tenantId") Long tenantId)
    {
        return toAjax(iPayEmployeeInfoService.updateSgrEmployeeHour(oldHour,newHour,tenantId));
    }

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