package com.ruoyi.web.controller.clock;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.domain.bo.EmployeeBo;
import com.ruoyi.clock.domain.bo.EmployeeImportBo;
import com.ruoyi.clock.domain.bo.EmployeeStatusBo;
import com.ruoyi.clock.domain.vo.EmployeeVo;
import com.ruoyi.clock.listener.EmployeeImportListener;
import com.ruoyi.clock.service.IEmployeeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.apiencrypt.annotation.ApiDecrypt;
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.page.TableDataInfo;
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.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.listener.SysUserImportListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 店员信息Controller
 *
 * @author pengc
 * @date 2026-03-04
 */
@Validated
@Api(value = "店员信息控制器", tags = {"店员信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/clock/employee")
public class EmployeeController extends BaseController {

    private final IEmployeeService iEmployeeService;

    /**
     * 分页查询店员信息列表
     */
    @ApiOperation("查询店员信息列表")
    @SaCheckPermission("clock:employee:list")
    @GetMapping("/page")
    public TableDataInfo<EmployeeVo> page(@Validated(QueryGroup.class) EmployeeBo bo, PageQuery pageQuery) {
        TableDataInfo<EmployeeVo> employeeVoTableDataInfo = iEmployeeService.queryPageList(bo, pageQuery);
        if (CollUtil.isNotEmpty(employeeVoTableDataInfo.getRows())){
            employeeVoTableDataInfo.getRows().forEach(item -> {
                item.loadAgentVo();
                item.loadShopVo();
            });
        }
        return employeeVoTableDataInfo;
    }

    /**
     * 获取店员信息详细信息
     */
    @ApiOperation("获取店员信息详细信息")
    @SaCheckPermission("clock:employee:query")
    @GetMapping("/info/{employeeId}")
    public R<EmployeeVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("employeeId") Long employeeId) {
        EmployeeVo employeeVo = iEmployeeService.queryById(employeeId);
        if(ObjectUtil.isNotNull(employeeVo)){
            employeeVo.loadAgentVo();
            employeeVo.loadShopVo();
        }
        return R.ok(employeeVo);
    }

    /**
     * 新增店员信息
     */
    @ApiOperation("新增店员信息")
    @SaCheckPermission("clock:employee:add")
    @Log(title = "店员信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmployeeBo bo) {
        return toAjax(iEmployeeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改店员信息
     */
    @ApiOperation("修改店员信息")
    @SaCheckPermission("clock:employee:edit")
    @Log(title = "店员信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeeBo bo) {
        return toAjax(iEmployeeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除店员信息
     */
    @ApiOperation("删除店员信息")
    @SaCheckPermission("clock:employee:remove")
    @Log(title = "店员信息", businessType = BusinessType.DELETE)
    @PostMapping("/del/{employeeIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] employeeIds) {
        return toAjax(iEmployeeService.deleteWithValidByIds(Arrays.asList(employeeIds), true) ? 1 : 0);
    }

    /**
     * 重置密码
     */
    @ApiDecrypt
    @ApiOperation("重置密码")
    @GetMapping("/resetPwd/{employeeId}")
    public R<Void> resetPwd(@ApiParam("主键")
                                @NotNull(message = "主键不能为空")
                                @PathVariable("employeeId") Long employeeId) {
        return toAjax(iEmployeeService.resetPwd(employeeId));
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @Log(title = "店员状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody EmployeeStatusBo bo) {
        return toAjax(iEmployeeService.updateStatus(bo));
    }


    @Log(title = "店员管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<EmployeeImportBo> result = ExcelUtil.importExcel(file.getInputStream(), EmployeeImportBo.class, new EmployeeImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @ApiOperation("下载导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", EmployeeImportBo.class, response);
    }

    /**
     * 导出店员信息列表
     */
    @ApiOperation("导出店员信息列表")
    @SaCheckPermission("clock:employee:export")
    @Log(title = "店员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated EmployeeBo bo, HttpServletResponse response) {
        List<EmployeeVo> list = iEmployeeService.queryList(bo);
        list.forEach(item -> {
            item.loadAgentVo();
            item.loadShopVo();
        });
        ExcelUtil.exportExcel(list, "店员信息", EmployeeVo.class, response);
    }
}
