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 org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 店员动态信息Controller
 *
 * @author admin
 * @since 2024-05-04
 */
@RestController
@RequestMapping("/clerk/article")
public class PlayClerkArticleInfoController {
    @Resource
    private IPlayClerkArticleInfoService playClerkArticleInfoService;
    @Resource
    private IPlayCustomArticleInfoService playCustomArticleInfoService;


    /**
     * 查询店员动态信息列表
     */
    @PostMapping("/listByPage")
    public R list(@Validated @RequestBody PlayClerkArticleQueryVo vo) {
        IPage<PlayClerkArticleReturnVo> list = playClerkArticleInfoService.selectByPage(vo,false);
        return R.ok(list);
    }


    /**
     * 修改店员动态信息
     */
    //@PreAuthorize("@customSs.hasPermission('clerk:article:update')")
    @Log(title = "店员动态信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public R update(@RequestBody PlayClerkArticleReviewStateEditVo vo) {
        PlayClerkArticleInfoEntity entity = ConvertUtil.entityToVo(vo, PlayClerkArticleInfoEntity.class);
        boolean success = playClerkArticleInfoService.update(entity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除店员动态信息
     */
    //@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();
    }
}
