package shop.alien.store.controller;

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.StorePersonnel;
import shop.alien.entity.store.vo.StorePersonnelVo;
import shop.alien.store.service.StorePersonnelService;

import java.util.List;

/**
 * 店铺人员Controller
 *
 * @author system
 * @since 2025-01-15
 */
@Slf4j
@Api(tags = {"店铺人员"})
@ApiSort(5)
@CrossOrigin
@RestController
@RequestMapping("/personnel")
@RequiredArgsConstructor
public class StorePersonnelController {

    private final StorePersonnelService storePersonnelService;

    @ApiOperation("获取店铺人员列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getPersonnelList")
    public R<List<StorePersonnelVo>> getPersonnelList(
            @RequestParam(value = "storeId", required = true) Integer storeId) {
        log.info("StorePersonnelController.getPersonnelList?storeId={}", storeId);
        return R.data(storePersonnelService.getStorePersonnelList(storeId));
    }

    @ApiOperation("获取人员详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "人员id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getPersonnelInfo")
    public R<StorePersonnelVo> getPersonnelInfo(
            @RequestParam(value = "id", required = true) Integer id) {
        log.info("StorePersonnelController.getPersonnelInfo?id={}", id);
        return R.data(storePersonnelService.getPersonnelInfo(id));
    }

    @ApiOperation("新增或修改店铺人员")
    @ApiOperationSupport(order = 3)
    @PostMapping("/saveOrUpdate")
    public R<String> saveOrUpdate(@RequestBody StorePersonnelVo storePersonnelVo) {
        log.info("StorePersonnelController.saveOrUpdate?storePersonnelVo={}", storePersonnelVo);
        return storePersonnelService.saveOrUpdatePersonnel(storePersonnelVo);
    }

    @ApiOperation("删除店铺人员")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "人员id列表", dataType = "List<Integer>", paramType = "query", required = true)
    })
    @GetMapping("/delete")
    public R<String> delete(@RequestParam(value = "ids") List<Integer> ids) {
        log.info("StorePersonnelController.delete?ids={}", ids);
        return storePersonnelService.deletePersonnel(ids);
    }

    @ApiOperation("保存人员排序")
    @ApiOperationSupport(order = 5)
    @PostMapping("/savePersonnelSort")
    public R<Boolean> savePersonnelSort(@RequestBody List<StorePersonnel> storePersonnelList) {
        log.info("StorePersonnelController.savePersonnelSort?storePersonnelList={}", storePersonnelList);
        Boolean flag = storePersonnelService.savePersonnelSort(storePersonnelList);
        if (flag) {
            return R.success("已更新排序");
        } else {
            return R.fail("排序失败");
        }
    }
}

