package com.ruoyi.web.bst;

import com.ruoyi.bst.store.service.StoreValidator;
import com.ruoyi.bst.storeStaff.domain.StoreStaff;
import com.ruoyi.bst.storeStaff.domain.StoreStaffQuery;
import com.ruoyi.bst.storeStaff.domain.StoreStaffVO;
import com.ruoyi.bst.storeStaff.service.StoreStaffConverter;
import com.ruoyi.bst.storeStaff.service.StoreStaffService;
import com.ruoyi.bst.storeStaff.service.StoreStaffValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工运维Controller
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@RestController
@RequestMapping("/bst/storeStaff")
public class StoreStaffController extends BaseController
{
    @Autowired
    private StoreStaffService storeStaffService;
    @Autowired
    private StoreValidator storeValidator;
    @Autowired
    private StoreStaffConverter storeStaffConverter;
    @Autowired
    private StoreStaffValidator storeStaffValidator;

    /**
     * 查询员工运维列表
     */
    @PreAuthorize("@ss.hasPermi('bst:storeStaff:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreStaffQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<StoreStaffVO> list = storeStaffService.selectStoreStaffList(query);
        return getDataTable(list);
    }

    /**
     * 导出员工运维列表
     */
    @PreAuthorize("@ss.hasPermi('bst:storeStaff:export')")
    @Log(title = "员工运维", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StoreStaffQuery query)
    {
        query.setScope(true);
        List<StoreStaffVO> list = storeStaffService.selectStoreStaffList(query);
        ExcelUtil<StoreStaffVO> util = new ExcelUtil<StoreStaffVO>(StoreStaffVO.class);
        util.exportExcel(response, list, "员工运维数据");
    }

    /**
     * 获取员工运维详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:storeStaff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(storeStaffService.selectStoreStaffById(id,true));
    }

    /**
     * 新增员工运维
     */
    @PreAuthorize("@ss.hasPermi('bst:storeStaff:add')")
    @Log(title = "员工运维", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) StoreStaffVO storeStaff)
    {
        if (!storeValidator.canCheckForStore(storeStaff.getStoreId())){
            return AjaxResult.error("您无权选择ID为" + storeStaff.getStoreId() + "的店铺");
        }
        storeStaff.setCreateId(getUserId());
        StoreStaff po =storeStaffConverter.toPoByCreate(storeStaff);
        return toAjax(storeStaffService.insertStoreStaff(po));
    }

    /**
     * 修改员工运维
     */
    @PreAuthorize("@ss.hasPermi('bst:storeStaff:edit')")
    @Log(title = "员工运维", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) StoreStaffVO storeStaff)
    {
        // 判断能否操作当前运维数据
        if (!storeStaffValidator.canEdit(storeStaff.getId())) {
            return AjaxResult.error("您无权修改ID为" + storeStaff.getId() + "的数据");
        }
        if (!storeValidator.canCheckForStore(storeStaff.getStoreId())){
            return AjaxResult.error("您无权选择ID为" + storeStaff.getStoreId() + "的店铺");
        }
        StoreStaff po =storeStaffConverter.toPoByUpdate(storeStaff);
        if (storeStaffService.selectStoreStaffById(storeStaff.getId()) == null) {
            return AjaxResult.error("id为%s的用户不存在", storeStaff.getId());
        };
        return toAjax(storeStaffService.updateStoreStaff(po));
    }

    /**
     * 删除员工运维
     */
    @PreAuthorize("@ss.hasPermi('bst:storeStaff:remove')")
    @Log(title = "员工运维", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        // 判断能否删除当前加盟数据
        if (!storeStaffValidator.canDeleteAll(ids)) {
            return AjaxResult.error("您无权删除ID为" + ids + "的数据");
        }
        return toAjax(storeStaffService.deleteStoreStaffByIds(ids.toArray(new Long[0])));
    }
}
