package com.ruoyi.web.bst;

import com.ruoyi.bst.store.service.StoreValidator;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
import com.ruoyi.bst.suit.domain.Suit;
import com.ruoyi.bst.suit.domain.SuitQuery;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.service.SuitConverter;
import com.ruoyi.bst.suit.service.SuitService;
import com.ruoyi.bst.suit.service.SuitValidator;
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 com.ruoyi.system.user.service.UserValidator;
import org.checkerframework.checker.units.qual.A;
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-25
 */
@RestController
@RequestMapping("/bst/suit")
public class SuitController extends BaseController
{
    @Autowired
    private SuitService suitService;
    @Autowired
    private UserValidator userValidator;
    @Autowired
    private SuitConverter suitConverter;
    @Autowired
    private SuitValidator suitValidator;
    @Autowired
    private StoreValidator storeValidator;

    /**
     * 查询套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuitQuery query)
    {
        startPage();
        startOrderBy();
        query.setDeleted(false);
        query.setScope(true);
        query.addStorePermission(StoreStaffPermission.SUIT_VIEW.getCode());
        List<SuitVO> list = suitService.selectSuitList(query);
        return getDataTable(list);
    }

    /**
     * 导出套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:export')")
    @Log(title = "套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuitQuery query)
    {
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        ExcelUtil<SuitVO> util = new ExcelUtil<SuitVO>(SuitVO.class);
        util.exportExcel(response, list, "套餐数据");
    }

    /**
     * 获取套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:query')")
    @GetMapping(value = "/{suitId}")
    public AjaxResult getInfo(@PathVariable("suitId") Long suitId)
    {
        return success(suitService.selectSuitById(suitId,true));
    }

    /**
     * 新增套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:add')")
    @Log(title = "套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Suit suit)
    {
        // 若不能操作他人，则设置为当前用户
        if (!userValidator.canView(suit.getUserId())) {
            suit.setUserId(getUserId());
        }
        suit.setUserId(getUserId());
        suitConverter.toPoByCreate(suit);
        return toAjax(suitService.insertSuit(suit));
    }

    /**
     * 修改套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:edit')")
    @Log(title = "套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Suit suit)
    {
        if (!storeValidator.canEdit(suit.getStoreId())){
            return AjaxResult.error("您没有权限修改ID为" + suit.getStoreId() + "的商铺信息");
        }
        // 若不能操作他人，则不修改用户ID
        if (!userValidator.canView(suit.getUserId())) {
            suit.setUserId(null);
        }
        suitConverter.toPoByUpdate(suit);
        return toAjax(suitService.updateSuit(suit));
    }

    /**
     * 删除套餐
     */
//    @PreAuthorize("@ss.hasPermi('bst:suit:remove')")
//    @Log(title = "套餐", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable List<Long> ids)
//    {
//        if (!storeValidator.canDeleteAll(ids)) {
//            return AjaxResult.error("您没有权限删除ID为" + ids + "的店铺信息");
//        }
//        return toAjax(suitService.deleteSuitBySuitIds(ids));
//    }

    /**
     * 逻辑删除套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:remove')")
    @Log(title = "套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult logicDel(@PathVariable List<Long> ids)
    {
        if (!storeValidator.canDeleteAll(ids)) {
            return AjaxResult.error("您没有权限删除ID为" + ids + "的店铺信息");
        }
        return toAjax(suitService.logicDel(ids));
    }
}
