package com.ruoyi.web.bst;

import com.ruoyi.bst.part.domain.Part;
import com.ruoyi.bst.part.domain.PartQuery;
import com.ruoyi.bst.part.domain.PartVO;
import com.ruoyi.bst.part.service.PartService;
import com.ruoyi.bst.part.service.PartValidator;
import com.ruoyi.bst.store.service.StoreValidator;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
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.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.user.service.UserValidator;
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-04-18
 */
@RestController
@RequestMapping("/bst/part")
public class PartController extends BaseController
{
    @Autowired
    private PartService partService;
    @Autowired
    private PartValidator partValidator;
    @Autowired
    private StoreValidator storeValidator;


    /**
     * 查询分区列表
     */
    @PreAuthorize("@ss.hasPermi('bst:part:list')")
    @GetMapping("/list")
    public AjaxResult list(PartQuery query)
    {
        query.setScope(true);
        query.addStorePermission(StoreStaffPermission.PART_VIEW.getCode());
        List<PartVO> list = partService.selectPartList(query);
        return success(list);
    }

    /**
     * 导出分区列表
     */
    @PreAuthorize("@ss.hasPermi('bst:part:export')")
    @Log(title = "分区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartQuery query)
    {
        query.setScope(true);
        List<PartVO> list = partService.selectPartList(query);
        ExcelUtil<PartVO> util = new ExcelUtil<PartVO>(PartVO.class);
        util.exportExcel(response, list, "分区数据");
    }

    /**
     * 获取分区详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:part:query')")
    @GetMapping(value = "/{partId}")
    public AjaxResult getInfo(@PathVariable("partId") Long partId)
    {
        return success(partService.selectPartById(partId,true));
    }

    /**
     * 新增分区
     */
    @PreAuthorize("@ss.hasPermi('bst:part:add')")
    @Log(title = "分区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Part part)
    {
        // 判断能否选择店铺
        if (!storeValidator.canCheckForStore(part.getStoreId())) {
            return AjaxResult.error("您无权选择ID为" + part.getStoreId() + "的店铺");
        }
        return toAjax(partService.insertPart(part));
    }

    /**
     * 修改分区
     */
    @PreAuthorize("@ss.hasPermi('bst:part:edit')")
    @Log(title = "分区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody  @Validated(ValidGroup.Update.class) Part part)
    {
        if (!partValidator.canEdit(part.getPartId())) {
            return AjaxResult.error("您没有权限修改ID为" + part.getPartId() + "的分区");
        }
        return toAjax(partService.updatePart(part));
    }

    /**
     * 删除分区
     */
    @PreAuthorize("@ss.hasPermi('bst:part:remove')")
    @Log(title = "分区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        if (!partValidator.canDeleteAll(ids)) {
            return AjaxResult.error("您没有权限删除ID为" + ids + "的分区");
        }
        return toAjax(partService.deletePartByPartIds(ids));
    }
}
