package com.ruoyi.web.controller.clock;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.domain.bo.ShopBo;
import com.ruoyi.clock.domain.vo.ShopVo;
import com.ruoyi.clock.service.IShopService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 门店信息Controller
 *
 * @author pengc
 * @date 2026-03-04
 */
@Validated
@Api(value = "门店信息控制器", tags = {"门店信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/clock/shop")
public class ShopController extends BaseController {

    private final IShopService iShopService;

    /**
     * 分页查询门店信息列表
     */
    @ApiOperation("查询门店信息列表")
    @SaCheckPermission("clock:shop:list")
    @GetMapping("/page")
    public TableDataInfo<ShopVo> page(@Validated(QueryGroup.class) ShopBo bo, PageQuery pageQuery) {
        TableDataInfo<ShopVo> shopVoTableDataInfo = iShopService.queryPageList(bo, pageQuery);
        if (CollUtil.isNotEmpty(shopVoTableDataInfo.getRows())){
            shopVoTableDataInfo.getRows().forEach(item -> {
                item.loadAgentVo();
            });
        }
        return shopVoTableDataInfo;
    }

    /**
     * 查询门店信息列表
     */
    @ApiOperation("查询门店信息列表")
    @GetMapping("/list")
    public R<List<ShopVo>> list(@Validated(QueryGroup.class) ShopBo bo) {
        return R.ok(iShopService.queryList(bo));
    }


    /**
     * 获取门店信息详细信息
     */
    @ApiOperation("获取门店信息详细信息")
    @SaCheckPermission("clock:shop:query")
    @GetMapping("/info/{shopId}")
    public R<ShopVo> getInfo(@ApiParam("主键")  @NotNull(message = "主键不能为空")
                              @PathVariable("shopId") Long shopId) {
        ShopVo shopVo = iShopService.queryById(shopId);
        if(ObjectUtil.isNotNull(shopVo)){
            shopVo.loadAgentVo();
        }
        return R.ok(shopVo);
    }

    /**
     * 新增门店信息
     */
    @ApiOperation("新增门店信息")
    @SaCheckPermission("clock:shop:add")
    @Log(title = "门店信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ShopBo bo) {
        return toAjax(iShopService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改门店信息
     */
    @ApiOperation("修改门店信息")
    @SaCheckPermission("clock:shop:edit")
    @Log(title = "门店信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ShopBo bo) {
        return toAjax(iShopService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除门店信息
     */
    @ApiOperation("删除门店信息")
    @SaCheckPermission("clock:shop:remove")
    @Log(title = "门店信息", businessType = BusinessType.DELETE)
    @PostMapping("/del/{shopIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] shopIds) {
        return toAjax(iShopService.deleteWithValidByIds(Arrays.asList(shopIds), true) ? 1 : 0);
    }

    /**
     * 导出门店信息列表
     */
    @ApiOperation("导出门店信息列表")
    @SaCheckPermission("clock:shop:export")
    @Log(title = "门店信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ShopBo bo, HttpServletResponse response) {
        List<ShopVo> list = iShopService.queryList(bo);
        if (CollUtil.isNotEmpty(list)){
            list.forEach(item -> {
                item.loadAgentVo();
            });
        }
        ExcelUtil.exportExcel(list, "门店信息", ShopVo.class, response);
    }

}
