package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.staff.ShopStaffAddDTO;
import com.czg.account.dto.staff.ShopStaffEditDTO;
import com.czg.account.dto.staff.ShopStaffRemoveDTO;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.ShopStaff;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopStaffService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺员工管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/shopStaff")
public class ShopStaffController {

    @Resource
    private ShopStaffService shopStaffService;
    @Resource
    private ShopInfoService shopInfoService;

    /**
     * 员工列表
     * 权限标识: shopStaff:list
     * @param name 账号名
     * @param code 员工编号
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "员工管理", value = "shopStaff:list", name = "员工列表")
    @GetMapping
    public CzgResult<Page<ShopStaff>> list(String name, String code) {
        return CzgResult.success(shopStaffService.get(name, code));
    }

    /**
     * 获取员工对应的权限id
     * 权限标识: shopStaff:permission
     * @return 权限id集合
     */
    @SaAdminCheckPermission(parentName = "员工管理", value = "shopStaff:permission", name =  "获取员工对应的权限id")
    @GetMapping("/permission")
    public CzgResult<List<Long>> permission(@RequestParam Long id) {
        return CzgResult.success(shopStaffService.permission(id));
    }

    /**
     * 获取当前登录员工信息
     * @return 员工信息
     */
    @GetMapping("/info")
    public CzgResult<ShopStaff> info() {
        if (StpKit.USER.isStaff()) {
            return CzgResult.success(shopStaffService.getOne(new QueryWrapper().eq(ShopStaff::getShopId, StpKit.USER.getShopId()).eq(ShopStaff::getId, StpKit.USER.getLoginIdAsLong())));
        }

        ShopInfo shopInfo = shopInfoService.getById(StpKit.USER.getShopId());
        return CzgResult.success(BeanUtil.copyProperties(shopInfo, ShopStaff.class));
    }

    /**
     * 员工详情
     * 权限标识: shopStaff:detail
     * @param id shopStaff id
     * @return 详细信息
     */
    @SaAdminCheckPermission(parentName = "员工管理", value = "shopStaff:detail", name = "员工详情")
    @GetMapping("/detail")
    public CzgResult<ShopStaff> detail(@RequestParam Long id) {
        return CzgResult.success(shopStaffService.detail(StpKit.USER.getShopId(), id));
    }

    /**
     * 员工添加
     * 权限标识: shopStaff:add
     * @param shopStaffAddDTO 添加信息
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "员工管理", value = "shopStaff:add", name = "员工添加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated ShopStaffAddDTO shopStaffAddDTO) {
        return CzgResult.success(shopStaffService.add(shopStaffAddDTO));
    }

    /**
     * 员工修改
     * 权限标识: shopStaff:edit
     * @param shopStaffEditDTO 添加信息
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "员工管理", value = "shopStaff:edit", name = "员工修改")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopStaffEditDTO shopStaffEditDTO) {
        return CzgResult.success(shopStaffService.edit(shopStaffEditDTO));
    }

    /**
     * 员工删除
     * 权限标识: shopStaff:del
     * @param shopStaffRemoveDTO 删除信息
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "员工管理", value = "shopStaff:del", name = "员工删除")
    @DeleteMapping
    public CzgResult<Boolean> delete(@RequestBody @Validated ShopStaffRemoveDTO shopStaffRemoveDTO) {
        return CzgResult.success(shopStaffService.delete(shopStaffRemoveDTO));
    }
}
