package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreStaffConfig;
import shop.alien.store.service.StoreStaffConfigService;

import java.io.IOException;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-11
 * @Description: 员工管理
 */
@Api(tags = {"员工管理"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/storeStaffConfig")
@RequiredArgsConstructor
public class StoreStaffConfigController {

    private final StoreStaffConfigService storeStaffConfigService;

    @ApiOperation("员工列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getStaffConfigList")
    public R<IPage<StoreStaffConfig>> getStaffConfigList(@RequestParam(value = "page", defaultValue = "1") int page,
                                                         @RequestParam(value = "size", defaultValue = "10") int size,
                                                         @RequestParam(value = "status", required = false) String status) {
        log.info("StoreStaffConfigController.getStaffConfigList?status={}", status);
        return R.data(storeStaffConfigService.getStaffConfigList(page, size, status));
    }

    @ApiOperation("新增修改员工")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addOrUpdateStaffConfig")
    public R<String> addOrUpdateStaffConfig(@RequestBody StoreStaffConfig storeStaffConfig) throws IOException, InterruptedException {
        log.info("StoreStaffConfigController.addOrUpdateStaffConfig?multipartRequest={}, storeStaffConfig={}", storeStaffConfig);
        int num = storeStaffConfigService.addOrUpdateStaffConfig(storeStaffConfig);
        if (0 == num) {
            return R.fail("新增修改员工失败");
        } else {
            return R.data("新增修改员工成功");
        }
    }

    @ApiOperation("员工审核")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "comment", value = "审批意见", dataType = "String", paramType = "query")})
    @GetMapping("/audit")
    public R<Integer> audit(Integer id, Integer status, String rejectionReason) {
        log.info("StoreStaffConfigController.audit?id={},status={},rejectionReason={}", id, status, rejectionReason);
        return R.data(storeStaffConfigService.audit(id, status, rejectionReason));
    }

    @ApiOperation("员工详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getStaffConfigDeatail")
    public R<StoreStaffConfig> getStaffConfigDeatail(Integer id) {
        log.info("StoreStaffConfigController.getStaffConfigDeatail?id={}", id);
        return R.data(storeStaffConfigService.getStaffConfigDeatail(id));
    }

    @ApiOperation("员工导出")
    @ApiOperationSupport(order = 11)
    @GetMapping("/staffConfigExport")
    public R staffConfigExport(@RequestParam(required = false) String status) throws IOException {
        log.info("PlatformStoreCouponController.staffConfigExport");
        String s = storeStaffConfigService.staffConfigExport(status);
        return R.data(s);
    }

    @ApiOperation("删除员工")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/deleteStaffConfig")
    public R<Integer> deleteStaffConfig(Integer id) {
        log.info("StoreStaffConfigController.deleteStaffConfig?id={}", id);
        return R.data(storeStaffConfigService.deleteStaffConfig(id));
    }

    @ApiOperation("置顶员工")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "topStatus", value = "置顶状态 0-取消置顶, 1-置顶", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/setTopStatus")
    public R<Integer> setTopStatus(Integer id, Integer topStatus) {
        log.info("StoreStaffConfigController.setTopStatus?id={},topStatus={}", id, topStatus);
        return R.data(storeStaffConfigService.setTopStatus(id, topStatus));
    }
    /**
     * 员工列表查询接口（用户端）
     *
     * @param page    分页页数
     * @param size    分页条数
     * @param storeId 店铺ID
     * @param status  员工状态
     * @return 员工列表
     */
    @ApiOperation("员工列表查询（用户端）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "员工状态(0-待审核 1-审核通过 2-审核拒绝)", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/queryStaffList")
    public R<IPage<StoreStaffConfig>> queryStaffList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "100") Integer size,
            @RequestParam(value = "storeId", required = true) Integer storeId,
            @RequestParam(value = "status", required = false) String status) {
        log.info("StoreStaffConfigController.queryStaffList?page={}&size={}&storeId={}&status={}", page, size, storeId, status);
        IPage<StoreStaffConfig> result = storeStaffConfigService.queryStaffList(page, size, storeId, status);
        return R.data(result);
    }

    /**
     * 员工详情查询接口（用户端）
     *
     * @param id 员工主键id
     * @return 员工详情
     */
    @ApiOperation("员工详情查询（用户端）")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工主键id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryStaffDetail")
    public R<StoreStaffConfig> queryStaffDetail(@RequestParam(value = "id", required = true) Integer id) {
        log.info("StoreStaffConfigController.queryStaffDetail?id={}", id);
        StoreStaffConfig result = storeStaffConfigService.queryStaffDetail(id);
        if (result == null) {
            return R.fail("员工不存在");
        }
        return R.data(result);
    }
}
