package com.starry.admin.modules.clerk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.modules.clerk.module.entity.PlayClerkArticleInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkArticleQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkArticleReturnVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkArticleReviewStateEditVo;
import com.starry.admin.modules.clerk.service.IPlayClerkArticleInfoService;
import com.starry.admin.modules.clerk.service.IPlayCustomArticleInfoService;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 店员动态信息Controller
 *
 * @author admin
 * @since 2024-05-04
 */
@Api(tags = "店员动态管理", description = "店员动态信息管理相关接口，包括查询、审核和删除等操作")
@RestController
@RequestMapping("/clerk/article")
public class PlayClerkArticleInfoController {
    @Resource
    private IPlayClerkArticleInfoService playClerkArticleInfoService;
    @Resource
    private IPlayCustomArticleInfoService playCustomArticleInfoService;

    /**
     * 查询店员动态信息列表
     */
    @ApiOperation(value = "分页查询动态列表", notes = "分页查询店员动态信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkArticleReturnVo.class, responseContainer = "Page")})
    @PostMapping("/listByPage")
    public R list(@ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayClerkArticleQueryVo vo) {
        IPage<PlayClerkArticleReturnVo> list = playClerkArticleInfoService.selectByPage(vo, false);
        return R.ok(list);
    }

    /**
     * 修改店员动态信息
     */
    @ApiOperation(value = "修改动态审核状态", notes = "更新店员动态审核状态，通过或拒绝")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败")})
    // @PreAuthorize("@customSs.hasPermission('clerk:article:update')")
    @Log(title = "店员动态信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "审核状态信息", required = true) @RequestBody PlayClerkArticleReviewStateEditVo vo) {
        PlayClerkArticleInfoEntity entity = ConvertUtil.entityToVo(vo, PlayClerkArticleInfoEntity.class);
        boolean success = playClerkArticleInfoService.update(entity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除店员动态信息
     */
    @ApiOperation(value = "删除动态", notes = "根据ID批量删除店员动态信息")
    @ApiImplicitParam(name = "ids", value = "动态ID数组", required = true, paramType = "path", dataType = "String[]", example = "1,2,3")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    // @PreAuthorize("@customSs.hasPermission('clerk:article:delete')")
    @Log(title = "店员动态信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        for (String id : ids) {
            playClerkArticleInfoService.deletePlayClerkArticleInfoById(id);
            playCustomArticleInfoService.deleteByArticleId(id);
        }
        return R.ok();
    }
}
