package com.ruoyi.xq.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.enums.BusinessType;
import com.ruoyi.xq.domain.ActivityShop;
import com.ruoyi.xq.dto.admin.activity.ActivityShopAdminUpdateReq;
import com.ruoyi.xq.dto.admin.activity.ActivityShopAdminVo;
import com.ruoyi.xq.enums.activity.ActivityShopStatusEnum;
import com.ruoyi.xq.service.ActivityShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 相亲会
 *
 * @author 77
 * @date 2024-05-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xq/activityShop")
public class ActivityShopController extends BaseController {

    private final ActivityShopService activityShopService;

    /**
     * 查询相亲会列表
     */
    @SaCheckPermission("xq:activityShop:list")
    @GetMapping("/list")
    public TableDataInfo<ActivityShopAdminVo> list(ActivityShop bo, PageQuery pageQuery) {
        Page<ActivityShop> page = activityShopService.page(pageQuery.build(), Wrappers.lambdaQuery(bo));
        TableDataInfo<ActivityShopAdminVo> build = TableDataInfo.build(page, ActivityShopAdminVo::new);
        List<ActivityShopAdminVo> rows = build.getRows();
        for (ActivityShopAdminVo row : rows) {
            row.setOpenStatus(ActivityShopStatusEnum.getEnumByTime(row.getStartTime(), row.getEndTime()).getCode());
        }
        return build;
    }


    @SaCheckPermission("xq:activityShop:query")
    @GetMapping("/{id}")
    public R<ActivityShop> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(activityShopService.getById(id));
    }

    @SaCheckPermission("xq:activityShop:add")
    @Log(title = "相亲会", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ActivityShop bo) {
        activityShopService.saveData(bo);
        return R.ok();
    }

    @SaCheckPermission("xq:activityShop:edit")
    @Log(title = "相亲会", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ActivityShopAdminUpdateReq bo) {
        activityShopService.updateData(bo);
        return R.ok();
    }

    @SaCheckPermission("xq:activityShop:edit")
    @Log(title = "相亲会人数调整", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/user")
    public R<Void> editUser(@Validated(EditGroup.class) @RequestBody ActivityShop bo) {
        activityShopService.updateDataUser(bo);
        return R.ok();
    }

    /**
     * 删除相亲会
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xq:activityShop:remove")
    @Log(title = "相亲会", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        activityShopService.removeDataByIds(ids);
        return R.ok();
    }
}
