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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.shop.module.entity.PlayShopArticleInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoAddVo;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoQueryVo;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoUpdateVo;
import com.starry.admin.modules.shop.service.IPlayShopArticleInfoService;
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 java.util.Date;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺文章信息Controller
 *
 * @author admin
 * @since 2024-06-05
 */
@Api(tags = "店铺文章管理", description = "店铺文章信息管理相关接口，包括查询、新增、修改和删除等操作")
@RestController
@RequestMapping("/shop/articleInfo")
public class PlayShopArticleInfoController {

    @Resource
    private IPlayShopArticleInfoService playShopArticleInfoService;

    /**
     * 查询店铺文章信息列表
     */
    @ApiOperation(value = "分页查询文章列表", notes = "分页查询店铺文章信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayShopArticleInfoEntity.class, responseContainer = "Page")})
    // @PreAuthorize("@customSs.hasPermission('shop:articleInfo:list')")
    @PostMapping("/listByPage")
    public R listByPage(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayShopArticleInfoQueryVo vo) {
        IPage<PlayShopArticleInfoEntity> list = playShopArticleInfoService.selectByPage(vo);
        return R.ok(list);
    }

    /**
     * 新增店铺文章信息
     */
    @ApiOperation(value = "新增或修改文章", notes = "新增或修改店铺文章信息")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:articleInfo:add')")
    @PostMapping("/create")
    public R create(@ApiParam(value = "文章信息", required = true) @Validated @RequestBody PlayShopArticleInfoAddVo vo) {
        PlayShopArticleInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopArticleInfoEntity.class);
        PlayShopArticleInfoEntity entity1 = playShopArticleInfoService.selectByType(entity.getArticleType());
        if (entity.getId() == null) {
            if (entity1 != null) {
                throw new CustomException("同类型的文章只能存在一个");
            }
            playShopArticleInfoService.create(entity);
        } else {
            if (entity1 != null && !entity1.getId().equals(entity.getId())) {
                throw new CustomException("同类型的文章只能存在一个");
            }
            entity.setUpdatedTime(new Date());
            playShopArticleInfoService.update(entity);
        }

        return R.ok();
    }

    /**
     * 修改店铺文章信息
     */
    @ApiOperation(value = "更新文章状态", notes = "修改店铺文章状态")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:articleInfo:update')")
    @PostMapping("/handleUpdateState")
    public R listByPage(
            @ApiParam(value = "更新信息", required = true) @Validated @RequestBody PlayShopArticleInfoUpdateVo vo) {
        PlayShopArticleInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopArticleInfoEntity.class);
        playShopArticleInfoService.update(entity);
        return R.ok();
    }

    /**
     * 删除店铺文章信息
     */
    @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('shop:articleInfo:delete')")
    @Log(title = "店铺文章信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(playShopArticleInfoService.deleteByIds(ids));
    }

}
